基于其他主机的变量构建模板
可以在Ansible Playbook中使用“hostvars”将其他主机的变量存储为变量,并在模板中使用这些变量。以下是一个示例:
在Inventory文件中,定义两个主机,并为它们指定变量:
[web]
web1 ansible_host=192.168.1.10 web_var=foo
web2 ansible_host=192.168.1.20 web_var=bar
在Playbook中,使用“hostvars”将这些变量存储起来:
- name: Store variables from other hosts
hosts: web
gather_facts: false
vars:
web1_var: "{{ hostvars['web1']['web_var'] }}"
web2_var: "{{ hostvars['web2']['web_var'] }}"
tasks:
- name: Print variables
debug:
msg: "Variable from web1 is {{ web1_var }} and variable from web2 is {{ web2_var }}"
这将从web1和web2主机中提取"web_var"变量,并将它们分别存储为"web1_var"和"web2_var"变量。
最后,可以在模板中使用这些变量。例如,在以下Jinja2模板中使用这些变量:
Variable from web1 is {{ web1_var }} and variable from web2 is {{ web2_var }}
这将生成以下内容:
Variable from web1 is foo and variable from web2 is bar
上一篇:BuildTaskVSBuild@1不能正确构建.NetFramework4的Web应用程序(aspx)
下一篇:Buildtemplatebasedonvariablesofotherhosts 构建基于其他主机变量的模板”为中文