zozo6015
zozo6015

Reputation: 577

Ansible: get list of hosts in comma separated value

I have the following loop in a template:

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

the issue is that it gives the output in list of ip's and I need it in comma separated value. Any idea how to achieve that?

the answer I get look like this:

10.0.0.190
10.0.0.117
10.0.0.151

but I need it like this:

10.0.0.190,10.0.0.117,10.0.0.151

Upvotes: 5

Views: 5829

Answers (1)

techraf
techraf

Reputation: 68639

A quick fix to your Jinja2 template:

{% for host in groups['dbnodes'] -%}
 {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}{% if not loop.last %},{% endif %}
{%- endfor %}

Upvotes: 5

Related Questions