cancel
Showing results for 
Search instead for 
Did you mean: 
  • 565 Views

Jinja2 template error with for loops using fact variables

I am getting following error while executing playbook to create /etc/hosts like file using jinja2 template. For loop with fact variables are used in templates

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: 'dict object' has no attribute 'default_ipv4'. 'dict object' has no attribute 'default_ipv4'
fatal: [node1]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'default_ipv4'. 'dict object' has no attribute 'default_ipv4'"}

Labels (1)
21 Replies
ARoumiantsev
Flight Engineer
Flight Engineer
  • 335 Views

More info, please. Inventory, playbook may be source of issue

  • 281 Views

Fact variables are working in playbook.

But not in for loop in templates

The template is as follows

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

and Playbook is

---
- name: Create hosts file on all managed hosts
  hosts: dev
  tasks:
     - name: Generate /etc/myhosts file using template
      ansible.builtin.template:
         src: hosts.j2
         dest: /etc/myhosts

 

0 Kudos
  • 276 Views

Facts_1.pngFacts_error.png

0 Kudos
ARoumiantsev
Flight Engineer
Flight Engineer
  • 273 Views

Correct your template, please:

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

Good luck

0 Kudos
  • 257 Views

I tried with this also

It also didnt work

0 Kudos
ARoumiantsev
Flight Engineer
Flight Engineer
  • 240 Views

More info, please.

0 Kudos
ARoumiantsev
Flight Engineer
Flight Engineer
  • 154 Views

Hi @Manjiri_28 

I have reproduce same behaivor of your playbook here. May be it gives you idea to solve your issue. Put attantion to inventory and routing setting at node2 outside of dev group.

Good luck 

[student@workstation a]$ ansible -m setup -a "filter=ansible_default_ipv4" dev
node1 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "172.25.250.9",
"alias": "eth0",
"broadcast": "172.25.250.255",
"gateway": "172.25.250.254",
"interface": "eth0",
"macaddress": "52:54:00:00:fa:09",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "172.25.250.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
[student@workstation a]$ ansible-playbook test.yml

PLAY [Create hosts file] *******************************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [generate hosts] **********************************************************
fatal: [node1]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'default_ipv4'"}

PLAY RECAP *********************************************************************
node1 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

[student@workstation a]$ cat inventory
node2
[dev]
node1
[student@workstation a]$ ssh student@node2 ip route
172.25.249.0/24 dev eth1 proto kernel scope link src 172.25.249.10 metric 101
172.25.250.0/24 dev eth0 proto kernel scope link src 172.25.250.10 metric 100

0 Kudos
Chetan_Tiwary_
Community Manager
Community Manager
  • 324 Views

@Manjiri_28 Is your host reachable ? Are you gathering facts for all of your hosts ?

0 Kudos
  • 281 Views

yes

 

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