这是我的问题,我需要使用一个变量“target_host”,然后将“_host”附加到它的值以获取我需要其值的另一个变量名称。
如果你看看我的剧本。任务 nbr 1,2,3 获取变量的值,但是 nbr 4 无法执行我所期望的操作。有没有其他方法可以在ansible中实现相同的目的?
---
- name: "Play to for dynamic groups"
hosts: local
vars:
- target_host: smtp
- smtp_host: smtp.max.com
tasks:
- name: testing
debug: msg={{ target_host }}
- name: testing
debug: msg={{ smtp_host }}
- name: testing
debug: msg={{ target_host }}_host
- name: testing
debug: msg={{ {{ target_host }}_host }}
Output:
TASK: [testing] ***************************************************************
ok: [127.0.0.1] => {
"msg": "smtp"
}
TASK: [testing] ***************************************************************
ok: [127.0.0.1] => {
"msg": "smtp.max.com"
}
TASK: [testing] ***************************************************************
ok: [127.0.0.1] => {
"msg": "smtp_host"
}
TASK: [testing] ***************************************************************
ok: [127.0.0.1] => {
"msg": "{{{{target_host}}_host}}"
}
如果你有一个像这样的变量
vars:
myvar: xxx
xxx_var: anothervalue
Ansible 的工作语法:
- debug: msg={{ vars[myvar + '_var'] }}
会给你类似的:
- debug: msg={{ xxx_var }}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)