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?
Andreas Härpfer <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.
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].
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.
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> ezt írta (időpont: 2021. jan. 22., P, 17:43):