# Cannot iterate over hosts in group of groups inside of a template

**URL:** <https://forum.ansible.com/t/cannot-iterate-over-hosts-in-group-of-groups-inside-of-a-template/14973>\
**Category:** Ansible Project\
**Created:** [April 9, 2014, 3:24am UTC](https://forum.ansible.com/t/cannot-iterate-over-hosts-in-group-of-groups-inside-of-a-template/14973 "2014-04-09T03:24:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juergen\_Brendel1](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@Juergen\_Brendel1](https://forum.ansible.com/u/Juergen_Brendel1)\
**Post date:** [April 9, 2014, 3:24am UTC](https://forum.ansible.com/t/cannot-iterate-over-hosts-in-group-of-groups-inside-of-a-template/14973/1 "2014-04-09T03:24:20Z")

</div>

Hello!

I don't seem to be able to iterate over the hosts of a group of groups  
within a template.

My inventory file looks similar to this:

&nbsp;&nbsp;[group1]

&nbsp;&nbsp;[group2]

&nbsp;&nbsp;[group3:children]  
&nbsp;&nbsp;group1  
&nbsp;&nbsp;group2

The groups "group1" and "group2" are initially left empty. However, they  
are populated with dynamically created EC2 instances by some playbook.

Later on, I have some tasks, which are just for hosts in group3:

&nbsp;&nbsp;hosts: group3  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- ...

This works as expected, so I know that Ansible knows what group3 is and  
what it consists of.

However, when I try to iterate over the hosts of group3 from within a  
template (in some other tasks), it does not work:

&nbsp;&nbsp;# Some template file  
&nbsp;&nbsp;{% for host in groups['group1'] %} this works {% endfor %}  
&nbsp;&nbsp;{% for host in groups['group2'] %} this works {% endfor %}  
&nbsp;&nbsp;{% for host in groups['group3'] %} this does NOT work {% endfor %}

By "not working" I mean: The template engine seems to think that the  
group3 is just empty.

Is that as intended? Is it a bug? Or am I just doing something wrong?

Any help would be greatly appreciated...

Thank you very much!

Juergen

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 9, 2014, 2:10pm UTC](https://forum.ansible.com/t/cannot-iterate-over-hosts-in-group-of-groups-inside-of-a-template/14973/2 "2014-04-09T14:10:24Z")

</div>

It’s not really intended that you can mix static groups and dynamic groups like this.

Each inventory source is mostly it’s own thing.

You would probably want to amend your inventory script so that the hosts came back in more groups as presently implemented.
