Hi,
Need clarification for the follwing code snippet from Chapter 6 : Deploying Custom Files with Jinja2 Templates
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['fqdn'] }} {{ hostvars[host]['ansible_facts']['hostname'] }}
{% endfor %}
Is the "hostvars" a magic variable ? If so why can't we just use the "ansible_facts" variable as is.
Regards,
Harjit Sachar
Hi @harjitsachar,
Variables and facts are scoped to one host only. 'hostvars' is a magic variable which allows one host to access variables and facts from a different host or even all other hosts of your inventory.
Your code snippet generates a complete /etc/hosts file describing all hosts of your inventory. It doesn't matter where that template is going to be deployed, all hosts of your inventory are going to be in it. That is only possible by instructing each host to retrieve all hosts' private ip address, fqdn and name, looping over their facts to retrieve those information.
Using 'ansible_facts' directly in that template would instanciate a one-line file on each host, describing itself only. It wouldn't achieve the expected goal.
Run this playbook to better understand the structure of 'hostvars' :
---
- hosts: localhost
connection: local
tasks:
- debug:
var: hostvars
Hello,
from the ansible doc:
hostvars: A dictionary/map with all the hosts in inventory and variables assigned to them
With hostvars you have the possiblity to get facts from other hosts then the actuate one.
Greetings
Andreas
Hi,
In https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html?highlight=magic you can see that hostvars is in a list of magic variable.
Look, please, at simple example which show a differenses with using hostvars and simple facts as you asking:
[student@workstation test-lab]$ cat jinja.yml
---
- name: list of hostvars
hosts: lamp
tasks:
- name: task1
debug:
msg: "{% for host in groups['lamp'] %} {{ hostvars[host].ansible_fqdn }} {% endfor %}"
- name: task2
debug:
msg: "{% for host in groups['lamp'] %} {{ ansible_fqdn }} {% endfor %}"
...
[student@workstation test-lab]$ ansible-playbook jinja.yml
PLAY [list of hostvars] ********************************************************
TASK [Gathering Facts] *********************************************************
ok: [serverb.lab.example.com]
ok: [servera.lab.example.com]
TASK [task1] *******************************************************************
ok: [servera.lab.example.com] => {
"msg": " servera.lab.example.com serverb.lab.example.com "
}
ok: [serverb.lab.example.com] => {
"msg": " servera.lab.example.com serverb.lab.example.com "
}
TASK [task2] *******************************************************************
ok: [servera.lab.example.com] => {
"msg": " servera.lab.example.com servera.lab.example.com "
}
ok: [serverb.lab.example.com] => {
"msg": " serverb.lab.example.com serverb.lab.example.com "
}
PLAY RECAP *********************************************************************
servera.lab.example.com : ok=3 changed=0 unreachable=0 failed=0
serverb.lab.example.com : ok=3 changed=0 unreachable=0 failed=0
Hi @harjitsachar,
Variables and facts are scoped to one host only. 'hostvars' is a magic variable which allows one host to access variables and facts from a different host or even all other hosts of your inventory.
Your code snippet generates a complete /etc/hosts file describing all hosts of your inventory. It doesn't matter where that template is going to be deployed, all hosts of your inventory are going to be in it. That is only possible by instructing each host to retrieve all hosts' private ip address, fqdn and name, looping over their facts to retrieve those information.
Using 'ansible_facts' directly in that template would instanciate a one-line file on each host, describing itself only. It wouldn't achieve the expected goal.
Run this playbook to better understand the structure of 'hostvars' :
---
- hosts: localhost
connection: local
tasks:
- debug:
var: hostvars
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.