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.
Regards,
Nehal Patel
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 %}
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'
...
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 %}
Hello!
I tried to use it without single quotes for host.
It is giving following error.
please note that i am not using [all] group manually under inventory.
My inventory file looks following.
I suspect that there is issue with group['all'] with jinja2 with hostvars. these three combine make bug.
Regards,
Nehal Patel.
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.
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
Hello!
It is still not working even if i specify all at hosts playbook key word.
Regards,
Nehal Patel
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.
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']
Hi.
Try to remove single ' from host, like this:
.....
{{ hostvars[ host ]['ansible_facts']['default_ipv4']['address'] }}
....
It works for me.
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 %}
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.