Question:
I have vars where I put something like this:
1 2 3 4 |
vars/main.yml hello_port: 80 world_port: 81 |
in my ansbile file I load the vars with
1 2 3 |
vars_files: - ./vars/main.yml |
This is how I initialize m_name:
1 2 3 4 5 6 |
- name: set_fact set_fact: m_name: - 'hello' - 'world' |
and after that I have task with iterate using with_items:
1 2 3 4 |
- debug: msg: "{{ (item + '_port') }}" with_items: "{{ m_name }}" |
But I’ve got as output
1 2 3 |
hello_port world_port |
not their values.
OK I find that if I use debug var it is working. But If I want to put this expression "{{ (item + '_port') }}"
for an example in shell task it does not evaluate it. Is there a way to evaluate the dynamically created variables name – to get the value?
Answer:
https://docs.ansible.com/ansible/2.5/plugins/lookup/vars.html
1 2 3 4 5 6 |
- name: Show value of 'variablename' debug: msg="{{ lookup('vars', 'variabl' + myvar)}}" vars: variablename: hello myvar: ename |