Ansible synchronize Module: Rsync Files & Directories Between Hosts
By Luca Berton · Published 2024-01-01 · Category: installation
Discover how to use Ansible synchronize module for efficient file backups with rsync. Learn practical steps and see real-world examples for Linux systems.

How to backing up with rsync with Ansible?
I'm going to show you a live Playbook and some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.See also: Ansible Backup Windows 10/11/2019/2022: win_robocopy Playbook (Guide)
Ansible backup with rsync
> ansible.posix.synchronize: synchronize is a wrapper around rsync to make common tasks in your playbooks quick and easy.
Today we're talking about the Ansible module synchronize.
The full name is ansible.posix.synchronize, which means that is part of the collection targeting POSIX platforms.
A wrapper around rsync to make common tasks in your playbooks quick and easy.
rsync is a utility for efficiently transferring and synchronizing files between a computer and a storage drive and across networked computers by comparing the modification times and sizes of files.
rsync must be installed on both the local and remote host.
Currently, there are only a few connection types that support synchronize (ssh, paramiko, local, and docker) because a sync strategy has been determined for those connection types.
Main Parameters
• src _string_ - source path - absolute or relative • dest _string_ - destination path - absolute or relative • archive _boolean_ - mirrors the Rsync archive flag, enables recursive, links, perms, times, owner, group flags, and -D • rsync_opts _string_ - no/yesThe parameter list is pretty wide but these are the most important options of synchronize module.
The only mandatory parameters are "src" and "dest" parameters.
The "src" parameter is mandatory and specifies the path on the source host that will be synchronized to the destination. The path can be absolute or relative.
Same story for the dest parameter that specifies the path on the destination host that will be synchronized from the source.
Paths could be Local or Remote according to your needs.
The most useful parameter is "archive", which mirrors the Rsync archive flag, enables recursive, links, perms, times, owner, group flags, and -D. It's enabled by default so in the majority of use-cases you don't need to specify anything else. Please refer to the manual to enable/disable some specific settings.
It's possible to specify more Rsync parameters via the "rsync_opts" parameter.
See also: Automate Dell EMC DNOS 10 Backups with Ansible Playbook
Links
• rsync • ansible.posix.synchronize## Playbook
How to backup with rsync with Ansible Playbook. I’m going to show you how to replicate some directory tree in a Linux machine using the rsync utility via syncronize module.
code
---
- name: synchronize module Playbook
hosts: all
become: false
vars:
source: '~/prj/github/ansible-pilot/copy\ files\ to\ remote\ hosts/examples'
destination: 'example-backup'
tasks:
- name: rsync installed
ansible.builtin.package:
name: rsync
state: present
become: true
- name: data synchronization
ansible.posix.synchronize:
src: '{{ source }}'
dest: '{{ destination }}'
execution
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory copy\ files\ to\ remote\ hosts/rsync.yml
PLAY [synchronize module Playbook] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [rsync installed] ****************************************************************************
ok: [demo.example.com]
TASK [data synchronization] ***********************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory copy\ files\ to\ remote\ hosts/rsync.yml
PLAY [synchronize module Playbook] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [rsync installed] ****************************************************************************
ok: [demo.example.com]
TASK [data synchronization] ***********************************************************************
ok: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
ansible-pilot $ ls -al copy\ files\ to\ remote\ hosts/examples
total 16
drwxr-xr-x 4 lberton staff 128 Jan 17 11:54 .
drwxr-xr-x 9 lberton staff 288 Mar 28 14:13 ..
-rw-r--r-- 1 lberton staff 16 Sep 4 2021 report.txt
-rw-r--r-- 1 lberton staff 16 Jan 17 11:52 report2.txt
ansible-pilot $ cat copy\ files\ to\ remote\ hosts/examples/report.txt
test report.txt
ansible-pilot $ cat copy\ files\ to\ remote\ hosts/examples/report2.txt
test report.txt
ansible-pilot $ ssh devops@demo.example.com
Last login: Mon Mar 28 12:22:14 2022 from 192.168.0.59
[devops@demo ~]$ ls -al
total 20
drwx------. 4 devops wheel 127 Mar 28 12:22 .
drwxr-xr-x. 4 root root 35 Mar 24 17:44 ..
drwx------. 3 devops wheel 17 Mar 24 17:44 .ansible
-rw-------. 1 devops wheel 178 Mar 28 12:21 .bash_history
-rw-r--r--. 1 devops wheel 18 Jul 26 2021 .bash_logout
-rw-r--r--. 1 devops wheel 141 Jul 26 2021 .bash_profile
-rw-r--r--. 1 devops wheel 376 Jul 26 2021 .bashrc
drwx------. 2 devops wheel 29 Mar 24 17:44 .ssh
-rw-------. 1 devops wheel 1756 Mar 28 10:38 .viminfo
[devops@demo ~]$ rpm -qa | grep rsync
rsync-3.1.3-12.el8.x86_64
[devops@demo ~]$
after execution
ansible-pilot $ ssh devops@demo.example.com
Last login: Mon Mar 28 12:24:57 2022 from 192.168.0.59
[devops@demo ~]$ ls -al
total 20
drwx------. 5 devops wheel 149 Mar 28 12:24 .
drwxr-xr-x. 4 root root 35 Mar 24 17:44 ..
drwx------. 3 devops wheel 17 Mar 24 17:44 .ansible
-rw-------. 1 devops wheel 211 Mar 28 12:24 .bash_history
-rw-r--r--. 1 devops wheel 18 Jul 26 2021 .bash_logout
-rw-r--r--. 1 devops wheel 141 Jul 26 2021 .bash_profile
-rw-r--r--. 1 devops wheel 376 Jul 26 2021 .bashrc
drwx------. 2 devops wheel 29 Mar 24 17:44 .ssh
-rw-------. 1 devops wheel 1756 Mar 28 10:38 .viminfo
drwxr-xr-x. 3 devops wheel 22 Mar 28 12:24 example-backup
[devops@demo ~]$ tree example-backup/
example-backup/
`-- examples
|-- report2.txt
`-- report.txt
1 directory, 2 files
[devops@demo ~]$ cat example-backup/examples/report.txt
test report.txt
[devops@demo ~]$ cat example-backup/examples/report2.txt
test report.txt
[devops@demo ~]$ ls -al example-backup/examples/
total 8
drwxr-xr-x. 2 devops wheel 43 Jan 17 10:54 .
drwxr-xr-x. 3 devops wheel 22 Mar 28 12:24 ..
-rw-r--r--. 1 devops wheel 16 Sep 4 2021 report.txt
-rw-r--r--. 1 devops wheel 16 Jan 17 10:52 report2.txt
[devops@demo ~]$
changes in the source directory
ansible-pilot $ touch copy\ files\ to\ remote\ hosts/examples/report3.txt
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory copy\ files\ to\ remote\ hosts/rsync.yml
PLAY [synchronize module Playbook] ********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [rsync installed] ****************************************************************************
ok: [demo.example.com]
TASK [data synchronization] ***********************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $ ssh devops@demo.example.com
Last login: Mon Mar 28 18:44:10 2022 from 192.168.0.59
[devops@demo ~]$ ls -al example-backup/
total 0
drwxr-xr-x. 3 devops wheel 22 Mar 28 12:24 .
drwx------. 5 devops wheel 149 Mar 28 12:24 ..
drwxr-xr-x. 2 devops wheel 62 Mar 28 12:44 examples
[devops@demo ~]$ ls -al example-backup/examples/
total 8
drwxr-xr-x. 2 devops wheel 62 Mar 28 12:44 .
drwxr-xr-x. 3 devops wheel 22 Mar 28 12:24 ..
-rw-r--r--. 1 devops wheel 16 Sep 4 2021 report.txt
-rw-r--r--. 1 devops wheel 16 Jan 17 10:52 report2.txt
-rw-r--r--. 1 devops wheel 0 Mar 28 18:44 report3.txt
[devops@demo ~]$
Conclusion
Now you know how to Backup with rsync with Ansible.
See also: Backup Dell EMC DNOS 6 Configs with Ansible Playbook
Related Articles
• Windows fleet automation with Ansible • managing Docker with Ansible • Ansible inventory best practices • Ansible become methods compared • Ansible Cron Module GuideCategory: installation
Watch the video: Ansible synchronize Module: Rsync Files & Directories Between Hosts — Video Tutorial