Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 1 down vote accepted

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

share|improve this answer
    
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 at 20:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.