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

**URL:** <https://forum.ansible.com/t/ansible-python-api-how-to-transfer-files-to-remote-in-ansible-custom-module/28703>\
**Category:** Ansible Developer\
**Created:** [October 2, 2018, 3:33pm UTC](https://forum.ansible.com/t/ansible-python-api-how-to-transfer-files-to-remote-in-ansible-custom-module/28703 "2018-10-02T15:33:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hangsu](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@hangsu](https://forum.ansible.com/u/hangsu)\
**Post date:** [October 2, 2018, 3:33pm UTC](https://forum.ansible.com/t/ansible-python-api-how-to-transfer-files-to-remote-in-ansible-custom-module/28703/1 "2018-10-02T15:33:56Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 2, 2018, 5:48pm UTC](https://forum.ansible.com/t/ansible-python-api-how-to-transfer-files-to-remote-in-ansible-custom-module/28703/2 "2018-10-02T17:48:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hangsu](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@hangsu](https://forum.ansible.com/u/hangsu)\
**Post date:** [October 2, 2018, 9:16pm UTC](https://forum.ansible.com/t/ansible-python-api-how-to-transfer-files-to-remote-in-ansible-custom-module/28703/3 "2018-10-02T21:16:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 3, 2018, 6:41pm UTC](https://forum.ansible.com/t/ansible-python-api-how-to-transfer-files-to-remote-in-ansible-custom-module/28703/4 "2018-10-03T18:41:21Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![hangsu](https://avatars.discourse-cdn.com/v4/letter/h/d2c977/32.png) [@hangsu](https://forum.ansible.com/u/hangsu)\
**Post date:** [October 10, 2018, 11:08pm UTC](https://forum.ansible.com/t/ansible-python-api-how-to-transfer-files-to-remote-in-ansible-custom-module/28703/5 "2018-10-10T23:08:53Z")

</div>

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](https://github.com/hangsu-ma/ansible-template/blob/master/action_plugins/hello-world.py).

Best Regards,  
Hangsu Ma
