cancel
Showing results for 
Search instead for 
Did you mean: 
  • 563 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
bonnevil
Starfighter Starfighter
Starfighter
  • 310 Views

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.

Trevor
Commander Commander
Commander
  • 153 Views

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 ...

Trevor "Red Hat Evangelist" Chandler
ARoumiantsev
Flight Engineer
Flight Engineer
  • 144 Views

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

Trevor
Commander Commander
Commander
  • 138 Views

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....

 

 

Trevor "Red Hat Evangelist" Chandler
ARoumiantsev
Flight Engineer
Flight Engineer
  • 136 Views

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

 

Trevor
Commander Commander
Commander
  • 134 Views

Thanks again ARoumiantsev!  I can now move on to investigating another 
possiblity.

Trevor "Red Hat Evangelist" Chandler
ARoumiantsev
Flight Engineer
Flight Engineer
  • 122 Views

@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 

Trevor
Commander Commander
Commander
  • 119 Views

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 "Red Hat Evangelist" Chandler
ARoumiantsev
Flight Engineer
Flight Engineer
  • 95 Views

@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

Trevor
Commander Commander
Commander
  • 88 Views

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 ...

Trevor "Red Hat Evangelist" Chandler
Join the discussion
You must log in to join this conversation.