# How to incorporate if else statement in Jinja template.

**URL:** <https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404>\
**Category:** Ansible Project\
**Created:** [March 6, 2020, 9:59am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404 "2020-03-06T09:59:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 9:59am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/1 "2020-03-06T09:59:00Z")

</div>

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 %}

| | {{ item }} |
| --- | --- |

{% 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 ?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 6, 2020, 10:05am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/2 "2020-03-06T10:05:46Z")

</div>

See "if"  
[https://jinja.palletsprojects.com/en/2.11.x/templates/#if](https://jinja.palletsprojects.com/en/2.11.x/templates/#if)

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 10:52am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/3 "2020-03-06T10:52:14Z")

</div>

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........`

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [March 6, 2020, 11:02am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/4 "2020-03-06T11:02:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 11:06am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/5 "2020-03-06T11:06:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 11:09am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/6 "2020-03-06T11:09:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 6, 2020, 11:37am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/7 "2020-03-06T11:37:49Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 6, 2020, 11:42am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/8 "2020-03-06T11:42:50Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 11:48am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/9 "2020-03-06T11:48:19Z")

</div>

@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 %}

| | {{ item }} | {{ item }} |
| --- | --- | --- |

{% endfor %}

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 11:52am UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/10 "2020-03-06T11:52:46Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 6, 2020, 12:04pm UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/11 "2020-03-06T12:04:12Z")

</div>

> @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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 12:12pm UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/12 "2020-03-06T12:12:51Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 6, 2020, 12:24pm UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/13 "2020-03-06T12:24:50Z")

</div>

> @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](https://docs.ansible.com/ansible/latest/plugins/lookup/fileglob.html)

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [March 6, 2020, 12:35pm UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/14 "2020-03-06T12:35:14Z")

</div>

I know how to lookup a file using fileglob in Ansible but not sure how to inside a Jinja template.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 6, 2020, 1:01pm UTC](https://forum.ansible.com/t/how-to-incorporate-if-else-statement-in-jinja-template/32404/15 "2020-03-06T13:01:10Z")

</div>

> 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
