Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

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.