Ansible evaulate dynamic varaible name from vars

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?

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

- debug:
     msg: "{{ vars[item + '_port'] }}"
   with_items: "{{ m_name }}"

Thanks :slight_smile: