Reputation: 993
We are writing a for
loop in Jinja2, which will keep on concatenating the list variable to a string until the end of the list.We are storing the string to a variable. After the loop completes, we want to print the variable.
The code to perform that is as follows
{% set HOSTLIST = groups['master'] | map('extract', hostvars, ['ansible_default_ipv4', 'address'])| list %}
{% set TEST = "spark://" %}
{% for host in HOSTLIST %}
{% set sample = TEST + host %}
{% set TEST = sample+',' %}
{% endfor %}
{{ TEST }}
Can anybody point me, where we are doing a mistake.
Our output is as follows:
spark://
But the expected output should be like:
spark://192.168.49.111:7077,192.168.49.112:7077,
Upvotes: 0
Views: 1041
Reputation: 68229
Why not use it like this?
spark://{{ groups['master'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | map('regex_replace','$',':7077') | list | join(',') }}
Your original request to modify outer-scope variable inside a loop is solved only with jumping through some hoops, see Jinja2: Change the value of a variable inside a loop.
Upvotes: 1