在跨主机调用变量时可能会在变量中又引用另一个变量的情况
- 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']}},是无法通过语法检查的。当有多个变量的时候,只写一个{{}}就可以了,不需要嵌套写{{}}。