# Ansible Sync Remote Server A to Remote Server B

**URL:** <https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616>\
**Category:** Get Help\
**Tags:** rsync, synchronize, playbook\
**Created:** [September 12, 2023, 8:18pm UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616 "2023-09-12T20:18:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![KMRSolutions](https://avatars.discourse-cdn.com/v4/letter/k/e0b2c6/32.png) [@KMRSolutions](https://forum.ansible.com/u/KMRSolutions)\
**Post date:** [September 12, 2023, 8:18pm UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/1 "2023-09-12T20:18:59Z")

</div>

Need some advice regarding remote to remote syncing

I need to be able to run an ansible playbook from my local machine. And it sync a folder from Remote Server A to Remote Server B

I found several solutions online but all of them failed on the same issue.

From what I can see from the error

It looks like it’s using my local machine username instead of the ansible credientials I have in my ansible hosts which it uses on all other playbooks.

I need to run this from my local as it will be part of a provisioning new servers,one really big job combining 4 separate roles. The other 3 work perfectly.

## Some examples I’ve tried Example 1

- name: Sync Pull task - Executed on the Destination host “{{Migration-A}}”  
hosts: Migration-A  
user: ansible  
tasks:
  - name: Copy the file from Migration-B to Migration-A using Method Pull  
tags: sync-pull  
synchronize:  
src: /home/  
dest: /home/  
mode: pull  
delegate\_to: Migration-B  
register: syncfile  
run\_once: true

```auto
Example 2

---
- name: Copy Directories from Server A to Server B
  hosts: Migration-A # Replace with the actual hostname or group of Server A
  become: yes # You may need to escalate privileges to read the source directories

  tasks:
    - name: Copy /home/ from Server A to Server B
      synchronize:
        src: /home/
        dest: /home/
      delegate_to: Migration-B # Replace with the actual hostname of Server B
      become: yes # You may need to escalate privileges to write to the destination directory
```

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 12, 2023, 8:29pm UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/2 "2023-09-12T20:29:51Z")

</div>

Assuming the target (inventory\_hostname) is A, the copy target is B and the controller is C.

Most solutions you’ll have found won’t work with `become: yes`, you can only use become on the target server, but no the ‘other server’, so using things like ` rsync+ssh://user@B:/home/` (where user is not `root`) won’t work. Even though that is probably the most common solution for sync from A to B while controller is C.

Delegation just changes the ‘target’ and which means that your play instead of doing a sync from C to A does C to B. There is no existing solution with the `synchronize` action that will handle a 3 way + become. You can at best do sync A to C and then C to B within a playbook.

Other alternatives:

- Install and run `rsync` server (runs as `root`) on B and use `synchronize` from A with `dest: rsync://user@B:/home`
- Setup /home as network share (if NFS, no root squash) and use `synchronize` with ‘local paths’
- Enable a root login from A to B and then `dest: rsync+ssh://B:/home`

---

<div class="post-metadata">

**Author:** ![it-pappa](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/it-pappa/32/7523_2.png) [@it-pappa](https://forum.ansible.com/u/it-pappa)\
**Post date:** [September 12, 2023, 8:57pm UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/3 "2023-09-12T20:57:27Z")

</div>

Don’t know the reason why this task has to be done, but maybe using a share on the servers may be a solution?

As @bcoca provided some good solutions also. I would not see Ansible as a sync service tho. Move files around sure, but not a sync service.

robocopy or rsync would be a better solution, or work with shares. Use ansible to trigger robocopy on the remote etc is another thing I can get behind tho 🙂

---

<div class="post-metadata">

**Author:** ![KMRSolutions](https://avatars.discourse-cdn.com/v4/letter/k/e0b2c6/32.png) [@KMRSolutions](https://forum.ansible.com/u/KMRSolutions)\
**Post date:** [September 12, 2023, 10:00pm UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/4 "2023-09-12T22:00:52Z")

</div>

Thankyou @bcoca coca & @it-pappa

i would usually just use Rsync manually to sync the servers.

im in the process planning and testing migrating several production servers from Centos 7 to AlmaLinux 9

i have the playbooks written and tested for the base setup of the servers. creating the users adding ssh keys, building the lvm home pool, installing nginx php-fpm, composer, symfony etc

so the last part is to sync the /home/ and a couple of config files from old prod to new prod.

i had read that ansible had a sync feature so wondered if i could finish the whole task with just ansible. this way i could easily reuse for each server.

but if A B C setup wont work then i will just setup rsync manually to copy B to A from C like i used to do.

---

<div class="post-metadata">

**Author:** ![TheSpy0](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/thespy0/32/496_2.png) [@TheSpy0](https://forum.ansible.com/u/TheSpy0)\
**Post date:** [September 12, 2023, 11:05pm UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/5 "2023-09-12T23:05:45Z")

</div>

Doesn’t the synchronize module use rsync?

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 12, 2023, 11:14pm UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/6 "2023-09-12T23:14:36Z")

</div>

It does, but it emulates rsync from the command line as the login user, which creates a temporary rsync server on the ‘target’ but for ‘root’ to work either you login as root to the target (normally disabled) or you run rsync as a service there (as root).

You normally use rsync + root to preserve permissions on the files copied, when they are not the same user/group as the user doing the copying.

Ansible can put a lot of scaffolding around actions but it does not prevent the need for root access in some cases, become helps doing normal escalation on a target, but it won’t work in this case as they have 2 targets for one action and become is not designed for this … nor is rsync.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 12, 2023, 11:20pm UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/7 "2023-09-12T23:20:31Z")

</div>

Have you tried `scp -3`? it routes the traffic through C but does not write to disk so probably faster for new copies than `rsync`.

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [September 12, 2023, 11:24pm UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/8 "2023-09-12T23:24:37Z")

</div>

Also, I already pointed it out in the original post, Ansible CAN copy from A to C to B, just needs 2 synchronize tasks.

---

<div class="post-metadata">

**Author:** ![gwmngilfen](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gwmngilfen/32/6_2.png) [@gwmngilfen](https://forum.ansible.com/u/gwmngilfen)\
**Post date:** [October 6, 2023, 9:17am UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/9 "2023-10-06T09:17:26Z")

</div>

Hi @KMRSolutions! It looks like the post might be solved - could you check to see if the response by @bcoca worked for you?

If so, it would be _super_ helpful if you could click the ✔ on their post to accept the solution - it helps users find solutions (solved topics have a higher search priority), recognises the input of the people who help you, helps our volunteers find new issues to answer, and keeps the forum nice and tidy. It’s just a nice way to give back, and only takes a moment 🙂

Thanks!  
(this is template reply, do feel free to reply if I’ve misunderstood the situation!)

---

<div class="post-metadata">

**Author:** ![wa\_schaly](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wa_schaly/32/1079_2.png) [@wa\_schaly](https://forum.ansible.com/u/wa_schaly)\
**Post date:** [October 8, 2023, 9:54am UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/10 "2023-10-08T09:54:59Z")

</div>

I wouldn’t sync it by using ansible I would use git.  
If a play is ready to run I push it to a central git server. Any server(s) that you want to use the same play can do a git clone and execute the play with ansible.  
The additional benefit is that as soon as you have any update made to the play you don’t need to bother pushing it to all your ansible instances

---

<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:** [November 7, 2023, 9:55am UTC](https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616/11 "2023-11-07T09:55:09Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
