0

Folks, The following code is producing errors.

code:

{% for v in hostvars.iteritems() %}
    {{ v['ansible_all_ipv4_addresses'][0] }}  {{ v['ansible_hostname'] }}
{% endfor %}

error:

{'msg': "One or more undefined variables: 'tuple object' has no attribute 'ansible_all_ipv4_addresses'", 'failed': True}

What should this look like if i wanted an /etc/hosts file like:

192.168.111.222 hostnameA
192.168.111.211 hostnameB
...

Thanks!

CMag
  • 707
  • 2
  • 11
  • 32

1 Answers1

2

Looking at the Error message

'tuple object' has no attribute 'ansible_all_ipv4_addresses'

, it is clear that your hosts' facts does not either contain ansible_all_ipv4_addresses inside where you were searching or syntax parser caught you off-guard :)

Let's try this:

{% for minion in groups['web'] %}
 {{ hostvars[minion]['ansible_all_ipv4_addresses'][0] }} {{ hostvars[minion]['ansible_hostname'] }}
{% endfor %}

Consider we lay out our inventory like this

[web]
192.168.111.222 hostnameA
192.168.111.211 hostnameB

Hope it solves your problem. Cheers !!

kaji
  • 2,528
  • 16
  • 17
  • The issue is hostvars.iteritems()... switching to groups solved my problems. – CMag Dec 11 '13 at 15:30
  • Did something change? In 1.9.4 this fails: fatal: [data1]: FAILED! => {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible_all_ipv4_addresses'"} – bbaassssiiee Apr 11 '16 at 20:40