![如何从变量传递主机名并使用 ansible 中的主机名更新配置文件](https://linux22.com/image/758879/%E5%A6%82%E4%BD%95%E4%BB%8E%E5%8F%98%E9%87%8F%E4%BC%A0%E9%80%92%E4%B8%BB%E6%9C%BA%E5%90%8D%E5%B9%B6%E4%BD%BF%E7%94%A8%20ansible%20%E4%B8%AD%E7%9A%84%E4%B8%BB%E6%9C%BA%E5%90%8D%E6%9B%B4%E6%96%B0%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6.png)
我正在尝试将服务器的主机名写入配置文件,但它给我带来了问题。你能帮忙看一下代码吗?
---
- name: Update host.
hosts: all
connection: local
# Default Value
domain: '{{ default_domain }}'
hostname: “serv1.{{ '{{' }} domain {{ '}}' }}"
tasks:
- name: Update hostname config file
block:
- lineinfile:
path: /home/test/conf.yml
state: present
regexp: 'authorization-uri:(.*)$'
line: "authorization-uri: https://{{ serv1.{{ '{{' }} domain {{ '}}' }}/key/auth/mvn/vars/lenz/svc/chk”
Domain = serv1
Hostname = app2
Output should be:
https://serv1.app2/key/auth/mvn/vars/lenz/svc/chk”
答案1
如果要将domain
变量包含到hostname
,则需要使用以下语法:
hostname: 'serv1.{{ domain }}'
在lineinfile
任务中:
line : 'authorization-uri: https://{{ hostname }}/key/auth/mvn/vars/lenz/svc/chk'