我不明白为什么即使将完整的hostvars变量转储到另一个主机中时,也找不到本地主机的域变量。
这是完整的剧本:
---
- hosts: localhost
connection: local
vars:
- domain: foo.com
tasks:
- debug:
var: domain
- hosts: "{{ host_ip }}"
vars:
- domain: "{{ hostvars.localhost.domain }}"
tasks:
- copy:
content: "{{ hostvars }}"
dest: facts.json
- synchronize:
mode: pull
src: "./facts.json"
dest: "../facts.json"
- debug:
var: hostvars.localhost.domain
- debug:
var: domain
我可以理解它们是否隐藏在另一台主机或类似的主机下,但是域“ foo.com”根本不在hostvars中。我也尝试使用var:hostvars['localhost']['domain']
,但这没什么作用
运行时将产生以下输出:
$ ansible-playbook -v -u docker --private-key '/home/mikael/.ssh/aws' -i '1.2.3.4,' -e ansible_python_interpreter=/usr/bin/python3 /home/mikael/workspace/playbooks/vartest.yml --extra-vars 'host_ip=1.2.3.4'
Using /etc/ansible/ansible.cfg as config file
PLAY [localhost] ****************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ********************************************************************************************************************************************************************
ok: [localhost] => {
"domain": "foo.com"
}
PLAY [18.202.25.29] *************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************
ok: [18.202.25.29]
TASK [copy] *******************************************************************************************************************************************************************************************************************
changed: [18.202.25.29]
TASK [synchronize] ************************************************************************************************************************************************************************************************************
changed: [18.202.25.29]
TASK [debug] ********************************************************************************************************************************************************************
ok: [18.202.25.29] => {
"hostvars.localhost.domain": "VARIABLE IS NOT DEFINED!: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'domain'"
}
TASK [debug] ********************************************************************************************************************************************************************
ok: [18.202.25.29] => {
"domain": "VARIABLE IS NOT DEFINED!: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'domain'"
}
PLAY RECAP **********************************************************************************************************************************************************************
18.202.25.29 : ok=5 changed=2 unreachable=0 failed=0
localhost : ok=2 changed=0 unreachable=0 failed=0
答案 0 :(得分:0)
“ domain”变量仅在指定的播放范围内持久存在。这仅使变量可用于该播放中的任务,但不会将其添加到该播放中每个主机的事实集合中
相反,将任务添加到localhost播放中,该任务将一个事实添加到localhost的事实集合中,该事实将保留到播放之后:
- name: Set domain variable
set_fact:
domain: foo.com