# How to perform recursive vars\[varname\] expansion in template?

**URL:** <https://forum.ansible.com/t/how-to-perform-recursive-vars-varname-expansion-in-template/24250>\
**Category:** Ansible Project\
**Created:** [April 19, 2017, 4:48pm UTC](https://forum.ansible.com/t/how-to-perform-recursive-vars-varname-expansion-in-template/24250 "2017-04-19T16:48:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Long\_Vu](https://avatars.discourse-cdn.com/v4/letter/l/65b543/32.png) [@Long\_Vu](https://forum.ansible.com/u/Long_Vu)\
**Post date:** [April 19, 2017, 4:48pm UTC](https://forum.ansible.com/t/how-to-perform-recursive-vars-varname-expansion-in-template/24250/1 "2017-04-19T16:48:53Z")

</div>

Hi,

In a template, I use “{{ vars[varname] }}”. “varname” is a default var declared this way “varname: {{ another\_var }}”.

I would expect in the template to get the value of another\_var, not the name of another\_var.

I have reproduced the problem in test/integration/targets/var\_blending at this gist [https://gist.github.com/tlvu/ade0a36dc426eea4b9f6a28381cc1fc1](https://gist.github.com/tlvu/ade0a36dc426eea4b9f6a28381cc1fc1) (runme.sh pass).

I am using version 2.3.0.0 (stable-2.3 c92765e26e).

The real usecase for this is I’d like to do something like {{ vars[prefix + ‘\_token’] }} in a template and prefix would be declared as “prefix: {{ prod\_prefix }}” or “prefix: {{ staging\_prefix }}” depending on the inventory file used. I think this usecase is immensely useful so I was a bit surprised it did not work.

If there is an existing bug, please point me to it. I did search for “recursive vars expansion” and did not find anything useful.

If there is any work-around, please let me know because I am pretty stuck right now.

Thanks,  
Long

---

<div class="post-metadata">

**Author:** ![Long\_Vu](https://avatars.discourse-cdn.com/v4/letter/l/65b543/32.png) [@Long\_Vu](https://forum.ansible.com/u/Long_Vu)\
**Post date:** [April 19, 2017, 5:25pm UTC](https://forum.ansible.com/t/how-to-perform-recursive-vars-varname-expansion-in-template/24250/2 "2017-04-19T17:25:44Z")

</div>

Found this issue [https://github.com/ansible/ansible/issues/15753](https://github.com/ansible/ansible/issues/15753) “Accessing a dictionary using {{ vars[‘dictname’] }} doesn’t interpolate variables in the dictionary elements” that is very close to mine.

Still no work-around.

---

<div class="post-metadata">

**Author:** ![Long\_Vu](https://avatars.discourse-cdn.com/v4/letter/l/65b543/32.png) [@Long\_Vu](https://forum.ansible.com/u/Long_Vu)\
**Post date:** [April 20, 2017, 5:11pm UTC](https://forum.ansible.com/t/how-to-perform-recursive-vars-varname-expansion-in-template/24250/3 "2017-04-20T17:11:29Z")

</div>

To help the next person, I found a work-around: use loops over a list of dict as a way to “inject” variables into the template.

So I needed something like {{ vars[prefix + ‘readonly\_token’] }}, {{ vars[prefix + ‘readwrite\_token’] }}, {{ vars[prefix + ‘readonly\_tokensecret’] }}, {{ vars[prefix + ‘readwrite\_tokensecret’] }} in the template.

In the same play, I also needed to instantiate the template for multiple values of ‘prefix’, each prefix can be a different hostname or mapped to the same hostname like “prefix: {{ another\_prefix }}”.

The fact that prefix can be mapped to another prefix is way breaks the syntax “{{ vars[prefix + ‘readonly\_token’] }}”.

So the work-around is like this:

vars:  
host1: { readonly\_token: “blah”, readwrite\_token: “blah”, readonly\_tokensecret: “blah”, readwrite\_tokensecret: “blah” }

host2: { readonly\_token: “blah”, readwrite\_token: “blah”, readonly\_tokensecret: “blah”, readwrite\_tokensecret: “blah” }

host3: { readonly\_token: “blah”, readwrite\_token: “blah”, readonly\_tokensecret: “blah”, readwrite\_tokensecret: “blah” }

host4: { readonly\_token: “blah”, readwrite\_token: “blah”, readonly\_tokensecret: “blah”, readwrite\_tokensecret: “blah” }

list\_of\_all\_hosts: [“{{ host1 }}”, “{{ host2 }}”, “{{ host3 }}”, “{{ host4 }}”]

template:  
src: template file  
dest: blah  
with\_items: “{{ list\_of\_all\_hosts }}”

In the template file, instead of {{ vars[prefix + ‘readonly\_token’] }} I can then use {{ item.readonly\_token }} and the dict mapping ensure proper values for me.

The loop inject “updated values” for me into the template for each run, as if I can re-define a variable for each template call.

I see using template with loop over a dict as a way to inject variable into the template instead of the usual way to create variable (default role var, group var, host var, role var, commandline var …)

---

<div class="post-metadata">

**Author:** ![Long\_Vu](https://avatars.discourse-cdn.com/v4/letter/l/65b543/32.png) [@Long\_Vu](https://forum.ansible.com/u/Long_Vu)\
**Post date:** [April 25, 2017, 3:48pm UTC](https://forum.ansible.com/t/how-to-perform-recursive-vars-varname-expansion-in-template/24250/4 "2017-04-25T15:48:39Z")

</div>

Re-reading my own post, the vars declaration should have been like this, which will trigger the “no recursive vars[varname] expansion in template” problem which will need the work-around.

vars:  
host1\_ro\_token: “blah”  
host1\_ro\_tokensecret: “blah”  
host1: { readonly\_token: “{{ host1\_ro\_token }}”, readwrite\_token: “blah”, readonly\_tokensecret: “{{ host1\_ro\_tokensecret }}”, readwrite\_tokensecret: “blah” }  
host2: { readonly\_token: “{{ host1\_ro\_token }}”, readwrite\_token: “blah”, readonly\_tokensecret: “{{ host1\_ro\_tokensecret }}”, readwrite\_tokensecret: “blah” }  
host3: { readonly\_token: “{{ host1\_ro\_token }}”, readwrite\_token: “blah”, readonly\_tokensecret: “{{ host1\_ro\_tokensecret }}”, readwrite\_tokensecret: “blah” }  
host4: { readonly\_token: “{{ host1\_ro\_token }}”, readwrite\_token: “blah”, readonly\_tokensecret: “{{ host1\_ro\_tokensecret }}”, readwrite\_tokensecret: “blah” }
