Veselin Hristov
Veselin Hristov

Reputation: 147

List in lists in jinja2 template

I have the following variables defined:

datasources:
  - { name: 'dsname', target: ['server1', 'server2'] }

Running the following playbook:

---
- name: Create file
  template: src="datasource.py" dest="/tmp/datasource.py"

The Jinja template i am trying to build from is the following:

{% for ds_target in datasources.target 
%} Target:{{ ds_target }} {%
endfor %}

How can I do the template to have the following output?

Target:server1 Target:server2 

Any tip would be highly appreciated

Upvotes: 2

Views: 4441

Answers (3)

Veselin Hristov
Veselin Hristov

Reputation: 147

i added another loop to go over the list "target", so my code looks something like this:

{% for datasource in datasources 
%} {% for dst in datasource.target %}Target:{{ dst }} {% endfor %}{% endfor %}

Thank you very much guys !

Upvotes: 0

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

If your question is "how should I define my target list to make my template work?", then the answer is:

datasources:
  - name: dsname
    target:
      - server1
      - server2

or in other notation:

datasources:
  - { name: 'dsname', target: ['server1', 'server2'] }

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599590

Surely you need to loop over datasources itself and then access target in each iteration:

{% for ds in datasources %} Target:{{ ds.target }} {% endfor %}

Upvotes: 1

Related Questions