Evaluate dynamic variable name in ansible
Asked Answered
E

4

18

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?

Expletive answered 26/1, 2017 at 14:24 Comment(0)
T
33

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
Tachygraphy answered 22/7, 2018 at 15:57 Comment(0)
T
8
{{ hostvars[inventory_hostname][item + '_port'] }}

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

Testimonial answered 28/1, 2018 at 23:20 Comment(0)
E
5

I think you are searching for:

{{ vars[item ~ '_port'] }}
Effectually answered 26/1, 2019 at 2:2 Comment(8)
Please explain why it would work in addition to the answer.Hegarty
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] }}"Effectually
Wow! It actually better than lookup('vars', item ~ '_port') in may ways.Bluebeard
@Effectually 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.Navarrette
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.Ausgleich
vars is not documented and is an internal implementation detail. hostvars magic dict, or vars lookup, are documented, and in other answers.Alodee
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 loopsGorilla
This page of the documentation refers to this syntax: docs.ansible.com/ansible/latest/reference_appendices/…Galactopoietic
R
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

Ronnyronsard answered 7/9, 2021 at 23:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.