# How can I loop over all hosts in a group in this playbook?

**URL:** <https://forum.ansible.com/t/how-can-i-loop-over-all-hosts-in-a-group-in-this-playbook/15950>\
**Category:** Ansible Project\
**Created:** [August 14, 2014, 9:16pm UTC](https://forum.ansible.com/t/how-can-i-loop-over-all-hosts-in-a-group-in-this-playbook/15950 "2014-08-14T21:16:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abe\_Voelker](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Abe\_Voelker](https://forum.ansible.com/u/Abe_Voelker)\
**Post date:** [August 14, 2014, 9:16pm UTC](https://forum.ansible.com/t/how-can-i-loop-over-all-hosts-in-a-group-in-this-playbook/15950/1 "2014-08-14T21:16:32Z")

</div>

I have an inventory file like this:

`  
[webservers]  
10.0.0.1  
10.0.0.2  
10.0.0.3

[postgresservers]  
10.0.0.4  
`

And I’m trying to run this playbook against my Postgres host:

`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [August 14, 2014, 11:04pm UTC](https://forum.ansible.com/t/how-can-i-loop-over-all-hosts-in-a-group-in-this-playbook/15950/2 "2014-08-14T23:04:28Z")

</div>

I think what you want is the following:

---

<div class="post-metadata">

**Author:** ![Abe\_Voelker](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Abe\_Voelker](https://forum.ansible.com/u/Abe_Voelker)\
**Post date:** [August 15, 2014, 5:30am UTC](https://forum.ansible.com/t/how-can-i-loop-over-all-hosts-in-a-group-in-this-playbook/15950/3 "2014-08-15T05:30:15Z")

</div>

Matt, thanks so much, I do believe that did the trick!

---

<div class="post-metadata">

**Author:** ![Venkata\_Vuppala](https://avatars.discourse-cdn.com/v4/letter/v/e36b37/32.png) [@Venkata\_Vuppala](https://forum.ansible.com/u/Venkata_Vuppala)\
**Post date:** [February 25, 2019, 4:36pm UTC](https://forum.ansible.com/t/how-can-i-loop-over-all-hosts-in-a-group-in-this-playbook/15950/4 "2019-02-25T16:36:24Z")

</div>

Hi Matt,

Thanks for the with\_items variable, but can this looping system be used to scan for hosts deep inside the inventory file rather than groups (a child group? )  
admin-1/2 are hosts here. I am using Ansible 2.4

Eg:

[admin:children]  
admin-1  
admin-2

I did try with the below syntax, but it didn’t work. I think Ansible didn’t like it either.

- -A INPUT -s “{{ hostvars[my\_platform + my\_server + [item]][‘ansible\_ssh\_host’] }}” -j ACCEPT  
with\_items: groups[‘admin:children’]

Regards  
VK

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 25, 2019, 4:51pm UTC](https://forum.ansible.com/t/how-can-i-loop-over-all-hosts-in-a-group-in-this-playbook/15950/5 "2019-02-25T16:51:46Z")

</div>

> Thanks for the with\_items variable, but can this looping system be used to  
> scan for hosts deep inside the inventory file rather than groups (a child  
> group? )

nsible inventory kind of a flat structure, groups and hosts.

> admin-1/2 are hosts here. I am using Ansible 2.4
> 
> Eg:
> 
> [admin:children]  
> admin-1  
> admin-2

This means that group admin-1 and group admin-2 is member of group admin.

So if admin-1 and 2 are host this should be

[admin]  
admin-1  
admin-2

> I did try with the below syntax, but it didn't work. I think Ansible didn't  
> like it either.
> 
> - -A INPUT -s "{{ hostvars[my\_platform + my\_server + [item]]['ansible\_ssh\_host'] }}" -j ACCEPT

This Jinja template is not valid, I'm not sure what you are trying to do.

> with\_items: groups['admin:children']

this would be  
with\_items: groups['admin']

---

<div class="post-metadata">

**Author:** ![Venkata\_Vuppala](https://avatars.discourse-cdn.com/v4/letter/v/e36b37/32.png) [@Venkata\_Vuppala](https://forum.ansible.com/u/Venkata_Vuppala)\
**Post date:** [February 25, 2019, 5:08pm UTC](https://forum.ansible.com/t/how-can-i-loop-over-all-hosts-in-a-group-in-this-playbook/15950/6 "2019-02-25T17:08:56Z")

</div>

Hi Kai,

I was trying to write IPtable rules for database servers in ansible, wherein every time an admin server is added into the environment it should be allowed to ssh to the database server.  
I tried to use Matt’s idea of with\_items and tried to call hosts listed in the admin:children ( in the child group ) and unfortunately, it didn’t work.

My question was that with\_items can look for hosts listed in the child groups in the inventory?

Regards  
VK

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 25, 2019, 5:32pm UTC](https://forum.ansible.com/t/how-can-i-loop-over-all-hosts-in-a-group-in-this-playbook/15950/7 "2019-02-25T17:32:29Z")

</div>

There's nothing called a child group, that's just a syntax to say which group is a member in another group.  
These two inventory is the same just different syntax.

inventory1:
