Custom Jinja tests?

257 views
Skip to first unread message

Joost Cassee

unread,
May 25, 2014, 5:28:19 PM5/25/14
to ansible...@googlegroups.com
Hi,

Custom template filters are a great way to take the programming out of my playbooks. Sometimes I would like to have a Jinja test, though.

For example, I have a filter 'is_in_zone(domain, zone)' to see if a domain ends with a certain tail. For example, 'test.example.com' is in zone 'example.com' (and in 'com').

{{ domain | is_in_zone(zone) }} would look nicer as {{ domain is in_zone(zone) }}, and having it as a test would also make it possible to use it in 'select' filters.

Is it possible to create custom Jinja tests? If not, where in the code should I look if I want to take a stab at adding that feature?

Regards,
Joost

Michael DeHaan

unread,
May 25, 2014, 5:32:40 PM5/25/14
to ansible...@googlegroups.com
What you are describing is a Jinja2 filter.

Read about filter plugins here and search the source tree for "core.py" for plenty of examples. 





--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c4944cfc-a3f2-4a6c-a304-409ef072d5c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
May 25, 2014, 5:33:18 PM5/25/14
to ansible...@googlegroups.com
Also this is also a topic for ansible-devel :)

It's international join-ansible-devel advocacy day!

Matt Martz

unread,
May 25, 2014, 5:50:37 PM5/25/14
to ansible...@googlegroups.com
Additionally you could achieve this using the regex "|search" filter or even the python string method .endswith()
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgzifdahBehjoPCDtJjiiyn1s6waCJJgd5JNa-T1c3QfcQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
ma...@sivel.net
http://sivel.net/

Joost Cassee

unread,
May 26, 2014, 5:06:44 AM5/26/14
to ansible...@googlegroups.com
Moving to ansible-devel. :-)
> You received this message because you are subscribed to a topic in the
> Google Groups "Ansible Project" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/ansible-project/jJcSMkBdFXQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgwC7mnJBt5%3DTg9DwngMzUQxyT8Qi2ESS1Hvp4f5hV2sRg%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Joost Cassee
http://joost.cassee.net
Reply all
Reply to author
Forward
0 new messages