Calling a filter_plugin from a custom module

53 views
Skip to first unread message

Ananda Debnath

unread,
May 15, 2015, 10:49:53 AM5/15/15
to ansible...@googlegroups.com
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?

Brian Coca

unread,
May 15, 2015, 10:52:21 AM5/15/15
to ansible...@googlegroups.com
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.


--
Brian Coca

Ananda Debnath

unread,
May 15, 2015, 11:05:03 AM5/15/15
to ansible...@googlegroups.com
Thanks Brian.

Is there a way to add custom common functions?

Brian Coca

unread,
May 15, 2015, 11:21:52 AM5/15/15
to ansible...@googlegroups.com
stuff in lib/ansible/module_utils/
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/1557d25e-418d-44f3-b2a1-d3b0e5149bd9%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Ananda Debnath

unread,
May 15, 2015, 3:57:16 PM5/15/15
to ansible...@googlegroups.com
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.

Brian Coca

unread,
May 15, 2015, 3:58:38 PM5/15/15
to ansible...@googlegroups.com
er .. we do:

from ansible.module_utils.basic import *

Ananda Debnath

unread,
May 16, 2015, 8:53:15 PM5/16/15
to ansible...@googlegroups.com
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 *?

Brian Coca

unread,
May 18, 2015, 8:20:44 AM5/18/15
to ansible...@googlegroups.com
On Sat, May 16, 2015 at 8:53 PM, Ananda Debnath
<ananda....@gmail.com> wrote:
> 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



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages