# Evaluate double-templated variable:  "{{ versions\_{{ choosen\_app }} }}"

**URL:** <https://forum.ansible.com/t/evaluate-double-templated-variable-versions-choosen-app/20041>\
**Category:** Ansible Project\
**Created:** [December 17, 2015, 2:57pm UTC](https://forum.ansible.com/t/evaluate-double-templated-variable-versions-choosen-app/20041 "2015-12-17T14:57:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_Muszynski](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jakub\_Muszynski](https://forum.ansible.com/u/Jakub_Muszynski)\
**Post date:** [December 17, 2015, 2:57pm UTC](https://forum.ansible.com/t/evaluate-double-templated-variable-versions-choosen-app/20041/1 "2015-12-17T14:57:30Z")

</div>

Is it possible to double template the vars?  
vars:  
versions\_appA: ‘1.2.3.4’  
versions\_appB: ‘4.3.2.1’

in tasks (for jinja template):

the ideal would be (this is fake):

- set\_fact:  
choosen\_app: ‘appA’  
choosen\_app\_version: “{{ versions\_{{ choosen\_app }} }}”

Expected result  
choosen\_app\_version: ‘1.2.3.4’

I can get as close as:

- set\_fact:  
choosen\_app\_version\_tmp: “versions\_{{ choosen\_app | default() }}”
- set\_fact:  
choosen\_app\_version: “{{ choosen\_app\_version\_tmp }}”

that evaluates to  
choosen\_app\_version: versions\_appA  
(I need version number - not a name of the variable)

---

<div class="post-metadata">

**Author:** ![Jakub\_Muszynski](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jakub\_Muszynski](https://forum.ansible.com/u/Jakub_Muszynski)\
**Post date:** [December 17, 2015, 3:05pm UTC](https://forum.ansible.com/t/evaluate-double-templated-variable-versions-choosen-app/20041/2 "2015-12-17T15:05:29Z")

</div>

There was suggestion on irc channel to define it as:  
choosen\_app\_version: “{{ ‘versions\_’ + choosen\_app }}”

still evaluates to  
choosen\_app\_version: versions\_appA

W dniu czwartek, 17 grudnia 2015 15:57:30 UTC+1 użytkownik sirkubax napisał:

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [December 17, 2015, 3:55pm UTC](https://forum.ansible.com/t/evaluate-double-templated-variable-versions-choosen-app/20041/3 "2015-12-17T15:55:32Z")

</div>

There's no such thing as "double mustaches" -- once you're inside a {{ }}  
block, you're parsing Jinja.

When I want to do something like this, I use a more complicated struct,  
like

&nbsp;&nbsp;versions:  
&nbsp;&nbsp;&nbsp;&nbsp;appA: "1.2.3.4"  
&nbsp;&nbsp;&nbsp;&nbsp;appB: "4.3.2.1"

and then you can say

&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;choosen\_app: 'appA'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;choosen\_app\_version: "{{ versions[choosen\_app] }}"

and that should work.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

---

<div class="post-metadata">

**Author:** ![Jakub\_Muszynski](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jakub\_Muszynski](https://forum.ansible.com/u/Jakub_Muszynski)\
**Post date:** [December 17, 2015, 6:25pm UTC](https://forum.ansible.com/t/evaluate-double-templated-variable-versions-choosen-app/20041/4 "2015-12-17T18:25:10Z")

</div>

Thanks Josh.

I’m using that in other part of my inventory.  
I was looking for ‘non\_dict’ variable since I had some issue passing json as extra-args in rundeck yaml job deffiniton.  
Well  
I gave it another try, and I did succede 😉  
Great

just to show the example

- id: xxxxxxxxxxxxxxxxxxxxxx  
project: xxxxxxxxx  
loglevel: INFO  
sequence:  
keepgoing: false  
strategy: node-first  
commands:

- exec: sudo su - ansible bash -c “”“export ANSIBLE\_FORCE\_COLOR=true; cd ~/ansible; source ~/ansible/environment.sh; ansible-playbook ~/ansible/etc/playbooks/playbook.yml **-e ‘{“versions[appA]”:“$RD\_OPTION\_APP”}’** “””  
description: setting up the app  
(…)  
options:  
app:  
value: ‘1.2.3.4’ #here as a default value comes the value from the template → **“{{ versions[choosen\_app] }}”**  
values:

- other\_options

- some dynamic option

W dniu czwartek, 17 grudnia 2015 16:55:53 UTC+1 użytkownik Josh Smift napisał:

---

<div class="post-metadata">

**Author:** ![Jakub\_Muszynski](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jakub\_Muszynski](https://forum.ansible.com/u/Jakub_Muszynski)\
**Post date:** [December 18, 2015, 11:52am UTC](https://forum.ansible.com/t/evaluate-double-templated-variable-versions-choosen-app/20041/5 "2015-12-18T11:52:38Z")

</div>

Unfortunatelly, it does not work as stated here  
[https://groups.google.com/forum/#!topic/ansible-project/AKpHsOod9Lo](https://groups.google.com/forum/#!topic/ansible-project/AKpHsOod9Lo)

The value does not get overriden unless the extra\_args is passed in a JSON format  
And I can not pass JSON in a yaml file (or I did not find a way for it yet)

Like this:  
-e ‘versions.myapp=“0.1.2”’  
not like that:  
-e ‘{ “versions”: { “myapp”: “0.1.2”} }’

Reason - while importing jobs into rundeck it does complain about  
ml files)

Error: Failed request to load jobs: Jobs Document was invalid for format yaml: mapping values are not allowed here  
-e '{“versions” **:** {"promo

^

W dniu czwartek, 17 grudnia 2015 19:25:10 UTC+1 użytkownik sirkubax napisał:

---

<div class="post-metadata">

**Author:** ![Jakub\_Muszynski](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jakub\_Muszynski](https://forum.ansible.com/u/Jakub_Muszynski)\
**Post date:** [December 18, 2015, 12:05pm UTC](https://forum.ansible.com/t/evaluate-double-templated-variable-versions-choosen-app/20041/6 "2015-12-18T12:05:03Z")

</div>

The answer is in the second topic

_Ok - I made it work in rundeck import_

_[http://symfony.com/doc/current/components/yaml/yaml\_format.html](http://symfony.com/doc/current/components/yaml/yaml_format.html)_  
_“Mappings use a colon followed by a space (`:` ) to mark each key/value pair:”_

_So I did some nasty change, removing the space in the JSON - it does not look well, but it works:_  
_-e ‘{ “versions”: { “myapp”: “0.1.2”} }’_  
_ **-e ‘{ “versions”:{ “myapp”:“0.1.2”} }’** _

W dniu piątek, 18 grudnia 2015 12:52:38 UTC+1 użytkownik sirkubax napisał:

---

<div class="post-metadata">

**Author:** ![Jakub\_Muszynski](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jakub\_Muszynski](https://forum.ansible.com/u/Jakub_Muszynski)\
**Post date:** [December 18, 2015, 4:00pm UTC](https://forum.ansible.com/t/evaluate-double-templated-variable-versions-choosen-app/20041/7 "2015-12-18T16:00:15Z")

</div>

I was to happy,  
it does import to rundeck, but then the commands (that works in shell), when executed via rundec, looses double-quotes, and a JSON fails to create the dict \*(\*_Expecting property name enclosed in double quotes)_  
[https://groups.google.com/forum/#!topic/rundeck-discuss/NWQ7Y6sHq60](https://groups.google.com/forum/#!topic/rundeck-discuss/NWQ7Y6sHq60)

W dniu piątek, 18 grudnia 2015 13:05:03 UTC+1 użytkownik sirkubax napisał:
