inheriting from other modules

19 views
Skip to first unread message

János Szigetvári

unread,
Jan 22, 2021, 5:26:45 AM1/22/21
to Ansible Development

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

Andreas Härpfer

unread,
Jan 22, 2021, 5:41:01 AM1/22/21
to Ansible Development
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

SZIGETVÁRI János

unread,
Jan 22, 2021, 8:29:32 AM1/22/21
to Andreas Härpfer, Ansible Development
Hi Andi,

Andreas Härpfer <ahae...@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
--
Janos SZIGETVARI
RHCE, License no. 150-053-692

LinkedIn: linkedin.com/in/janosszigetvari

__@__˚V˚
Make the switch to open (source) applications, protocols, formats now:
- windows -> Linux, iexplore -> Firefox, msoffice -> LibreOffice
- msn -> jabber protocol (Pidgin, Google Talk)
- mp3 -> ogg, wmv -> ogg, jpg -> png, doc/xls/ppt -> odt/ods/odp


Andreas Härpfer

unread,
Jan 22, 2021, 10:23:53 AM1/22/21
to SZIGETVÁRI János, Ansible Development

> On 22. Jan 2021, at 14:28, SZIGETVÁRI János <jszig...@gmail.com> wrote:
>
> 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.


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
command module is a simple and fairly straightforward script [1].
No OOP magic here.

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

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

(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
[2]: https://docs.ansible.com/ansible/latest/dev_guide/developing_module_utilities.html
[3]: https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html

SZIGETVÁRI János

unread,
Jan 22, 2021, 11:20:41 AM1/22/21
to Andreas Härpfer, Ansible Development
Thanks Andi for the advice!
I will try modifying a copy of command.py then.

Have a great day!
János

Brian Coca

unread,
Jan 22, 2021, 11:42:59 AM1/22/21
to Ansible Development
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.

--
----------
Brian Coca

SZIGETVÁRI János

unread,
Feb 1, 2021, 1:31:59 AM2/1/21
to Ansible Development
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

--
Janos SZIGETVARI
RHCE, License no. 150-053-692

LinkedIn: linkedin.com/in/janosszigetvari

__@__˚V˚
Make the switch to open (source) applications, protocols, formats now:
- windows -> Linux, iexplore -> Firefox, msoffice -> LibreOffice
- msn -> jabber protocol (Pidgin, Google Talk)
- mp3 -> ogg, wmv -> ogg, jpg -> png, doc/xls/ppt -> odt/ods/odp

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Development" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-devel/TxquMcC45sM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/CACVha7eATAZTQ5p_Q%2BQA8N9%2BqBDYfhAi8Fjva4KnSx25WRWNRQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages