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

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:

`

I think what you want is the following:

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

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

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']

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

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: