Ansible: Get all the IP addresses of a group


Let’s imagine an inventory file like this:

In my playbook I now want to create some variables containing the IP addresses of the group mainnodes:

This is what I got so far:

but that would use the host names instead of the IP addresses.

Any ideas how this could be done?


looking at the docs for a while, I think this would allow me to loop through all the ip adresses:

But I just can’t figure out how to create an array that holds all these IPs. So that I can use the |join() command on them.

I just thought I had figured it out… but it turns out that you cannot use the {% %} syntax in the playbook… or can I?
Well in the vars section it didn’t. :/


I got it to work on my own now. I’m not too happy about the solution, but it will do:

Leave a Reply