My Playbook:
- hosts: master
gather_facts: True
become: True
tasks:
- set_fact: headnode={{ ansible_all_ipv4_addresses[0] }}
- hosts: slave
become: True
tasks:
- debug: msg={{ hostvars.master }}
And no fact headnode
there. Am I missing something? Also, hostvars.master
doesn't contain ['ansible_eth0']
So is all the documentation wrong?
This doesn't work also: hostvars['master ']['ansible_eth0']['ipv4']['address']
And this: hostvars[groups['master'][0]]['ansible_eth0']
http://docs.ansible.com/ansible/playbooks_variables.html#registered-variables
hostvars.master:
ok: [slave0] => {
"msg": {
"ansible_check_mode": false,
"ansible_ssh_host": "xxx.westeurope.cloudapp.azure.com",
"ansible_ssh_port": 22,
"ansible_ssh_private_key_file": "/root/.ssh/id_rsa",
"ansible_ssh_user": "vagrant",
"ansible_version": {
"full": "2.2.2.0",
"major": 2,
"minor": 2,
"revision": 2,
"string": "2.2.2.0"
},
"group_names": [
"master"
],
"groups": {
"all": [
"master",
"slave0"
],
"master": [
"master"
],
"slave": [
"slave0"
],
"ungrouped": []
},
"inventory_dir": "/xxx/.vagrant/provisioners/ansible/inventory",
"inventory_file": null,
"inventory_hostname": "master",
"inventory_hostname_short": "master",
"omit": "__omit_place_holder__fb9f90a73039d94f14c1a1f0af132f1c36b9fb4a",
"playbook_dir": "/xxx/ansible"
}
}
The - debug: msg={{ hostvars.master }}
task shows only facts available from the inventory file. It does not contain the IP address nor quite a number of other facts that should be gathered from the remote machine by the regular setup module.
Now, as you seemingly run the playbook in Vagrant provisioning, it likely runs the playbook two times, first on master
only, then on slave0
only. Vagrant by default passes a --limit
argument to Ansible limiting the scope to the currently-provisioned machine only.
In result, when the playbook is being executed against slave0
, the first play (set to run on hosts: master
) is not executed and the facts from hosts belonging to the master
group (in this case from the host also named master
) are not gathered.
You can either change the scope of the provisioning for the slave0
machine in Vagrantfile by setting ansible.limit = 'all'
.
Or you can run ansible-playbook
afterwards pointing to the Vagrant-generated inventory file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.