I have a Jinja2 template and I want to pass args to it from my vars/main.yml
.
For each set of vars, I need to generate a separate file on the remote server.
My vars/main.yml
has the following structure:
List:
- { src: [
classPath1: xxx,
classPath2: xxx,
contxtHost: xxx,
logDir: xxx,
contxtRegion: xxx,
....
],
dest: xxxx
}
- { src: [
xxxx
],
dest: xxxx
}
in my playbook task is defined as below:
- name: testing templates
template: "src=templates/sampletest.j2
dest=/path/in/Server/{{ item.dest }}
owner=app
group=app
mode=0644"
with_items: '{{ List }}'
How do I pass item.src
to my template?
Note: I am trying to generate multiple files based on each set of vars item.src
and file name is item.dest
using with_items
.
Best Answer
Just reference them in the
templates/sampletest.j2
, for example:For each item on the
List
the template will be parsed and saved to a file defined indest
.Now, the problem is that your
vars/main.yml
file definessrc
as a list (square brackets) of dictionaries, each containing a single key-value pair (with the key named differently in each item on the list), so:src[1]
)classPath2
for and only forsrc[1]
).