cancel
Showing results for 
Search instead for 
Did you mean: 
Nehal
Cadet
Cadet
  • 5,011 Views

Ansible hostvars with jinja2 template

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
8 Replies
Alexandre
Starfighter Starfighter
Starfighter
  • 5,005 Views

Re: Ansible hostvars with jinja2 template

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 %}

 

Reply
Loading...
ddnehal
Cadet
Cadet
  • 4,992 Views

Re: Ansible hostvars with jinja2 template

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
Reply
Loading...
ddnehal
Cadet
Cadet
  • 4,991 Views

Re: Ansible hostvars with jinja2 template

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
Reply
Loading...
Alexandre
Starfighter Starfighter
Starfighter
  • 4,982 Views

Re: Ansible hostvars with jinja2 template

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

Reply
Loading...
ddnehal
Cadet
Cadet
  • 4,950 Views

Re: Ansible hostvars with jinja2 template

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
Reply
Loading...
Alexandre
Starfighter Starfighter
Starfighter
  • 4,931 Views

Re: Ansible hostvars with jinja2 template

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
Reply
Loading...
  • 930 Views

Re: Ansible hostvars with jinja2 template

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
Reply
Loading...
Cyrille_V
Cadet
Cadet
  • 375 Views

Re: Ansible hostvars with jinja2 template

Hi.

Try to remove single ' from host, like this:

.....

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

....

It works for me.

 

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