# Calling a filter\_plugin from a custom module

**URL:** <https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236>\
**Category:** Ansible Project\
**Created:** [May 15, 2015, 2:49pm UTC](https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236 "2015-05-15T14:49:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ananda\_Debnath](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Ananda\_Debnath](https://forum.ansible.com/u/Ananda_Debnath)\
**Post date:** [May 15, 2015, 2:49pm UTC](https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236/1 "2015-05-15T14:49:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 15, 2015, 2:52pm UTC](https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236/2 "2015-05-15T14:52:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ananda\_Debnath](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Ananda\_Debnath](https://forum.ansible.com/u/Ananda_Debnath)\
**Post date:** [May 15, 2015, 3:05pm UTC](https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236/3 "2015-05-15T15:05:03Z")

</div>

Thanks Brian.

Is there a way to add custom common functions?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 15, 2015, 3:21pm UTC](https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236/4 "2015-05-15T15:21:47Z")

</div>

stuff in lib/ansible/module\_utils/

---

<div class="post-metadata">

**Author:** ![Ananda\_Debnath](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Ananda\_Debnath](https://forum.ansible.com/u/Ananda_Debnath)\
**Post date:** [May 15, 2015, 7:57pm UTC](https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236/5 "2015-05-15T19:57:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 15, 2015, 7:58pm UTC](https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236/6 "2015-05-15T19:58:33Z")

</div>

er .. we do:

from ansible.module\_utils.basic import \*

---

<div class="post-metadata">

**Author:** ![Ananda\_Debnath](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Ananda\_Debnath](https://forum.ansible.com/u/Ananda_Debnath)\
**Post date:** [May 17, 2015, 12:53am UTC](https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236/7 "2015-05-17T00:53:15Z")

</div>

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 \***?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 18, 2015, 12:20pm UTC](https://forum.ansible.com/t/calling-a-filter-plugin-from-a-custom-module/18236/8 "2015-05-18T12:20:41Z")

</div>

> 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
