0

I have a simplified test.yml playbook here for the example:

--- # test.yml

- hosts: localhost
  gather_facts: no
  tasks:
  - include_vars:
      file: varsfile.yml

  - debug:
      msg: |
        {% for property, value in {{ app }}_properties.items() %}
        {{ property }}={{ value }}
        {% endfor %}

And I have a varsfile.yml file which contains the following variables:

--- # varsfile.yml

myapp1_properties:
  key1: value1
  key2: value2

myapp2_properties:
  key1: value1

So what I would like to do is to be able to use a extra-variable in the following line:

{% for property, value in {{ app }}_properties.items() %}

and so I would like to launch my playbook as follows:

ansible-playbook test.yml -e "app=myapp1"

or

ansible-playbook test.yml -e "app=myapp2"

In order to be able to loop on my dict vars myapp1_properties or myapp2_properties.

But, this way I got the following error:

  msg: |-
    template error while templating string: expected token ':', got '}'. String: {% for property, value in {{ app }}_properties.items() %}
    {{ property }}={{ value }}
    {% endfor %}

Is it possible to correctly use a variable in my {% for %} loop?

Expected result:

$ ansible-playbook test.yml -e "app=myapp1"

PLAY [localhost] ***********************************************************************************************************************************************************************

TASK [include_vars] ********************************************************************************************************************************************************************
mardi 30 août 2022  15:43:48 +0200 (0:00:00.020)       0:00:00.020 ************
ok: [localhost]

TASK [debug] ***************************************************************************************************************************************************************************
mardi 30 août 2022  15:43:48 +0200 (0:00:00.028)       0:00:00.048 ************
ok: [localhost] =>
  msg: |-
    key1=value1

$ ansible-playbook test.yml -e "app=myapp2"

PLAY [localhost] ***********************************************************************************************************************************************************************

TASK [include_vars] ********************************************************************************************************************************************************************
mardi 30 août 2022  15:43:40 +0200 (0:00:00.020)       0:00:00.020 ************
ok: [localhost]

TASK [debug] ***************************************************************************************************************************************************************************
mardi 30 août 2022  15:43:40 +0200 (0:00:00.030)       0:00:00.050 ************
ok: [localhost] =>
  msg: |-
    key1=value1
    key2=value2
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Wrest
  • 113
  • 1
  • 11
  • 1
    You are looking for the vars lookup: [`{% for property, value in lookup('vars', app ~ '_properties').items() %}`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/vars_lookup.html) – β.εηοιτ.βε Aug 30 '22 at 13:58
  • 1
    Great duplicate reference as all 4 possible options are given in the 4 different answers – Zeitounator Aug 30 '22 at 14:06

0 Answers0