Hi,
I was just working on a playbook with the challenge of syncing files from Server A to Server B which was already discussed in a topic:
https://forum.ansible.com/t/ansible-sync-remote-server-a-to-remote-server-b/616
To respond to some of the points in the answer:
- “won’t work with
become: yes
.” This is quite inconvenient, since it blocks arbitrary directory copies between machines where you often need root privileges. - " Install and run
rsync
server (runs asroot
) on B" . The ansible synchronize module doesn’t set this up automatically, right? - “Setup /home as network share” Not a very trivial task.
- “Enable a root login from A to B”. That is usually prohibited.
The end result, after going over all these items, is that something you would hope to be handled really well by Ansible is difficult to implement.
Possible action items… Just ideas…
-
A new ansible module that implements this functionality. That is - full ‘become’ privileges, full ‘root’ privileges while easily copying files from Server A to Server B, and it’s all maximally automated, with one task. This is the type of puzzle Ansible is designed to solve for users.
-
Or, modify the synchronize module to support it.
-
Or, at least, add a page of documentation discussing this topic in great detail with many examples and instructions. “How to Sync Files and Directories between Remote Server A and Remote Server B”. Link to that doc page from the synchronize module docs. For now, the user must search around on the web.
By the way, the following is a solution that meets many of the criterion:
- copying files from Server A to B, without involving C (control machine)
- Allows copying ‘root’ files, or setting ‘root’ permissions.
----
To run on 'source' hosts:
----
- name: Copy files to tmp folder
ansible.builtin.copy:
src: "/dir1"
dest: "/tmp/ansible_tmp_1"
owner: "ubuntu"
group: "ubuntu"
mode: '0755'
remote_src: true
- name: Transfering files
become: false
ansible.posix.synchronize:
src: "/tmp/ansible_tmp_1/dir1"
dest: "/tmp/ansible_tmp_1"
mode: pull
delegate_to: "{{ item }}"
vars:
ansible_ssh_extra_args: '-A'
with_items: "{{ groups['destination-group'] }}"
- name: Cleanup files from source
ansible.builtin.file:
path: "/tmp/ansible_tmp_1"
state: absent
---
To run on dest hosts:
---
- name: Copy tmp files to dest
become: true
ansible.builtin.copy:
src: "/tmp/ansible_tmp_1/dir1"
dest: "/"
owner: "root"
group: "root"
mode: "0755"
remote_src: true
- name: Cleanup files from dest
ansible.builtin.file:
path: "/tmp/ansible_tmp_1"
state: absent