cancel
Showing results for 
Search instead for 
Did you mean: 
Nehal
Cadet
Cadet
  • 30.2K 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)
2 Solutions

Accepted Solutions
  • 18.8K 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

  • 9,670 Views

I agree with everyone that you need to remove the single quotes from host:

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

Most importantly, in your playbook you identified 'hosts: webservers'

All you need to make it 'hosts: all' instead and then apply a 'when' statement to perform the task to webservers only like this:

---
- name: create myhost
hosts: all
tasks:
- name: create /etc/myhost
template:
src: myhost.j2
dest: /etc/myhost
when: '"webservers" in group_names'
...

 

View solution in original post

Tags (3)
15 Replies
Alexandre
Starfighter Starfighter
Starfighter
  • 27.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
  • 27.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
  • 27.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
  • 27.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
  • 27.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
  • 27.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
  • 23.1K 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
  • 22.6K Views

Hi.

Try to remove single ' from host, like this:

.....

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

....

It works for me.

 

  • 18.8K 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.