Evaluate double-templated variable: "{{ versions_{{ choosen_app }} }}"

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)

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ł:

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

  versions:
    appA: "1.2.3.4"
    appB: "4.3.2.1"

and then you can say

  - set_fact:
      choosen_app: 'appA'
      choosen_app_version: "{{ versions[choosen_app] }}"

and that should work.

                                      -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.

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 :wink:
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ł:

Unfortunatelly, it does not work as stated here
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ł:

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
“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ł:

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

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