Dears,
I am trying to construct a variable inside the loop of ansible template. The value gets constructed, but I'm unable to use it outside the loop.
Looks like variables with same name inside the loop and outside the loop are pointing to actually different variables.
Is there any way to construct a varialbe inside the loop, and use it outside?
Example playbook:
---
- hosts: "localhost"
connection: local
gather_facts: False
vars:
test_list:
- foo
- bar
- gotcha
tasks:
- name: templating test
template: src=dynvars.j2 dest=/tmp/testvars.txt
dynvars.j2 template:
[0/0]
{% set mynewvar = "test" %}
{% for value in test_list %}
{% set mynewvar = value %}
{{ mynewvar }}
{% endfor %}
{{ mynewvar }}
Actual result:
Inside: foo
Inside: bar
Inside: gotcha
Outside:test <------- Here I would expect 'gotcha' as it was last one assigned inside the loop
Any responses appreciated!
Thanks