Nehal
Cadet
Cadet
  • 10.3K Views

Ansible hostvars with jinja2 template

Jump to solution

Hello!

Can anyone confirm there is bug with "template" module if we use jinja2 template along with hostvars?

I check / search the post related to it but no one is able to clearcut provide solution.

The following simple code of jinja2 template is not able to generate ip address of all host on all host.

{% for host in groups['all'] %}
{{ hostvars['host']['ansible_facts']['default_ipv4']['address'] }}
{% endfor %}

my ansible version is 2.8

Additional details regarding my playbook files and other files.

image (3).pngimage (2).pngimage (1).png

Regards,

Nehal Patel

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
Tech_Hub_YT
Cadet
Cadet
  • 1,773 Views

Instead of this -

{% for host in groups['all'] %}
{{ hostvars['host']['ansible_facts']['default_ipv4']['address'] }}
{% endfor %}

 Use it -

{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }}
{% endfor %}

View solution in original post

10 Replies
Alexandre
Starfighter Starfighter
Starfighter
  • 10.2K Views

Hi

It looks that you incorrectly use internal variable "host".  Try this :

{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }}
{% endfor %}

 

ddnehal
Cadet
Cadet
  • 10.2K Views

Hello!

 

I tried to use it without single quotes for host.

It is giving following error.

image (5).pngimage (4).png

please note that i am not using [all] group manually under inventory.

My inventory file looks following.

Screenshot 2020-10-15 112017.png

 

I suspect that there is issue with group['all'] with jinja2 with hostvars. these three combine make bug.

 

Regards,

Nehal Patel.

0 Kudos
ddnehal
Cadet
Cadet
  • 10.2K Views

Additional info.

If I use specific group at jinja2 template like following ( notice groups )

{% for host in groups['webservers'] %}

then the code is working without error but I am only able to get ipaddress /hostname/ fqdn for only those hosts which are in webservers group.

I want to specify all under groups. like groups['all'] only with this it is not working.

I want to include all hosts of inventory.

Regards,

Nehal Patel.

0 Kudos
Alexandre
Starfighter Starfighter
Starfighter
  • 10.2K Views

Hi

It is other story now.  

Only for hosts from webservers group ( it is shown in your create_myhost.yml file) facts are collected. For other hosts ( if you use "all" in you myhost.j2 ) facts info is absent in hostvars.

Simple solution for your request  :

  hosts: all

in your create_myhost.yml file

ddnehal
Cadet
Cadet
  • 10.2K Views

Hello!

It is still not working even if i specify all at hosts playbook key word.

 

image (9).pngimage (7).pngimage (8).png

 

Regards,

Nehal Patel

0 Kudos
Alexandre
Starfighter Starfighter
Starfighter
  • 10.1K Views

Hi

As you can see ansible_facts are collected now for all hosts. Current problem conserns only low level of ansible_fact hierarhy - address. May be it is some sort of typo or more serious problem. Look, please, with some debug tasks or simple ad-hoc setup command does "address" exist for all involved hosts. Good luck.

0 Kudos
  • 6,130 Views

Hi, I have a similar issue, to resolve this update you template as below:

{% for host in groups['all'] %}

{{ hostvars[host]['ansible_default_ipv4']['address'] }}

{% endfor %}

just delete ['ansible_facts']

0 Kudos
Cyrille_V
Cadet
Cadet
  • 5,575 Views

Hi.

Try to remove single ' from host, like this:

.....

{{ hostvars[ host ]['ansible_facts']['default_ipv4']['address'] }}

....

It works for me.

 

Tech_Hub_YT
Cadet
Cadet
  • 1,774 Views

Instead of this -

{% for host in groups['all'] %}
{{ hostvars['host']['ansible_facts']['default_ipv4']['address'] }}
{% endfor %}

 Use it -

{% for host in groups['all'] %}
{{ hostvars[host]['ansible_facts']['default_ipv4']['address'] }}
{% endfor %}
Join the discussion
You must log in to join this conversation.