【发布时间】:2018-12-12 14:56:31
【问题描述】:
我不明白为什么找不到来自 localhost 的域变量,即使将完整的 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”根本不在主机变量中。我也尝试过使用 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
【问题讨论】:
标签: ansible