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'"}
The fact ansible_facts['default_ipv4'] might only be set if you have a default route, so that the system can determine what it thinks your "default interface" is. If you're on some disconnected network or don't have a default route for some reason, I don't think it gets a value even if you have IP addresses assigned to network interfaces that are up. I'd have to check the docs to be certain.
,
Do me a favor, and add the following line (that I'm showing in BOLD)
to your playbook:
---
- name: Create hosts file on all managed hosts
hosts: dev
gathering_facts: true
tasks:
- name: Generate /etc/myhosts file using template
ansible.builtin.template:
src: hosts.j2
dest: /etc/myhosts
Standing by ...
Hi @Trevor
Result same as was shown above. gather_facts: true - is default setting
Alexandre
[student@workstation a]$ cat test.yml
---
- name: Create hosts file
hosts: dev
gather_facts: true
tasks:
- name: generate hosts
ansible.builtin.template:
src: hosts.j2
dest: /tmp/hosts
[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
Thanks ARoumiantsev for your report on this.
May I now get you to replace, in the jinja2 code, "default_ipv4",
with "ansible_default_ipv4".
This is the only substitution - leave everything else as is!!!
Standing by....
Hi @Trevor
[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 'ansible_default_ipv4'"}
PLAY RECAP *********************************************************************
node1 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Thanks again ARoumiantsev! I can now move on to investigating another
possiblity.
@Trevor To be clear: The situation is clear for me and it is good explanation from @bonnevil above. I only had tried to undestend the original issue from @Manjiri_28. For this propose I have reproduced possible version of his issue according info which he presented. It is intersting for me what is original issue - this one or something else? Alexandre
Forgive me Alexadre, but I'm not real clear on your message.
Are you essentially saying that you have the resolution to the
issue?
@Trevor Solution is here ( see my post above
[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
and explanation see @bonnevil
I am not sure that it is solution of original @Manjiri_28 's issue only
Alexandre
Alexamdre. do me another favor, and execute the following
command: # ansible all -m setup
I just want to see what (if any) facts are being retrieved.
Standing by ...
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.