Reputation: 286
I have a template file config.j2
:
{% for host in groups['dbs'] %}
ips= {{ hostvars[host].ansible_default_ipv4.address }}
{% endfor %}
My output is:
ips= 192.168.231.91
ips= 192.168.231.92
ips= 192.168.231.93
I want save in array variable like this:
ips=['192.168.231.91','192.168.231.92','192.168.231.93']
How can do this?
Upvotes: 0
Views: 1948
Reputation: 68469
Solution
ips=[{{ groups['dbs'] | map("regex_replace", "(.*)", "'\\1'") | join(",") }}]
Explanation
Strings ips[
and ]
are printed directly in the template;
The Jinja2 expression processes the groups['dbs']
list:
map
filter applies a filter (regex_replace
) to individual elements of the list;
regex_replace
filter surrounds each list element (string) in single quotes;
join
filter converts the resulting list to comma-delimited string in the output.
Upvotes: 1