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), <[email protected] 
> <mailto:[email protected]>> 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), <[email protected] 
> <mailto:[email protected]>
>     <mailto:[email protected] <mailto:[email protected]>>> wrote:
>     >
>     >     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
>     >
>     >     >
>     >     > 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.
>     >     >
>     >     > On Friday, March 6, 2020 at 5:13:26 PM UTC+5:30, Stefan Hornburg 
> (Racke) wrote:
>     >     >
>     >     >     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 <[email protected] 
> <mailto:[email protected]> <mailto:[email protected]
>     <mailto:[email protected]>> <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
>     >     >     > [email protected] 
> <mailto:[email protected]> <mailto:[email protected]
>     <mailto:[email protected]>> <javascript:>
>     >     <mailto:[email protected] 
> <mailto:ansible-project%[email protected]>
>     <mailto:ansible-project%[email protected] 
> <mailto:ansible-project%[email protected]>>
>     >     <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
>     >     > [email protected] 
> <mailto:ansible-project%[email protected]>
>     <mailto:ansible-project%[email protected] 
> <mailto:ansible-project%[email protected]>>
>     >     <mailto:[email protected] 
> <mailto:ansible-project%[email protected]>
>     <mailto:ansible-project%[email protected] 
> <mailto:ansible-project%[email protected]>>>.
>     >     > 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
>     >     [email protected] 
> <mailto:ansible-project%[email protected]>
>     <mailto:ansible-project%[email protected] 
> <mailto:ansible-project%[email protected]>>.
>     >     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
>     > [email protected] 
> <mailto:ansible-project%[email protected]>
>     <mailto:[email protected] 
> <mailto:ansible-project%[email protected]>>.
>     > 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>.
> 
> 
>     -- 
>     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
>     [email protected] 
> <mailto:ansible-project%[email protected]>.
>     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
> [email protected] 
> <mailto:[email protected]>.
> 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>.


-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/df65d0bd-d0ef-504a-4f92-0c15bdb02047%40linuxia.de.

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to