Jinja 2 filter

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

Any thoughts or better solutions?

Jeroen

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. :wink:

        -JP

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

Any thoughts or better solutions?

I like the idea! :slight_smile:

        -JP

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