我不明白为什么找不到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"根本不在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
"domain"变量仅在指定的播放范围内保持。这只是使该播放中的任务可以使用该变量,但不会将其添加到播放中每个主机的事实集合中。
相反,向localhost播放添加一个任务,该任务将向localhost的事实集合添加一个事实,该事实集合将在播放之后持久化:
- name: Set domain variable
set_fact:
domain: foo.com