How to get all the IP addresses of a group in Ansible

Mar 28, 2021 · 2 mins read · Post a comment
How to get all the IP addresses of a group in Ansible

Usually when developing an Ansible role, most of the times we need to write a jinja2 template for some configuration file. Speaking from personal experience, I was writing an elasticsearch role and I needed to add an elasticsearch.yml jinja2 template, and I was struggling a bit, because I had to add all of the elasticsearch group nodes IP addresses into the following line: ["", "", ""]

After a little bit of googling, I found a temporary solution on stack overflow: ["{{ elasticsearch_hosts }}"]
elasticsearch_hosts: "{{ groups['elasticnodes'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join(', ') }}"

which lead me to the following result: [,,]

We are still missing the double quotes, so I had to make some small adjustments. But first, let’s talk a bit about map and join functions.


  • Ansible

map function

The map function is allowing us to change every item in a list. So, we can try to extract all of the IP addresses of an Ansible group with:

{{ groups['elasticnodes'] | map('extract', hostvars, ['ansible_default_ipv4', 'address'])

Note that you can specify an interface rather then the ansible default ipv4 address, but keep in mind that ansible_default_ipv4 is more reliable since interface names can become unpredictable.

join function

The join function is a filter that’s allowing us to join a list of items into a string. Now, that we have IP addresses with the map function, we can concatenate them with double quotes:

{{ groups['elastic'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join('", "') }}

The final result: ["", "", ""]


In this post, we covered all the needed steps to concatenate a list of IP addresses of an Ansible group with double quotes and space between them.
More about the map function.
More about the join function.
Feel free to leave a comment below if you find this tutorial useful and follow our official channel on telegram.