19

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

This is how I initialize m_name:

 - name: set_fact
     set_fact:
        m_name:
          - 'hello'
          - 'world'

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

Community
  • 1
  • 1
Jordan Borisov
  • 1,603
  • 6
  • 34
  • 69

4 Answers4

30

https://docs.ansible.com/ansible/2.5/plugins/lookup/vars.html

- name: Show value of 'variablename'
  debug: msg="{{ lookup('vars', 'variabl' + myvar)}}"
  vars:
    variablename: hello
    myvar: ename
Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37
abc
  • 627
  • 7
  • 17
8
{{ hostvars[inventory_hostname][item + '_port'] }}

http://docs.ansible.com/ansible/latest/faq.html#how-do-i-access-a-variable-name-programmatically

Ben Tse
  • 649
  • 5
  • 7
5

I think you are searching for:

{{ vars[item ~ '_port'] }}
GKE
  • 960
  • 8
  • 21
henry
  • 59
  • 1
  • 1
  • Please explain why it would work in addition to the answer. – GKE Jan 26 '19 at 03:22
  • I don't exactly know the implementation why it's working, but i'm using in templates to set passwords for different production stages which are stored in vault encrypted files. `password="{{ vars['password_' ~ stage] }}"` – henry Jan 28 '19 at 22:35
  • Wow! It actually better than lookup('vars', item ~ '_port') in may ways. – Vsevolod Mar 26 '21 at 23:45
  • @henry I couldn't find any documentation regards `vars[..]`. Do you recall maybe where you found it? I'm trying to understand what it actually doing. – Chen A. Apr 28 '21 at 18:13
  • This method will not resolve variable names in a list. e.g. In the OPs example, if 'hello' was a variable with the value of 'hello' then using var[item ~ '_port'] will NOT resolve the variablename to 'hello'. Using the lookup function does however. – GeoSword Aug 05 '21 at 06:25
  • 2
    vars is not documented and is an internal implementation detail. hostvars magic dict, or vars lookup, are documented, and in other answers. – John Mahowald Dec 03 '21 at 14:16
  • I stamped across vars by chance also, it acts weird with json_query, ansible 2.9.6. lookup('vars', 'vars') also acts weird as it can cause recursive loops – Yorai Levi Mar 23 '22 at 18:55
  • This page of the documentation refers to this syntax: https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#how-do-i-access-a-variable-name-programmatically – Gostega Dec 01 '22 at 08:29
1

I guess best way is to use varnames_lookup

- name: List variables that start with qz_
  debug: msg="{{ lookup('varnames', '^qz_.+')}}"

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/varnames_lookup.html