# Jinja 2 filter

**URL:** <https://forum.ansible.com/t/jinja-2-filter/12046>\
**Category:** Ansible Project\
**Created:** [October 29, 2012, 4:08pm UTC](https://forum.ansible.com/t/jinja-2-filter/12046 "2012-10-29T16:08:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeroen\_Hoekx](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jeroen\_Hoekx](https://forum.ansible.com/u/Jeroen_Hoekx)\
**Post date:** [October 29, 2012, 4:08pm UTC](https://forum.ansible.com/t/jinja-2-filter/12046/1 "2012-10-29T16:08:41Z")

</div>

Hi,

I think I need custom jinja 2 filters.

My goal is to create a reverse DNS bind configuration. I know the IP  
address of my systems. Now I want only the part after the last dot.

I can write an action plugin that reads out the ip address, and stores  
what I need in the inventory again. But that feels like cheating.

Adding filter\_plugins is not that hard, but the syntax of the plugin  
is. The information we need to know is the name of the filter and the  
function to call. So either we follow the other plugins and have a  
FilterModule class inside it with a filters() method that returns this  
information, or we have plain functions in a file, with a function  
filters().

What I wrote (and works right now) is the FilterModule variant. See  
[https://github.com/jhoekx/ansible/tree/filter-plugins](https://github.com/jhoekx/ansible/tree/filter-plugins)

Any thoughts or better solutions?

Jeroen

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [October 29, 2012, 5:03pm UTC](https://forum.ansible.com/t/jinja-2-filter/12046/2 "2012-10-29T17:03:10Z")

</div>

> My goal is to create a reverse DNS bind configuration. I know the IP  
> address of my systems. Now I want only the part after the last dot.

Have you thought about using dynamic DNS updates (RFC 2136) to update  
your reverse DNS directly from the node (assuming you're using a name  
server that supports that)? This doesn't answer your question re filters  
of course, but it would probably solve your problem. 😉

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [October 29, 2012, 5:08pm UTC](https://forum.ansible.com/t/jinja-2-filter/12046/3 "2012-10-29T17:08:04Z")

</div>

> What I wrote (and works right now) is the FilterModule variant. See  
> [https://github.com/jhoekx/ansible/tree/filter-plugins](https://github.com/jhoekx/ansible/tree/filter-plugins)
> 
> Any thoughts or better solutions?

I like the idea! 🙂

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 1, 2012, 7:49pm UTC](https://forum.ansible.com/t/jinja-2-filter/12046/4 "2012-11-01T19:49:59Z")

</div>

Ok,

I would be fine with to\_json and from\_json being filter plugins.

I do not want users to have to use any special syntax to load filter plugins, all plugins should be available to all templates.

A filter plugin should return a hash of names to functions, and the patch to implement it _must_ include to\_json and from\_json as filter plugins for this to be merged in. (I believe there’s also a from/to\_yaml, so ditto for those).

I’ve seen a proposal for a “with\_filters”, which I don’t approve of, you should not have to manually load a filter plugin or even know it exists to use it.

–Michael
