# how to add custom jinja filters

**URL:** <https://forum.ansible.com/t/how-to-add-custom-jinja-filters/16897>\
**Category:** Ansible Project\
**Created:** [November 26, 2014, 4:58pm UTC](https://forum.ansible.com/t/how-to-add-custom-jinja-filters/16897 "2014-11-26T16:58:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ilin](https://avatars.discourse-cdn.com/v4/letter/i/22d042/32.png) [@Ilin](https://forum.ansible.com/u/Ilin)\
**Post date:** [November 26, 2014, 4:58pm UTC](https://forum.ansible.com/t/how-to-add-custom-jinja-filters/16897/1 "2014-11-26T16:58:48Z")

</div>

Hi,

Is there a good example how to extend ansible with custom jinja filters?

I have added this, but my filter is not found  
[http://docs.ansible.com/intro\_configuration.html#filter-plugins](http://docs.ansible.com/intro_configuration.html#filter-plugins)

Adding the function to core.py works, but it just looks wrong

---

<div class="post-metadata">

**Author:** ![azaghal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/azaghal/32/4743_2.png) [@azaghal](https://forum.ansible.com/u/azaghal)\
**Post date:** [November 26, 2014, 10:01pm UTC](https://forum.ansible.com/t/how-to-add-custom-jinja-filters/16897/2 "2014-11-26T22:01:52Z")

</div>

As it turns out, I played around a bit with this just a couple of weeks  
ago, and it's quite simple.

The easiest way is to simply create a directory where your top-level  
playbook is (site.yml?) called filter\_plugins. Within that directory  
you simply put .py files with content similar to (----%---- is just to  
show where file begins/ends, don't copy that :):

----%----  
def uppercase\_all(arg):  
&nbsp;&nbsp;&nbsp;&nbsp;return arg.upper()

class FilterModule(object):  
&nbsp;&nbsp;&nbsp;&nbsp;def filters(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return {'uppercase\_all': uppercase\_all}  
----%----

With the above in place, you can simply use the filter uppercase\_all  
within your playbooks, roles etc. Note that you don't necessarily need  
to have the same name for function and filter, but it's probably better  
to do it that way.

If you look at the page you posted, it will direct you to  
[http://docs.ansible.com/developing\_plugins.html](http://docs.ansible.com/developing_plugins.html) which describes most of  
I wrote (although, in a bit of a less verbose way, and without  
examples).

I hope this helps.

Best regards

---

<div class="post-metadata">

**Author:** ![Ilin](https://avatars.discourse-cdn.com/v4/letter/i/22d042/32.png) [@Ilin](https://forum.ansible.com/u/Ilin)\
**Post date:** [November 27, 2014, 8:34am UTC](https://forum.ansible.com/t/how-to-add-custom-jinja-filters/16897/3 "2014-11-27T08:34:45Z")

</div>

Hi Branko,

Thanks for the reply, I got it working.

What was different in my case is that the folder was named “filters”.  
I renamed it to “filter\_plugins” and it works fine now.

I also noticed that the setting [http://docs.ansible.com/intro\_configuration.html#filter-plugins](http://docs.ansible.com/intro_configuration.html#filter-plugins)

in the config file has no effect. I may open issue on github.

Regards,

ilin

---

<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:** [December 1, 2014, 9:06pm UTC](https://forum.ansible.com/t/how-to-add-custom-jinja-filters/16897/4 "2014-12-01T21:06:55Z")

</div>

If that path is not taking on the 1.8.X series, please do. Thanks!

---

<div class="post-metadata">

**Author:** ![Nayeem\_Syed](https://avatars.discourse-cdn.com/v4/letter/n/a8b319/32.png) [@Nayeem\_Syed](https://forum.ansible.com/u/Nayeem_Syed)\
**Post date:** [April 28, 2017, 4:10pm UTC](https://forum.ansible.com/t/how-to-add-custom-jinja-filters/16897/5 "2017-04-28T16:10:30Z")

</div>

the approach outlined below doesn’t seem to be working, Ansible can’t find existing filters anymore after I add this in?
