Here is my variable file
`
layers:
name: APP
things:
cactus
lotus
jasmine
rose
name: WAS
things:
mango
apple
`
Below is my jinja template file:
`
{% for layer in layers %}
{% for item in layer.things %}
{% endfor %}
{% endfor %}
`
My requirement is to put an if else condition where if any file err*txt is present under {{ item }} folder like below
if ls {{ playbook_dir }}/{{ item }}/err*.txt | head -1
returns true / records then the template evaluates to
{{ item }}
else
{{ item }}
How do i put the if /else condition in the jinja template ?
vbotka
(Vladimir Botka)
March 6, 2020, 10:05am
2
Vladimir Hi,
I tried
`
{% if {{ playbook_dir }}/{{ item }}/bad*.txt | exists %}
{{ item }}
{% else %}
{{ item }}
{% endif %}
`
But I get this error:
TASK [Create the Jinja2 based template] ********************************************************************************************************************* fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token ':', got '}'. String: {% for layer in layers %}\n........
Hi,
Your error is about a for loop, but your template included in your email doesn’t have this loop.
Looks like you didn’t give us the good one
Regards,
JYL
If I remove the of statement the error goes away.
I can confirm i’m using the same Jinja template as the output error shared.
Jean hi,
I incorporated the if statement from my previous post in my original Jinja template after the second ‘for loop’ in my original post.
Once the if condition is removed the error does not show anymore.
racke
(Stefan Hornburg)
March 6, 2020, 11:37am
7
Hi,
Your error is about a for loop, but your template included in your email doesn't have this loop.
The loop could be applied to the task, in that case you can use {{ item }} in the template on its own.
Regards
Racke
racke
(Stefan Hornburg)
March 6, 2020, 11:42am
8
Vladimir Hi,
I tried
>
{%if{{playbook_dir }}/{{item }}/bad*.txt |exists %}
Don't use the curly braces inside the {%if ... } condition:
{% if playbook_dir + '/' + item + '/' + 'bad*.txt' | exists %}
Regards
Racke
@Stefan sorry but I could not understand your statement.
Are you asking me to move the if statement out of the jinja template ?
Below is the complete jinja template with the if statement that errors. I’m able to populate all the values correctly and the playbook works fine when the if statement is removed from the jinja template.
{% for layer in layers %}
{% for item in layer.things %}
{% if {{ playbook_dir }}/{{ item }}/bad*.txt | exists %}
{% else %}
{% endif %}
{% endfor %}
{% endfor %}
@Stefan I tried you suggestion of not using the curly braces and it helped resolve the error. But now i get a new error.
{% if playbook_dir + '/' + item + '/' + 'bad*.txt' | exists %}
TASK [Create the Jinja2 based template] *********************************************************************************************************************
fatal: [localhost]: FAILED! => {“changed”: false, “msg”: "AnsibleError: template error while templating string: no filter named ‘exists’.
Looks like the issue is with “exists” filter.
racke
(Stefan Hornburg)
March 6, 2020, 12:04pm
11
@Stefan I tried you suggestion of not using the curly braces and it helped resolve the error. But now i get a new error.
>
{%ifplaybook_dir +'/'+item +'/'+'bad*.txt'|exists %}
>
There is no filter named 'exists'. Please explain what you want to achieve with this template.
Regards
Racke
@Stefan I wish to check if the file bad*.txt exists on the path. Based on the if condition I wish to template different output.
racke
(Stefan Hornburg)
March 6, 2020, 12:24pm
13
@Stefan I wish to check if the file bad*.txt exists on the path. Based on the if condition I wish to template different
<td> output.
I assume that you are checking for file names matching bad*.txt. In that case you can use the fileglob lookup:
https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html
Regards
Racke
I know how to lookup a file using fileglob in Ansible but not sure how to inside a Jinja template.
racke
(Stefan Hornburg)
March 6, 2020, 1:01pm
15
I know how to lookup a file using fileglob in Ansible but not sure how to inside a Jinja template.
Very simple:
{% if lookup('fileglob', '/home/racke/tmp/*.txt') %}
Regards
Racke