# How to Copy Files between two Remote Servers using Ansible

**URL:** <https://forum.ansible.com/t/how-to-copy-files-between-two-remote-servers-using-ansible/28007>\
**Category:** Ansible Developer\
**Tags:** ubuntu\
**Created:** [July 10, 2018, 3:15pm UTC](https://forum.ansible.com/t/how-to-copy-files-between-two-remote-servers-using-ansible/28007 "2018-07-10T15:15:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhinav.grntla](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@abhinav.grntla](https://forum.ansible.com/u/abhinav.grntla)\
**Post date:** [July 10, 2018, 3:15pm UTC](https://forum.ansible.com/t/how-to-copy-files-between-two-remote-servers-using-ansible/28007/1 "2018-07-10T15:15:57Z")

</div>

I am trying to copy the files from Server A to Server B with Ansible running in Server C.

I have tried using Ansible Synchronize Module:

Exchanged SSH id\_rsa.pub keys between the servers. Was able to ssh between the servers without asking password.

When running playbook it was able to gather\_facts but throwing error during task execution. If seen in verbose output it is trying yo connect to different ip’s rather than the ips mentioned in the host file.

Hosts file

`  
[ServerA] 192.168.1.7 ansible\_user=ubuntu  
[ServerB] 192.168.1.2 ansible\_user=ubuntu

`

main.yml

`

---

<div class="post-metadata">

**Author:** ![Shubham\_Gampawar](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Shubham\_Gampawar](https://forum.ansible.com/u/Shubham_Gampawar)\
**Post date:** [July 10, 2018, 3:23pm UTC](https://forum.ansible.com/t/how-to-copy-files-between-two-remote-servers-using-ansible/28007/2 "2018-07-10T15:23:31Z")

</div>

Can you please try to run playbook with the increased timeout value.

---

<div class="post-metadata">

**Author:** ![abhinav.grntla](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@abhinav.grntla](https://forum.ansible.com/u/abhinav.grntla)\
**Post date:** [July 10, 2018, 3:30pm UTC](https://forum.ansible.com/t/how-to-copy-files-between-two-remote-servers-using-ansible/28007/3 "2018-07-10T15:30:02Z")

</div>

Hi Shubham

If you can observe the verbose output rather than connecting to 192.168.1.2 it is trying to connect to 198.105.254.228 , 198.105.244.228. These ip’s are not mentioned in the host file nor the server ip address. Why is it connecting to those addresses.

---

<div class="post-metadata">

**Author:** ![Shubham\_Gampawar](https://avatars.discourse-cdn.com/v4/letter/s/67e7ee/32.png) [@Shubham\_Gampawar](https://forum.ansible.com/u/Shubham_Gampawar)\
**Post date:** [July 10, 2018, 3:41pm UTC](https://forum.ansible.com/t/how-to-copy-files-between-two-remote-servers-using-ansible/28007/4 "2018-07-10T15:41:31Z")

</div>

Is that you are referring to same hosts file comes under Ansible repository(/etc/ansible/hosts) or you are using any different custom hosts file ?

---

<div class="post-metadata">

**Author:** ![tkuratom](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@tkuratom](https://forum.ansible.com/u/tkuratom)\
**Post date:** [July 10, 2018, 5:17pm UTC](https://forum.ansible.com/t/how-to-copy-files-between-two-remote-servers-using-ansible/28007/5 "2018-07-10T17:17:11Z")

</div>

Your hosts file appears to be using groups for ServerA and ServerB.  
delegate\_to doesn't actually know about groups so delegate\_to is not  
finding an ansible host with that name and then it's consulting dns as  
a fallback. there are several ways to achieve what you want, though.

\* In your example hosts file, it looks like you can define a host  
rather than a group for ServerA and ServerB.  
\* If you do have multple Servers to process, you can use a loop:

&nbsp;&nbsp;- name: Transferring file from ServerA to ServerB  
&nbsp;&nbsp;&nbsp;&nbsp;synchronize:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: /home/ubuntu/Dockerfile  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /home/ubuntu  
&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: '{{ item }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_inventory\_hostnames: ServerA

-Toshio

---

<div class="post-metadata">

**Author:** ![Mangai](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Mangai](https://forum.ansible.com/u/Mangai)\
**Post date:** [January 14, 2019, 7:42am UTC](https://forum.ansible.com/t/how-to-copy-files-between-two-remote-servers-using-ansible/28007/6 "2019-01-14T07:42:31Z")

</div>

I wanted to copy directory from the remote.  
ssh is set. I am able to access the remote without the password but still facing issue while copying.
