How to incorporate if else statement in Jinja template.

478 views
Skip to first unread message

Shifa Shaikh

unread,
Mar 6, 2020, 4:59:00 AM3/6/20
to Ansible Project
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 %}
<table>
<tr>
<th> </th>
{% for item in layer.things %}

<th align="center">{{ item }}</
th>

{% endfor %}
</tr>
</
table>
{% 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
<th align="center">{{ item }}</th>
else
<th align="left">{{ item }}</th>

How do i  put the if /else condition in the jinja template ?

Vladimir Botka

unread,
Mar 6, 2020, 5:06:04 AM3/6/20
to Shifa Shaikh, ansible...@googlegroups.com
On Fri, 6 Mar 2020 01:59:00 -0800 (PST)
Shifa Shaikh <shif...@gmail.com> wrote:

> How do i put the if /else condition in the jinja template ?

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

Shifa Shaikh

unread,
Mar 6, 2020, 5:52:15 AM3/6/20
to Ansible Project
Vladimir Hi, 

I tried 

{% if {{ playbook_dir }}/{{ item }}/bad*.txt | exists %}



<th align="center">{{ item }}</th>



{% else %}



<th align="left">{{ item }}</th>



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

Jean-Yves LENHOF

unread,
Mar 6, 2020, 6:03:39 AM3/6/20
to ansible...@googlegroups.com

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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com.

Shifa Shaikh

unread,
Mar 6, 2020, 6:07:15 AM3/6/20
to ansible...@googlegroups.com
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.

Shifa Shaikh

unread,
Mar 6, 2020, 6:09:56 AM3/6/20
to ansible...@googlegroups.com
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.

On Fri, 6 Mar, 2020, 4:33 pm Jean-Yves LENHOF, <jean...@lenhof.eu.org> wrote:

Stefan Hornburg (Racke)

unread,
Mar 6, 2020, 6:38:37 AM3/6/20
to ansible...@googlegroups.com
On 3/6/20 12:02 PM, Jean-Yves LENHOF wrote:
> 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

>
> Looks like you didn't give us the good one
>
> Regards,
>
> JYL
>
>
> Le 06/03/2020 à 11:52, Shifa Shaikh a écrit :
>> Vladimir Hi, 
>>
>> I tried 
>>
>> |
>> {%if{{playbook_dir }}/{{item }}/bad*.txt |exists %}
>>
>>
>> <th align="center">{{item }}</th>
>>
>>
>>
>> {%else%}
>>
>>
>> <th align="left">{{item }}</th>
>>
>>
>>
>> {%endif %}
>>
>> |
>>
>> But I get this error:
>>
>> |
>> TASK [Createthe Jinja2based
>> template]*********************************************************************************************************************
>> fatal:[localhost]:FAILED!=>{"changed":false,"msg":"AnsibleError: template error while templating string: expected
>> token ':', got '}'. String: {% for layer in layers %}\n........
>> |
>>
>>
>>
>>
>> On Friday, March 6, 2020 at 3:36:04 PM UTC+5:30, Vladimir Botka wrote:
>>
>> On Fri, 6 Mar 2020 01:59:00 -0800 (PST)
>> Shifa Shaikh <shif...@gmail.com <javascript:>> wrote:
>>
>> > How do i  put the if /else condition in the jinja template ?
>>
>> See "if"
>> https://jinja.palletsprojects.com/en/2.11.x/templates/#if <https://jinja.palletsprojects.com/en/2.11.x/templates/#if>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to
>> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
>> <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/44bc6389-53a2-31d9-62a4-25897191e902%40lenhof.eu.org
> <https://groups.google.com/d/msgid/ansible-project/44bc6389-53a2-31d9-62a4-25897191e902%40lenhof.eu.org?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Stefan Hornburg (Racke)

unread,
Mar 6, 2020, 6:43:26 AM3/6/20
to ansible...@googlegroups.com
On 3/6/20 11:52 AM, Shifa Shaikh wrote:
> 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

>
> <th align="center">{{item }}</th>
>
>
>
> {%else%}
>
>
> <th align="left">{{item }}</th>
>
>
>
> {%endif %}
>
> |
>
> But I get this error:
>
> |
> TASK [Createthe Jinja2based
> template]*********************************************************************************************************************
> fatal:[localhost]:FAILED!=>{"changed":false,"msg":"AnsibleError: template error while templating string: expected token
> ':', got '}'. String: {% for layer in layers %}\n........
> |
>
>
>
>
> On Friday, March 6, 2020 at 3:36:04 PM UTC+5:30, Vladimir Botka wrote:
>
> On Fri, 6 Mar 2020 01:59:00 -0800 (PST)
> Shifa Shaikh <shif...@gmail.com <javascript:>> wrote:
>
> > How do i  put the if /else condition in the jinja template ?
>
> See "if"
signature.asc

Shifa Shaikh

unread,
Mar 6, 2020, 6:48:19 AM3/6/20
to Ansible Project
@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 %}
<table>
<tr>
<th> </th>
{% for item in layer.things %}

{% if {{ playbook_dir }}/{{ item }}/bad*.txt | exists %}
<th align="center">{{ item }}</th>

{% else %}
<th align="left">{{ item }}</th>

{% endif %}

{% endfor %}
</tr>
</
table>
{% endfor %}

>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com
>> <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to

Shifa Shaikh

unread,
Mar 6, 2020, 6:52:47 AM3/6/20
to Ansible Project
@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 Hornburg (Racke)

unread,
Mar 6, 2020, 7:04:49 AM3/6/20
to ansible...@googlegroups.com
On 3/6/20 12:52 PM, Shifa Shaikh wrote:
> @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
> > ansible...@googlegroups.com <javascript:> <mailto:ansible-proje...@googlegroups.com <javascript:>>.
> <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/d22c2c58-3d3c-4cb6-8119-662a1a2e52ea%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/d22c2c58-3d3c-4cb6-8119-662a1a2e52ea%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc

Shifa Shaikh

unread,
Mar 6, 2020, 7:13:22 AM3/6/20
to ansible...@googlegroups.com
@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.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a7f0c40f-a98e-58e2-c659-c10b283bddf0%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Mar 6, 2020, 7:25:26 AM3/6/20
to ansible...@googlegroups.com
On 3/6/20 1:12 PM, Shifa Shaikh wrote:
> @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
> >     >     Shifa Shaikh <shif...@gmail.com <mailto:shif...@gmail.com> <javascript:>> wrote:
> >     >
> >     >     > How do i  put the if /else condition in the jinja template ?
> >     >
> >     >     See "if"
> >     >     https://jinja.palletsprojects.com/en/2.11.x/templates/#if
> >     <https://jinja.palletsprojects.com/en/2.11.x/templates/#if>
> >     <https://jinja.palletsprojects.com/en/2.11.x/templates/#if
> <https://jinja.palletsprojects.com/en/2.11.x/templates/#if>>
> >     >
> >     >
> >     > --
> >     > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     > To unsubscribe from this group and stop receiving emails from it, send an email to
> >     > ansible...@googlegroups.com <mailto:ansible...@googlegroups.com> <javascript:>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <javascript:>>.
> >     > To view this discussion on the web visit
> >     > https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com
> >     <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com>
> >     >
> >   
>  <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com?utm_medium=email&utm_source=footer
> >   
>  <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com?utm_medium=email&utm_source=footer>>.
> >
> >
> >
> >     --
> >     Ecommerce and Linux consulting + Perl and web application programming.
> >     Debian and Sympa administration. Provisioning with Ansible.
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>>.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/ansible-project/d22c2c58-3d3c-4cb6-8119-662a1a2e52ea%40googlegroups.com
> >
> <https://groups.google.com/d/msgid/ansible-project/d22c2c58-3d3c-4cb6-8119-662a1a2e52ea%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/a7f0c40f-a98e-58e2-c659-c10b283bddf0%40linuxia.de.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAJtox0mWObSSL%2Bc%2BYHd7DDsaMPpd%3Dsm14br6jLFLbKSm%3D%3DEjCA%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAJtox0mWObSSL%2Bc%2BYHd7DDsaMPpd%3Dsm14br6jLFLbKSm%3D%3DEjCA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc

Shifa Shaikh

unread,
Mar 6, 2020, 7:35:53 AM3/6/20
to ansible...@googlegroups.com
I know how to lookup a file using fileglob in Ansible but not sure how to inside a Jinja template.

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e4ad4ca8-36e5-902d-ebed-c46cff77b0e3%40linuxia.de.

Stefan Hornburg (Racke)

unread,
Mar 6, 2020, 8:02:11 AM3/6/20
to ansible...@googlegroups.com
On 3/6/20 1:35 PM, Shifa Shaikh wrote:
> 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

> On Fri, 6 Mar, 2020, 5:55 pm Stefan Hornburg (Racke), <ra...@linuxia.de <mailto:ra...@linuxia.de>> wrote:
>
> On 3/6/20 1:12 PM, Shifa Shaikh wrote:
> > @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
>
> >
> > On Fri, 6 Mar, 2020, 5:34 pm Stefan Hornburg (Racke), <ra...@linuxia.de <mailto:ra...@linuxia.de>
> >     >     >     Shifa Shaikh <shif...@gmail.com <mailto:shif...@gmail.com> <mailto:shif...@gmail.com
> <mailto:shif...@gmail.com>> <javascript:>> wrote:
> >     >     >
> >     >     >     > How do i  put the if /else condition in the jinja template ?
> >     >     >
> >     >     >     See "if"
> >     >     >     https://jinja.palletsprojects.com/en/2.11.x/templates/#if
> >     >     <https://jinja.palletsprojects.com/en/2.11.x/templates/#if>
> >     >     <https://jinja.palletsprojects.com/en/2.11.x/templates/#if
> >     <https://jinja.palletsprojects.com/en/2.11.x/templates/#if>>
> >     >     >
> >     >     >
> >     >     > --
> >     >     > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     >     > To unsubscribe from this group and stop receiving emails from it, send an email to
> >     >     > ansible...@googlegroups.com <mailto:ansible...@googlegroups.com> <mailto:ansible...@googlegroups.com
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>
> >     <javascript:>>.
> >     >     > To view this discussion on the web visit
> >     >     > https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com
> >     >     <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com>
> >     >     >
> >     >   
> >   
>   <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com?utm_medium=email&utm_source=footer
> >     >   
> >   
>   <https://groups.google.com/d/msgid/ansible-project/e2c31593-0389-4c5d-9bbb-9f29e528a832%40googlegroups.com?utm_medium=email&utm_source=footer>>.
> >     >
> >     >
> >     >
> >     >     --
> >     >     Ecommerce and Linux consulting + Perl and web application programming.
> >     >     Debian and Sympa administration. Provisioning with Ansible.
> >     >
> >     > --
> >     > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     > To unsubscribe from this group and stop receiving emails from it, send an email to
> >     > ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>
> >     <mailto:ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>>.
> >     > To view this discussion on the web visit
> >     > https://groups.google.com/d/msgid/ansible-project/d22c2c58-3d3c-4cb6-8119-662a1a2e52ea%40googlegroups.com
> >     >
> >   
>  <https://groups.google.com/d/msgid/ansible-project/d22c2c58-3d3c-4cb6-8119-662a1a2e52ea%40googlegroups.com?utm_medium=email&utm_source=footer>.
> >
> >
> >     --
> >     Ecommerce and Linux consulting + Perl and web application programming.
> >     Debian and Sympa administration. Provisioning with Ansible.
> >
> >     --
> >     You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> >     To unsubscribe from this group and stop receiving emails from it, send an email to
> >     ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>
> <mailto:ansible-project%2Bunsu...@googlegroups.com <mailto:ansible-project%252Buns...@googlegroups.com>>.
> >     To view this discussion on the web visit
> >     https://groups.google.com/d/msgid/ansible-project/a7f0c40f-a98e-58e2-c659-c10b283bddf0%40linuxia.de.
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to
> https://groups.google.com/d/msgid/ansible-project/CAJtox0mWObSSL%2Bc%2BYHd7DDsaMPpd%3Dsm14br6jLFLbKSm%3D%3DEjCA%40mail.gmail.com
> >
> <https://groups.google.com/d/msgid/ansible-project/CAJtox0mWObSSL%2Bc%2BYHd7DDsaMPpd%3Dsm14br6jLFLbKSm%3D%3DEjCA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> --
> Ecommerce and Linux consulting + Perl and web application programming.
> Debian and Sympa administration. Provisioning with Ansible.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-project%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/e4ad4ca8-36e5-902d-ebed-c46cff77b0e3%40linuxia.de.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAJtox0nzH2QZOmOUiHh-zQT0UifaE02HRs6u_P6dNS1Go%2BMOaQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAJtox0nzH2QZOmOUiHh-zQT0UifaE02HRs6u_P6dNS1Go%2BMOaQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc
Reply all
Reply to author
Forward
0 new messages