How to incorporate if else statement in Jinja template.

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 ?

See "if"
https://jinja.palletsprojects.com/en/2.11.x/templates/#if

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.

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

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

{{ item }} {{ item }}
{% 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.

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

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

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