D
D
Darkcloud2020-07-03 15:15:23
Jinja
Darkcloud, 2020-07-03 15:15:23

How to create a variable name in ansible which will contain one more variable?

There is a variable "{{ some_staff1 }}".
I want to make a loop that will write:

var: "{{ some_staff1 }}"

The whole problem is to make it so that the index at the end of the variable is also a variable:

var: "{{ some_staff{{ item.index }} } }"
with_items:
- { index: 1}
- { index: 2}

What I described above is not a working construct
. But I hope the essence of what I want to achieve is clear.
It is necessary that there is one more variable inside the variable name. Perhaps it will be possible to solve the issue using jinja2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2020-07-03
@MaxKozlov

To be honest, I didn’t really understand, but it seems that something like that is needed?

- set_fact:
    var_{{ item.index }}: "{{ item.value }}"
  loop:
    - index: x1
      value: 'aaa'
    - index: x2
      value: 'bbb'
- debug: var=var_x1
- debug: var=var_x2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question