how to use dynamic calling of a variable which is present in vars file

24 views
Skip to first unread message

Gopi Krishna

unread,
Jan 24, 2018, 6:09:45 AM1/24/18
to Ansible Project
Hi All,

My variable file:
     
     VISEBS_SYSTEM: password

My yaml.file:
 
I'll be passing a argument to my playbook as -e "src=VISEBS", in my playbook i am trying to get dynamically the VISEBS_SYSTEM value. any suggestions 

   - include_vars: variable_file

   - debug: 
        msg: "{{ '{{src}}_SYSTEM' }}"

as {{src}} will be replaced by VISEBS, i'm thinking that it will transform to {{VISEBS_SYSTEM}} which will finally print "password", but its not happening what is the
exact way to achieve this.


Kai Stian Olstad

unread,
Jan 24, 2018, 7:00:34 AM1/24/18
to ansible...@googlegroups.com
On 24.01.2018 12:09, Gopi Krishna wrote:
> - debug:
> msg: "{{ '{{src}}_SYSTEM' }}"
>
> as {{src}} will be replaced by VISEBS, i'm thinking that it will
> transform
> to {{VISEBS_SYSTEM}} which will finally print "password", but its not
> happening what is the
> exact way to achieve this.

You can't stack curly brackets, so the solution is

msg: "{{ vars[src ~ '_SYSTEM' }}"

~ (tilde) is used for concatenate strings in Jinja

--
Kai Stian Olstad

Gopi Krishna

unread,
Jan 25, 2018, 12:44:20 AM1/25/18
to Ansible Project
Hi Kai,

Thanks, it worked, but where can i find these kind of descriptions in ansible. as the ansible doc's didn't have these kind of docs i guess.

Kai Stian Olstad

unread,
Jan 25, 2018, 1:43:35 AM1/25/18
to ansible...@googlegroups.com
On 25.01.2018 06:44, Gopi Krishna wrote:
> Thanks, it worked, but where can i find these kind of descriptions in
> ansible. as the ansible doc's didn't have these kind of docs i guess.

The FAQ covers some of the pitfalls
http://docs.ansible.com/ansible/latest/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names

Since this is Jinja maybe it's documented there also.


--
Kai Stian Olstad

Gopi Krishna

unread,
Jan 25, 2018, 9:02:06 AM1/25/18
to Ansible Project
sure, thanks kai
Reply all
Reply to author
Forward
0 new messages