# inheriting from other modules

**URL:** <https://forum.ansible.com/t/inheriting-from-other-modules/34491>\
**Category:** Ansible Developer\
**Created:** [January 22, 2021, 10:26am UTC](https://forum.ansible.com/t/inheriting-from-other-modules/34491 "2021-01-22T10:26:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jszigetvari](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jszigetvari/32/1996_2.png) [@jszigetvari](https://forum.ansible.com/u/jszigetvari)\
**Post date:** [January 22, 2021, 10:26am UTC](https://forum.ansible.com/t/inheriting-from-other-modules/34491/1 "2021-01-22T10:26:45Z")

</div>

Hi,

In a POC project I would need to create a new Ansible module that would need to work just like the command module, with the only difference being that it would need to prepend a command to the command line passed to it.  
How would I need to get started with this? If we were talking in OOP terms, all I would need to do would be to inherit from the parent module/class, and just overload the methods that I need working slightly differently.  
Now how would that work in Ansible? I would think that making a copy of the whole command module would not be the best option out there.  
Could someone please give me a few hints or recommendations?

Thank you!

Best Regards,  
János Szigetvári

---

<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:** [January 22, 2021, 10:40am UTC](https://forum.ansible.com/t/inheriting-from-other-modules/34491/2 "2021-01-22T10:40:54Z")

</div>

Not sure if this is something that works for you, but couldn't you  
simply prepend the command inside your playbook and use the command  
module as is?

-Andi

---

<div class="post-metadata">

**Author:** ![jszigetvari](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jszigetvari/32/1996_2.png) [@jszigetvari](https://forum.ansible.com/u/jszigetvari)\
**Post date:** [January 22, 2021, 1:28pm UTC](https://forum.ansible.com/t/inheriting-from-other-modules/34491/3 "2021-01-22T13:28:53Z")

</div>

Hi Andi,

Andreas Härpfer \<[ahaerpfer@gmail.com](mailto:ahaerpfer@gmail.com)\> ezt írta (időpont: 2021. jan. 22., P, 11:41):

> Not sure if this is something that works for you, but couldn’t you  
> simply prepend the command inside your playbook and use the command  
> module as is?

Technically you are right, it would be possible to always prepend the extra command, and this being a POC is also be an argument in support of this option. (If I end up not finding an easy way of doing this, then I may end up doing like you suggested.)

However I am interested how this could be done, and it would also be useful to have a different module name to distinguish which command is running where.

Thanks!

Best Regards,  
János

---

<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:** [January 22, 2021, 3:23pm UTC](https://forum.ansible.com/t/inheriting-from-other-modules/34491/4 "2021-01-22T15:23:49Z")

</div>

I see your point. I have never developed a module myself, however,  
I see two issues with your approach:

\* There is no class to inherit from and just overwrite a method. The  
&nbsp;&nbsp;command module is a simple and fairly straightforward script [1].  
&nbsp;&nbsp;No OOP magic here.

\* More importantly, code for a module has to be essentially self-contained  
&nbsp;&nbsp;because it will be copied to the remote host and executed there!  
&nbsp;&nbsp;You can not make a lot of assumptions about what will be available  
&nbsp;&nbsp;there.

&nbsp;&nbsp;AFAIK you can only use stuff from the module utilities [2] which will  
&nbsp;&nbsp;then be packaged together with the module code itself and shipped to  
&nbsp;&nbsp;the remote host. And the Python standard lib, of course. You can  
&nbsp;&nbsp;\*not\* access code from other modules!

&nbsp;&nbsp;(Somebody please correct me if the above is not exact!)

So your best bet is probably to make a copy of command.py and tweak it  
to satisfy your needs [3].

Good luck  
-Andi

[1]: [https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/command.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/command.py)  
[2]: [https://docs.ansible.com/ansible/latest/dev\_guide/developing\_module\_utilities.html](https://docs.ansible.com/ansible/latest/dev_guide/developing_module_utilities.html)  
[3]: [https://docs.ansible.com/ansible/latest/dev\_guide/developing\_locally.html](https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html)

---

<div class="post-metadata">

**Author:** ![jszigetvari](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jszigetvari/32/1996_2.png) [@jszigetvari](https://forum.ansible.com/u/jszigetvari)\
**Post date:** [January 22, 2021, 4:20pm UTC](https://forum.ansible.com/t/inheriting-from-other-modules/34491/5 "2021-01-22T16:20:26Z")

</div>

Thanks Andi for the advice!  
I will try modifying a copy of command.py then.

Have a great day!  
János

Andreas Härpfer \<[ahaerpfer@gmail.com](mailto:ahaerpfer@gmail.com)\> ezt írta (időpont: 2021. jan. 22., P 16:23):

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 22, 2021, 4:42pm UTC](https://forum.ansible.com/t/inheriting-from-other-modules/34491/6 "2021-01-22T16:42:28Z")

</div>

Just to confirm, what Andreas describes is correct, any 'shared' code  
must be in module\_utils, modules cannot rely on other module's code as  
ONLY the current module code (+ module\_utils references) is shipped to  
the remote.

---

<div class="post-metadata">

**Author:** ![jszigetvari](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jszigetvari/32/1996_2.png) [@jszigetvari](https://forum.ansible.com/u/jszigetvari)\
**Post date:** [February 1, 2021, 6:31am UTC](https://forum.ansible.com/t/inheriting-from-other-modules/34491/7 "2021-02-01T06:31:20Z")

</div>

Hi Andi, Brian,

I did the minimal modifications, that were required, and set up the environment variables necessary to use the new modules, but now I am getting the following error message:

ERROR! this task ‘core\_command’ has extra params, which is only allowed in the following modules: …

How could I overcome this problem?

Thanks!

Best Regards,  
János

Brian Coca \<[bcoca@redhat.com](mailto:bcoca@redhat.com)\> ezt írta (időpont: 2021. jan. 22., P, 17:43):
