Ansible Python API: how to transfer files to remote in ansible custom module

Is there anyway to add files into the zip ansible control node send over to remotes? That's the zip Ansiballz generate, which contains the module and any imports from module_utils.

This is for a new style python custom module, and can't use copy module

The question is not about why and how to use copy module, it's about how to copy files to remote using ansible python api without creating a new connection(_transfer_file)

It would help if you gave more information about what you are trying
to accomplish as implementations might vary depending on that.

Hi Brian,
thanks for asking.
So this is a custom python module, It’s for a very domain specific process, I wont go into the exact steps.
But it needs to use bunch of file during the process. the file part is very similar to the copy or template module which is copy the files from control to remotes and the file needs to be updated few times during the process.

I had a look at AnsibleModule api, there is a atomic_move, which is what copy module uses, but after some digging, i realized the atomic_move is only moving from local to local, and for copy module, the src file/s has been zipped up with copy.py and sent over to remote before copy.py is called by ansible on remote. Which make complete sense, the modules are only executing on remotes, the files they use must be made available to them by ansible framework before they are invoked.

This is exactly what i need to do, i need to get the files required to be send over to remote in same zip with my custom_module.py. It must be a very simple config/api, but I just can’t find it.
I did come cross _transfer_file from normal action ActionBase, looks like action is where it happens, but developing an custom actions seem to be an overkill for such a common user case, and i dont want to reinvent wheels. I am hopping there must be a public version of this _transfer_file api, but i couldn’t find it. And think about it again,

I hope that give you a better pic.

And yes, I am using copy module to get files over for now, it’s working, but i consider it as a work around, because it’s exposing a file which is completely internal, and i have to do lots of boil plate on owner/group, permission, encryption, creating temp folder, deleting tmp, etc. Further more, these files should be completely hidden by whoever is using/maintaining the playbook.
And yes, i understand the merits of using copy module or template module, if you think i should be sticking to them, please consider this as a purely technical question, which is how to use ansible python API to send files to remote for a custom module.

zipped up with copy.py and sent over to remote before copy.py is called by ansible on remote. Which make complete sense, the modules are only executing on remotes, the files they use must be made available to them by ansible framework before they are invoked.

This is incorrect, the copy action plugin asks the connection plugin
to push the file to the remote before executing the module (which is
part of the zip as well as any dependencies it has in module_utils).
Both of these transfers should be using the same directory though, we
try to limit the number of temp dirs created/used.

I did come cross _transfer_file from normal action ActionBase, looks like action is where it happens, but developing an custom actions seem to be an overkill for such a common user case, and i dont want to reinvent wheels.

This is the correct method to use, this in turn calls the connection
plugin method for file transfers.
If you don't want to use directly, look at the template action plugin,
it just calls the copy action plugin when it needs to copy the files,
you could do the same in this case.

Hi Brian

Thanks for the tips. So the custom module on it’s own wont be able to transfer files to remotes, a custom action plugin would be required for this.
And it turns out creating a custom action plugin and re-use existing actions/modules in the custom action plugin is pretty straight forward and didn’t require lots of boilerplate.
For anyone that’s looking for code example. see the code here.

Best Regards,
Hangsu Ma