Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: Get all the IP addresses of a group

Let's imagine an inventory file like this:

node-01 ansible_ssh_host=192.168.100.101 node-02 ansible_ssh_host=192.168.100.102 node-03 ansible_ssh_host=192.168.100.103 node-04 ansible_ssh_host=192.168.100.104 node-05 ansible_ssh_host=192.168.100.105  [mainnodes] node-[01:04] 

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

vars:   main_nodes_ips: "192.168.100.101,192.168.100.102,192.168.100.103,192.168.100.104"   main_nodes_ips_with_port: "192.168.100.101:3000,192.168.100.102:3000,192.168.100.103:3000,192.168.100.104:3000" 

This is what I got so far:

vars:   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?

Update:

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'] %}     {{hostvars[host]['ansible_ssh_host']}} {% 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. :/

vars:   {% 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,')}}" 
like image 213
Forivin Avatar asked Mar 31 '16 09:03

Forivin


People also ask

What is Inventory_hostname in Ansible?

The inventory_hostname is the hostname of the current host, as known by Ansible. If you have defined an alias for a host, this is the alias name. For example, if your inventory contains a line like this: server1 ansible_host=192.168.4.10. then inventory_hostname would be server1 .

Where is Group_vars in Ansible?

In this, group_vars directory is under Ansible base directory, which is by default /etc/ansible/. The files under group_vars can have extensions including '. yaml', '. yml', '.

What is ansible_all_ipv4_addresses?

Ansible provides a fact named ansible_all_ipv4_addresses that is a list of all ip addresses.


2 Answers

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)

like image 85
mckelvin Avatar answered Sep 16 '22 14:09

mckelvin


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      tasks:       - name: create deploy template         template:           src: iplist.txt           dest: /tmp/iplist.txt       - include_vars: /tmp/iplist.txt        - debug: var=ip 

and the template file is

ip: {% for h in groups['webservers'] %}  - {{ hostvars[h].ansible_ssh_host }} {% endfor %} 
like image 28
user2599522 Avatar answered Sep 18 '22 14:09

user2599522