# Unable to access hostvars in template

**URL:** <https://forum.ansible.com/t/unable-to-access-hostvars-in-template/16950>\
**Category:** Ansible Project\
**Created:** [December 3, 2014, 5:04pm UTC](https://forum.ansible.com/t/unable-to-access-hostvars-in-template/16950 "2014-12-03T17:04:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gordon\_Ross](https://avatars.discourse-cdn.com/v4/letter/g/a8b319/32.png) [@Gordon\_Ross](https://forum.ansible.com/u/Gordon_Ross)\
**Post date:** [December 3, 2014, 5:04pm UTC](https://forum.ansible.com/t/unable-to-access-hostvars-in-template/16950/1 "2014-12-03T17:04:22Z")

</div>

According to the Ansible docs ( [http://docs.ansible.com/faq.html#how-do-i-loop-over-a-list-of-hosts-in-a-group-inside-of-a-template](http://docs.ansible.com/faq.html#how-do-i-loop-over-a-list-of-hosts-in-a-group-inside-of-a-template) ) You can put the following in a template:

{% for host in groups[’test\_servers'] %}  
&nbsp;&nbsp;{{ hostvars[host]['ansible\_eth0']['ipv4']['address'] }}  
{% endfor %}

But when I do that, I get:

TASK: [Template Test] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [srv.exmpale.com] =\> {'msg': "One or more undefined variables: 'dict object' has no attribute 'ansible\_eth0'", 'failed': True}

If I put:

{% for host in groups['test\_servers'] %}  
&nbsp;&nbsp;{{ hostvars[host] }}  
{% endfor %}

then I get a Python formatted output of all the facts.

Yet if I put in the template:

{{ hostvars[inventory\_hostname]['ansible\_eth0']['ipv4']['address'] }}

or

{{ hostvars['srv.exmpale.com']['ansible\_eth0']['ipv4']['address'] }}

Then that works and I get the IP address in the file.

Google only reveals someone who had the same problem and gave up ☹

Thanks,

GTG

---

<div class="post-metadata">

**Author:** ![Jean\_Philippe\_Caruan](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jean\_Philippe\_Caruan](https://forum.ansible.com/u/Jean_Philippe_Caruan)\
**Post date:** [December 3, 2014, 5:32pm UTC](https://forum.ansible.com/t/unable-to-access-hostvars-in-template/16950/2 "2014-12-03T17:32:32Z")

</div>

I had a helpful answer for that:

> ansible server -m setup -i production, should provide you the correct

info.

> I suspect one of your hosts does not have an ipv4 address assigned to

eth1.

I added a condition:

{% if hostvars[host]['ansible\_eth1']['active'] %}

(replace eth1 by eth0 in your case)

see  
[https://groups.google.com/forum/#!msg/ansible-project/RS1Mvdfjk2k/plfaT1g7cN0J](https://groups.google.com/forum/#!msg/ansible-project/RS1Mvdfjk2k/plfaT1g7cN0J)

hope this helps
