Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible jinja2 concatenate IP addresses

I would like to cocatenate a group of ips into a string.

example ip1:2181,ip2:2181,ip3:2181,etc

{% for host in groups['zookeeper'] %}
   {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}

I have the above code, but can't seem to quite figure out how to concatenate into a string.

searching for "Jinja2 concatenate" doesn't give me the info I need.

like image 476
Simply Seth Avatar asked Jan 30 '15 08:01

Simply Seth


1 Answers

You can use the 'extract' filter for this (provided you use ansible>=2.1):

{{ groups['zookeeper'] | map('extract', hostvars, ['ansible_eth0', 'ipv4', 'address']) | join(',') }}

More info: http://docs.ansible.com/ansible/playbooks_filters.html#extracting-values-from-containers

like image 58
Def_Os Avatar answered Sep 19 '22 06:09

Def_Os