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,')}}"
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 .
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', '.
Ansible provides a fact named ansible_all_ipv4_addresses that is a list of all ip addresses.
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
)
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With