# Iterate Hash in template.

**URL:** <https://forum.ansible.com/t/iterate-hash-in-template/20349>\
**Category:** Ansible Project\
**Created:** [January 26, 2016, 4:16am UTC](https://forum.ansible.com/t/iterate-hash-in-template/20349 "2016-01-26T04:16:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Blackburn](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@David\_Blackburn](https://forum.ansible.com/u/David_Blackburn)\
**Post date:** [January 26, 2016, 4:16am UTC](https://forum.ansible.com/t/iterate-hash-in-template/20349/1 "2016-01-26T04:16:31Z")

</div>

I am trying to iterate over a hash of hash of a list, but I do not seem to be having much luck, I have setup a group variable which I select using a variable, which appears to be working correctly as per the debug, but once in the template I don’t seem to be able to iterate over it no matter what permutations I use.

Any advice on this would be great.  
Thanks  
Dave

**cat group\_vars/all**  
boston:  
nameservers: [‘10.0.0.1’,‘10.0.0.2’]  
ntp: [‘[pool1.ntp.org](http://pool1.ntp.org)’,‘[pool2.ntp.org](http://pool2.ntp.org)’]  
losangeles:  
nameservers: [‘10.0.1.1’,‘10.0.1.2’]  
ntp: [‘[pool1.ntp.org](http://pool1.ntp.org)’,‘[pool2.ntp.org](http://pool2.ntp.org)’]

**cat main.yml**

---

<div class="post-metadata">

**Author:** ![David\_Blackburn](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@David\_Blackburn](https://forum.ansible.com/u/David_Blackburn)\
**Post date:** [January 26, 2016, 5:50am UTC](https://forum.ansible.com/t/iterate-hash-in-template/20349/2 "2016-01-26T05:50:29Z")

</div>

I managed to achieve this by using “hostvar” in my template is this the correct way to be referencing hashes in templates?

Thanks  
Dave

{% for servers in hostvars[inventory\_hostname][SITE][‘ntp’] %}  
server {{servers}}  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 26, 2016, 2:57pm UTC](https://forum.ansible.com/t/iterate-hash-in-template/20349/3 "2016-01-26T14:57:47Z")

</div>

its correct way to reference most vars

---

<div class="post-metadata">

**Author:** ![David\_Blackburn](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@David\_Blackburn](https://forum.ansible.com/u/David_Blackburn)\
**Post date:** [January 26, 2016, 4:22pm UTC](https://forum.ansible.com/t/iterate-hash-in-template/20349/4 "2016-01-26T16:22:13Z")

</div>

Thanks for clarifying this Brian.

Dave
