Calling a filter_plugin from a custom module

Is there a way to call a filter_plugin from within a custom module?

For example, I’ve written a simple filter_plugin utils.py that contains a ‘to_bytes’ function. This filter is used from my play like: “debug: msg=“{{‘25GB’|to_bytes}}””.

I need to call the same function from within one of my custom modules where I’m allocating a virtual disk in a xen server.

Rather than have two copies of the same to_bytes function, I wonder if it’s possible to call this filter_plugin from within my module?

no, filters are not available to modules, but modules do have a bunch
of common functions available, like 'pretty_bytes', probably the
reverse of what you want.

Thanks Brian.

Is there a way to add custom common functions?

stuff in lib/ansible/module_utils/

That would be /usr/lib/python2.6/site-packages/ansible/module_utils/ on my system (CentOS 6.6, Ansible 1.9.1) I’m guessing.

I know folks have discussed/pleaded this before - but I’d like to add my voice to this as well - having an officially sanctioned way to import common code for custom modules.

er .. we do:

from ansible.module_utils.basic import *

Sorry, I don’t follow.

If I have functions that are common to many of my custom libraries, I’ll need to put them in /usr/lib/python2.6/site-packages/ansible/module_utils/mycommon.py and then import them in my libraries with from ansible.module_utils.mycommon import * right?

Are you saying there’s another non-root type location I can stick mycommon.py in and have functions pulled into my module with from ansible.module_utils.mycommon import *?

Sorry, I don't follow.

If I have functions that are common to many of my custom libraries, I'll
need to put them in
/usr/lib/python2.6/site-packages/ansible/module_utils/mycommon.py and then
import them in my libraries with from ansible.module_utils.mycommon import *
right?

yes

Are you saying there's another non-root type location I can stick
mycommon.py in and have functions pulled into my module with from
ansible.module_utils.mycommon import *?

no