C
C
catquistador2022-01-24 10:28:05
Jinja
catquistador, 2022-01-24 10:28:05

How to dynamically get a dictionary by variable name?

Hey!
there is a set of dictionaries

project1_defaults:
  someprop: somevalue
project1_specific:
  someprop: somavalue
---
project2_defaults:
  ...
project2_specific:
       ...


tell me a beautiful option, how to load the corresponding dictionary when passing the "project" key from extra-args ansible, so that inside the template the fields can be described somehow like `"{{ project_value_defaults.someprop }}" / "{{ project_value_specific.someprop }} "`
or otherwise abstracted in the template on behalf of the passed project.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxKozlov, 2022-01-24
@catquistador

I don't know about prettiness, but this is
ansible.builtin.vars - Lookup templated value of variables
https://docs.ansible.com/ansible/latest/collection...
It should suit you

C
catquistador, 2022-01-24
@catquistador

Thanks, this is a great option.
In the call of the role from the playbook stuck

vars:
    project: "{{ lookup('vars', app)}}"
    project_specific: "{{ lookup('vars', app + '_specific' )}}"

and, of course, I pass app in the call.
in the template, bindings to specific dictionaries have disappeared

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question