Custom modules that can run on both Linux and Windows

Hello,

I’ve created two modules-- one in Python and one in PowerShell for each of my remote machines. Of course, as expected, the Python one can run only on the remote Linux and the PowerShell-written module can only be used on the remote Windows system.

I understand that Python modules can’t be run on Windows because winRM will only accept PowerShell modules. However, I managed to connect my remote Windows server to my Linux control client via SSH. Ansible picks it up perfectly and I haven’t run into any issues despite it being an experimental feature. This still causes me errors and the python module doesn’t run the same way it does on Linux. Why? Is there anything else I could try so that my custom Python module can be run on both OS’s? I know that there are some modules from ansible-core that can be run on both systems-- how can I do that with my custom modules?

If running a custom python module on windows is a big no-no, how can I get my custom PowerShell module to run on Linux? I know that modules can be written in any language, but why can’t Linux take in the PowerShell module? Are there some changes or settings that I can set to make to instruct ansible to use PowerShell in Linux? I also have PowerShell downloaded on that remote linux machine.

Any suggestions are greatly appreciated! This will save me a lot of time just creating one module instead of two for my two remote systems.

Thanks!

Theoretically this is all possible but there are a few things that are impediments to getting this all working.

  • Python modules can technically run on Windows but our general module_utils (basic.py) use libraries that are
  • PowerShell modules can technically run on non-Windows but a lot of the mechanisms to get it running assume the target is Windows

This essentially means to use either one on both platforms you will have to give up on using the shared module_utils which makes managing options and emitting the output a lot simpler than doing it manually.

I know there have been some unofficial attempts at getting PowerShell working on non-Windows with the module_utils but it uses some internal mechanisms so it isn’t guaranteed to work across newer Ansible versions. In saying that you can write cross platform modules for both platforms, you just can’t use any of the shared module_utils available https://github.com/ansible/ansible/issues/48881#issuecomment-440481672.

Thanks

Jordan

I have managed to get the same functions that windows uses for powershell on ansible to work on linux. In the exit-json function instead of return I changed to write-output, viola it works.

https://www.powershellgallery.com/packages/PSAnsibleHelperFunctions4Linux/0.0.1