Ansible: Get all the IP addresses of a group

ansible get ip address of another host
ansible inventory_hostname ip address
ip address in ansible playbook
ansible ping ip address
ansible hostvars
ansible get ip of specific interface
ansible groups
ansible resolve ip

Let's imagine an inventory file like this:

node-01 ansible_ssh_host=
node-02 ansible_ssh_host=
node-03 ansible_ssh_host=
node-04 ansible_ssh_host=
node-05 ansible_ssh_host=


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

  main_nodes_ips: ",,,"
  main_nodes_ips_with_port: ",,,"

This is what I got so far:

  main_nodes_ips: "{{groups['mainnodes']|join(',')}}"
  main_nodes_ips_with_port: "{{groups['mainnodes']|join(':3000,')}}"

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:

{% for host in groups['mainnodes'] %}
{% endfor %}

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.

Update2: 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. :/

  {% set main_nodes_ip_arr=[] %}
  {% for host in groups['mesos-slave'] %}
     {% if main_nodes_ip_arr.insert(loop.index,hostvars[host]['ansible_ssh_host']) %} {% endif %}
  {% endfor %}
  main_nodes_ips: "{{main_nodes_ip_arr|join(',')}}"
  main_nodes_ips_with_port: "{{main_nodes_ip_arr|join(':3000,')}}"

I find the magic map extract here.

main_nodes_ips: "{{ groups['mainnodes'] | map('extract', hostvars, ['ansible_host']) | join(',') }}"
main_nodes_ips_with_port: "{{ groups['mainnodes'] | map('extract', hostvars, ['ansible_host']) | join(':3000,') }}:3000"

An alternative(idea comes from here):

main_nodes_ips: "{{ groups['mainnodes'] | map('extract', hostvars, ['ansible_eth0', 'ipv4', 'address']) | join(',') }}"

(Suppose the interface is eth0)

ansible. Assume that you are using the 'template' module in Ansible and have an application that needs to list out all the IP addresses of  Assume that you are using the 'template' module in Ansible and have an application that needs to list out all the IP addresses of machines in another group. First, make sure your host is talked to in a previous play: - hosts: db_servers tasks: - # doesn't matter what you do, just that they were talked to previously.

i came across this problem a while back and this is what i came up with (not optimal, but it works)

# playbook.yml
  - hosts: localhost
    connection: local

      - name: create deploy template
          src: iplist.txt
          dest: /tmp/iplist.txt
      - include_vars: /tmp/iplist.txt

      - debug: var=ip

and the template file is

{% for h in groups['webservers'] %}
 - {{ hostvars[h].ansible_ssh_host }}
{% endfor %}

So I need to get a list of all IP addresses in my inventory to build an sshguard hosts: all name: setup sshguard become: true vars: list_of_ips: "{{ groups['all']  An Ansible pattern can refer to a single host, an IP address, an inventory group, a set of groups, or all hosts in your inventory. Patterns are highly flexible - you can exclude or require subsets of hosts, use wildcards or regular expressions, and more. Ansible executes on all inventory hosts included in the pattern.

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

main_nodes_ips: "{% set IP_ARR=[] %}{% for host in groups['mainnodes'] %}{% if IP_ARR.insert(loop.index,hostvars[host]['ansible_ssh_host']) %}{% endif %}{% endfor %}{{IP_ARR|join(',')}}"
main_nodes_ips_with_port: "{% set IP_ARR=[] %}{% for host in groups['mainnodes'] %}{% if IP_ARR.insert(loop.index,hostvars[host]['ansible_ssh_host']) %}{% endif %}{% endfor %}{{IP_ARR|join(':3000,')}

I have been searching this for a while especially when I was setting up GlusterFS. Challenge of getting all IPs of hosts within my group of  How to get the IP address of the current or remote host in Ansible. That’s the question, this article is going to address. While running a Playbook you might have had a requirement where you need to get the IP address of the connected and current remote host. There are many different ways to obtain the IP address of the remote host but it has multiple network channel like eth0 and enp0s8 etc.

I've done this by using ansible facts in a playbook. This playbook takes ansible_all_ipv4_addresses list and ansible_nodename (which is actually fully qualified domain name), iterates through all hosts and saves the data in localpath_to_save_ips file on your localhost. You can change localpath_to_save_ips to the absolute path on your localhost.

- hosts: all
  become: yes
  gather_facts: yes

  - name: get ip
    local_action: shell echo {{ ansible_all_ipv4_addresses }} {{ ansible_nodename }} >> localpath_to_save_ips

If you still want to filter out all non-IP address values, you can chain both filters However, IPv6 networks can have multiple IP addresses set on an interface:. Ansible: Get number of hosts in group. Thanks for contributing an answer to Stack Overflow! Ansible: Get all the IP addresses of a group. 0.

to a single host, an IP address, an inventory group, a set of groups, or all hosts in your inventory. If you use the IP address, you will once again get the error:. Ansible loop through group vars in template. Ask Question Asked 5 years, 6 months ago. Ansible: Get all the IP addresses of a group. Hot Network Questions

How to get the remote target server IP address in ansible playbook using /etc/​hosts files of all the hosts in the host group using ansible where  I am trying to use Ansible to add entries in the server hosts file. I have a group of servers that I need to talk to each other over a private LAN. My inventory file: [server_list] server1 server2 The task I am trying to get working:

OK. I had tried this before and it didn't really work. So I must have done something wrong back there. This works: - lineinfile: dest=/etc/hosts line="{{  Ansible Hosts File. Home > Network Automation > Ansible Tutorials > Ansible Hosts File. The Ansible Hosts File or Inventory file tells Ansible about the hosts that it can connect to. For Ansible to automate a Linux Server, Network device or Cloud server it has to exist within the inventory (also known as the Ansible hosts file) and saved in either YAML or INI format.

I also have hosts in group [reporters] . So I want to set a variable on reporters from groups['collectors'][0] ip address. But the playbook for setting up reporters doesn'  No: ansible_default_ipv4 is gathered by ansible during gather_facts, and therefore must run on a host in order to figure out that hosts' facts (inc it's ip address) Yes: ansible supports facts caching.

  • For anyone using version 2.0+ ansible_ssh_host has been deprecated. This solution still works but you need to replace it with ansible_host
  • You wrote you are not too happy with your own anser. You have been pretty close to the best practice. Please set McKelvins as the accepted one now. Thank you.
  • Great big middle finger to RedHat for ruining the Ansible support portal. The second link is dead.
  • Note that the alternative only works if all servers have all the main interface set to eth0
  • and note also that ansible_ssh_host has been renamed ansible_host since ansible 2.0
  • Thanks @wedi. Updated.
  • For the second part ansible_default_ipv4 might be more reliable, since interface names have become unpredictable....
  • Creating a task for that seems a bit overkill.
  • @McKelvin's answer should be the accepted solution, because it's far superior to this one and the recommended way by the Ansible official documentation to do it.
  • While this code snippet may solve the question, including an explanation helps to improve the quality of your response. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.