# Python boilerplate in modules

**URL:** <https://forum.ansible.com/t/python-boilerplate-in-modules/15545>\
**Category:** Ansible Developer\
**Created:** [June 25, 2014, 5:24pm UTC](https://forum.ansible.com/t/python-boilerplate-in-modules/15545 "2014-06-25T17:24:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas\_Harpfer](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andreas\_Harpfer](https://forum.ansible.com/u/Andreas_Harpfer)\
**Post date:** [June 25, 2014, 5:24pm UTC](https://forum.ansible.com/t/python-boilerplate-in-modules/15545/1 "2014-06-25T17:24:13Z")

</div>

Hi all,

being new to Ansible development and currently trying to  
understand how some of the core modules work, I was  
wondering why modules don't use the usual good practice  
Python boilerplate a la

&nbsp;&nbsp;&nbsp;&nbsp;from ansible.module\_utils.basic import \*

&nbsp;&nbsp;&nbsp;&nbsp;# main module code

&nbsp;&nbsp;&nbsp;&nbsp;if \_\_name\_\_ == '\_\_main\_\_':  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main()

instead of the current

&nbsp;&nbsp;&nbsp;&nbsp;# main module code  
&nbsp;&nbsp;&nbsp;&nbsp;from ansible.module\_utils.basic import \*  
&nbsp;&nbsp;&nbsp;&nbsp;main()

The former would allow importing Ansible modules into  
other code which might be quite useful in some situations.

Cheers  
-Andi

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 25, 2014, 6:28pm UTC](https://forum.ansible.com/t/python-boilerplate-in-modules/15545/2 "2014-06-25T18:28:11Z")

</div>

It’s because putting things at the bottom don’t affect line numbers in the tracebacks.

Ansible is designed to not ship around a lot of dependencies, so these particular imports are pre-processed, and while your editor can tab-complete them, they are not standard python imports and get replaced before transfer and execution.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 25, 2014, 6:29pm UTC](https://forum.ansible.com/t/python-boilerplate-in-modules/15545/3 "2014-06-25T18:29:12Z")

</div>

Please also note that while the boilerplate is licensed BSD, all ansible modules in core are GPL - so your application needs to be ok with that.

Enabling them for use as a library isn’t a priority really, but this is why module\_utils exists - it enables code sharing between modules.

---

<div class="post-metadata">

**Author:** ![Andreas\_Harpfer](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andreas\_Harpfer](https://forum.ansible.com/u/Andreas_Harpfer)\
**Post date:** [June 25, 2014, 6:58pm UTC](https://forum.ansible.com/t/python-boilerplate-in-modules/15545/4 "2014-06-25T18:58:41Z")

</div>

Michael, thanks for the clarification. I obviously overlooked  
the fact that includes of non-core-Python modules need to be  
expanded in-situ before transfer to the target machine.  
Accordingly, importing one Ansible module into another one does  
not make too much sense, I fear …

No worries regarding BSD vs GPL licensing, though. I'm fine with  
both of them and my question was more out of technical curiousity  
than with a particular application or real world use-case in mind.

Regards  
-Andi
