# Custom Jinja tests?

**URL:** <https://forum.ansible.com/t/custom-jinja-tests/15328>\
**Category:** Ansible Project\
**Created:** [May 25, 2014, 9:28pm UTC](https://forum.ansible.com/t/custom-jinja-tests/15328 "2014-05-25T21:28:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [May 25, 2014, 9:28pm UTC](https://forum.ansible.com/t/custom-jinja-tests/15328/1 "2014-05-25T21:28:19Z")

</div>

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](http://test.example.com)’ is in zone ‘[example.com](http://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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 25, 2014, 9:32pm UTC](https://forum.ansible.com/t/custom-jinja-tests/15328/2 "2014-05-25T21:32:36Z")

</div>

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.

[http://docs.ansible.com/developing\_plugins.html](http://docs.ansible.com/developing_plugins.html)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 25, 2014, 9:33pm UTC](https://forum.ansible.com/t/custom-jinja-tests/15328/3 "2014-05-25T21:33:16Z")

</div>

Also this is also a topic for ansible-devel 🙂

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

[https://groups.google.com/forum/#!forum/ansible-devel](https://groups.google.com/forum/#!forum/ansible-devel)

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 25, 2014, 9:50pm UTC](https://forum.ansible.com/t/custom-jinja-tests/15328/4 "2014-05-25T21:50:34Z")

</div>

Additionally you could achieve this using the regex “|search” filter or even the python string method .endswith()

---

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [May 26, 2014, 9:06am UTC](https://forum.ansible.com/t/custom-jinja-tests/15328/5 "2014-05-26T09:06:00Z")

</div>

Moving to ansible-devel. 🙂

---

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [May 26, 2014, 9:25am UTC](https://forum.ansible.com/t/custom-jinja-tests/15328/6 "2014-05-26T09:25:04Z")

</div>

[Moved from ansible-project.]

> Additionally you could achieve this using the regex "|search" filter or even  
> the python string method .endswith()

Thanks for that, indeed. That fixes my use-case.

Do you think tests are never needed?

> What you are describing is a Jinja2 filter.

Not really. Filters cannot be used in 'is' expressions or 'select'  
filters. (I do realize that the distinction is small, and many times a  
filter will do very nicely.)

Let me demonstrate with the 'search' filter. The first of the tasks in  
this playbook succeeds while the second one fails:

- hosts: all  
&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- debug: msg=filter  
&nbsp;&nbsp;&nbsp;&nbsp;when: "'foo.example.com' | search('\\.example\\.com$')"

&nbsp;&nbsp;- debug: msg=test  
&nbsp;&nbsp;&nbsp;&nbsp;when: "'foo.example.com' is search('\\.example\\.com$')"

Granted, Jinja tests are basically syntactic sugar for filters, but  
they are nice sometimes.

Is there interest in supporting custom tests?

Regards,  
Joost

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 27, 2014, 8:08pm UTC](https://forum.ansible.com/t/custom-jinja-tests/15328/7 "2014-05-27T20:08:27Z")

</div>

Ok, so they are filters.

You’d still write a filter plugin, you would just either return the value directly passed in or raise an exception.

Which is I’m guessing exactly what the filters do.
