cancel
Showing results for 
Search instead for 
Did you mean: 
harjitsachar
Flight Engineer
Flight Engineer
  • 2,895 Views

Ansible(D0407): Calrification required for use of magic variable in Jinja2 template

Jump to solution

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

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
littlebigfab
Flight Engineer Flight Engineer
Flight Engineer
  • 2,871 Views

Re: Ansible(D0407): Calrification required for use of magic variable in Jinja2 template

Jump to solution

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

View solution in original post

Reply
Loading...
4 Replies
andkra
Flight Engineer Flight Engineer
Flight Engineer
  • 2,888 Views

Re: Ansible(D0407): Calrification required for use of magic variable in Jinja2 template

Jump to solution

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

0 Kudos
Reply
Loading...
Alexandre
Starfighter Starfighter
Starfighter
  • 2,883 Views

Re: Ansible(D0407): Calrification required for use of magic variable in Jinja2 template

Jump to solution

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

 

0 Kudos
Reply
Loading...
Fab
Cadet
Cadet
  • 2,877 Views

Re: Ansible(D0407): Calrification required for use of magic variable in Jinja2 template

Jump to solution
 
0 Kudos
Reply
Loading...
littlebigfab
Flight Engineer Flight Engineer
Flight Engineer
  • 2,872 Views

Re: Ansible(D0407): Calrification required for use of magic variable in Jinja2 template

Jump to solution

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

View solution in original post

Reply
Loading...
Join the discussion
You must log in to join this conversation.