hock
hock

Reputation: 169

Ansible- how I can pass in Jinja2 template all variables from block in playbook

I have simple Jinja2 template

[blockA]
{% include 'blockA.example.j2' %}

and blockA.example.j2 (this is not working)

{% for var in varBlockA %}
{{ var.% }}
{% endfor %}

and playbook have variables like this:

  vars:
    varBlockA:
      varA: value1
      varB: value2

and I searching correct Jinja2 format for blockA.example.j2. I need output like this

[blockA]
varA: value1
varB: value2

but I wan't definie varA,varB in Jinja2 because I wan't to limit myself due to the large number of parameters, the name and value of which I want to define from the playbook level, not the Jinja2 template.

Thanks.

Upvotes: 0

Views: 809

Answers (1)

seshadri_c
seshadri_c

Reputation: 7340

You can use a different way to iterate over the varBlockA dictionary using items() function, so that you can access the key and value for each dictionary item.

blockA.example.j2 for example:

{% for key, value in varBlockA.items() %}
{{ key }}: {{ value }}
{% endfor %}

Upvotes: 2

Related Questions