How to Copy Files between two Remote Servers using Ansible

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

`

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

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.

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 ?

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:

  - name: Transferring file from ServerA to ServerB
    synchronize:
      src: /home/ubuntu/Dockerfile
      dest: /home/ubuntu
    delegate_to: '{{ item }}'
     with_inventory_hostnames: ServerA

-Toshio

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.