cancel
Showing results for 
Search instead for 
Did you mean: 
jpisaniello
Mission Specialist
Mission Specialist
  • 4,877 Views

DO407 - jinja templating error?

Jump to solution

Hello All,

I have seen a post or two about this already, but there hasn't been any real solution proposed.

 

The following is given as a template to generate /etc/hosts:

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

However, after running this is seems that I receive "AnsibleUndefinedVariable: 'dict object' has no attribute 'default_ipv4'".

 

What is the confusion here? How else would one be able to easily generate an /etc/hosts file using a template?

 

Thanks

Labels (1)
0 Kudos
1 Solution

Accepted Solutions
jpisaniello
Mission Specialist
Mission Specialist
  • 4,864 Views

It looks like I was specifying  a specific group of hosts to move this template to... This can't be done since we haven't gathered facts for all of the hosts, only the ones in which we run our play against.. oops

View solution in original post

2 Replies
jpisaniello
Mission Specialist
Mission Specialist
  • 4,865 Views

It looks like I was specifying  a specific group of hosts to move this template to... This can't be done since we haven't gathered facts for all of the hosts, only the ones in which we run our play against.. oops

  • 3,313 Views

This code is working fine:

$ cat hosts.yml
---
- name: use template
hosts: all
tasks:
- template:
src: hosts.j2
dest: myhosts

$ cat hosts.j2
{% for host in groups['all'] %}

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

{% endfor %}

To debug just let {{ hostvars[host] }} in the j2 file and watch the output

0 Kudos
Join the discussion
You must log in to join this conversation.