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'"}
More info, please. Inventory, playbook may be source of issue
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
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
I tried with this also
It also didnt work
More info, please.
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
@Manjiri_28 Is your host reachable ? Are you gathering facts for all of your hosts ?
yes
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.