Ansible evaulate dynamic varaible name from vars

43 views
Skip to first unread message

Yordan Borisov

unread,
Jan 26, 2017, 4:13:53 PM1/26/17
to Ansible Project, Йордан Борисов
I have vars where I put something like this:

vars/main.yml
hello_port: 80
world_port: 81

in my ansbile file I load the vars with 
  vars_files:
     - ./vars/main.yml

and after that I have task with iterate using with_items:

     - debug:
          msg: "{{ (item + '_port') }}"
       with_items: "{{ m_name }}"

But I've got as output 
hello_port
world_port

not the values. 
Is there a way to evaluate the dynamically created variables name - to get the value?

Johannes Kastl

unread,
Jan 27, 2017, 2:54:59 AM1/27/17
to ansible...@googlegroups.com
On 26.01.17 14:18 Yordan Borisov wrote:
> I have vars where I put something like this:
>
> vars/main.yml
>
> hello_port: 80
>
> world_port: 81
>
>
> in my ansbile file I load the vars with
> vars_files:
> - ./vars/main.yml
>
> and after that I have task with iterate using with_items:
>
> - debug:
> msg: "{{ (item + '_port') }}"
> with_items: "{{ m_name }}"

What does the variable m_name contain?

> But I've got as output
> hello_port
> world_port

I think your task is missing the lookup, i.e. it puts out the names of
the variable, that you want to get the value of.

Try this as the msg-line:
msg: "{{ [ (item + '_port') ] }}"

Or maybe you need to use hostvars syntax...

Johannes

signature.asc

Kai Stian Olstad

unread,
Jan 27, 2017, 3:40:11 AM1/27/17
to ansible...@googlegroups.com
- debug:
msg: "{{ vars[item + '_port'] }}"
with_items: "{{ m_name }}"

--
Kai Stian Olstad

Yordan Borisov

unread,
Feb 1, 2017, 5:52:40 AM2/1/17
to Ansible Project, iordan....@gmail.com
Thanks :)
Reply all
Reply to author
Forward
0 new messages