Server Side Module

Hey,

I tried making a win_copy module for ansible,
but misunderstood Mr De Haan a bit.
(on GitHub: https://github.com/ansible/ansible/pull/8241)
I thought with an action plugin he meant to convert it to a .ps1 script that runs from the client,
but after some research I discovered that under ansible/lib/ansible/runner/action_plugins/ are action plugins that run on the server side.
Now my question, is there any place where I can find some documentation on how to write server-side modules?
This because I’ve written some client side modules that solved my problems for now, but this would be a lot more practical to run server-side.

Especially since this would make me able to totally step away from Salt, because untill the implementation, I made a call to salt from ansible to get things done on my windows machines.
But it would be a lot more comfortable to have everything in the same provider.

(Just fyi, I’m not a seasoned python coder, that’s why I couldn’t replicate what I needed from the sources in the action_plugins path and is why I’m searching for some documentation to help me along)

“Now my question, is there any place where I can find some documentation on how to write server-side modules?”

Mostly I’d use the other action_plugins as reference as they aren’t particularly difficult, but they don’t have as much automagic in place to do option parsing as do the remote module stubs and such.

They basically make use of the connection classes and internals of runner (the system by which tasks are executed in ansible, at a lower level than playbooks) to call other modules on the remote end.

A good example (simple) action plugin to look at is the “raw” module:

https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/action_plugins/raw.py

Here it’s using a low level exec function to run something on the remote end, here you would replace this with calls to do the copying around.

The Linux copy is actually a bit more involved and deep, so I’d expect it’s probably better to have a win_copy here.

This module not only uses the common code in the connections to put the file, but allows modifying of file attributes in the same steps as the copy module.

https://github.com/ansible/ansible/blob/devel/lib/ansible/runner/action_plugins/copy.py

If you’re able to, making copy work transparently on Windows/Linux would be more ideal, but I suspect this may not be a good tactic right now - just because there’s a lot of low level things dealing with md5, diff mode, and so on, that may make it hard to insert.

In any event, we don’t need action_plugins for 99% of modules folks wish to write, but in the case of copying a file from A to B for windows, it’s useful, because you don’t have to do “local_action” on the step, it just feels (to the end user) like running any other module.

Let me know if I can help with more specific questions, it may take some tinkering to get used to them, but basically just dropping another plugin in the directory will make it usable.

We also have some integration tests in tests/integration/… that cover the Windows modules, so you could write some basic checks using “win_stat” to make sure the remote file was transferred, etc, and that could help you validate the copy module as you write it.

Super super interested in this, so good to see!

Thank you for the reply, this already got me on the way.
The first commit is in. It’s not up to par with the original copy module yet, for example, directories aren’t implemented yet, but I’m still working on some kinks with the directory (md5) validation. The upside, serving a 5MB binary file is now transfered in a few seconds, which was next to impossible with the WinRM transfer.

Good stuff Timothy! I was wondering about the same thing, could you point me to your fork/pull request?

-Trond