ansible {{}}引用变量,变量中嵌套变量如何表示

在跨主机调用变量时可能会在变量中又引用另一个变量的情况

- name: get password
shell: "cat /home/{{elastic_user}}/a"
register: encrypt_out
when: inventory_hostname in groups['master']

- set_fact:
hash_pass: "{{encrypt_out.stdout}}"
when: inventory_hostname in groups['master']

- name: modify password in internal_users.yml
replace:
path: "{{install_path}}/opendistro-es/opendistroforelasticsearch-1.8.0/plugins/opendistro_security/securityconfig/internal_users.yml"
regexp: ".*hash:.+"
#replace: ' hash: "{{hash_pass}}"'
replace: " hash: "{{hostvars[groups['master'][0]]['hash_pass']}}""
如果 最后一行写成 {{hostvars[{{groups['master'][0]}}]['hass_pass']}},是无法通过语法检查的。当有多个变量的时候,只写一个{{}}就可以了,不需要嵌套写{{}}。
————————————————
版权声明:本文为CSDN博主「qq_29891169」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_29891169/article/details/110874616

原文地址:https://www.cnblogs.com/cheyunhua/p/14613999.html