Backup With Rsync - Local to Remote - Ansible module synchronize
How to automate the backup of an “examples” directory minimizing network usage on Linux using Ansible module synchronize and rsync utility.


How to backing up with rsync with Ansible?
I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
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/yes
The 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.
Links
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
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 demo
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 demo] ********************************************************************
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 demo] ********************************************************************
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 [email protected]
Last login: Mon Mar 28 12:22:14 2022 from 192.168.0.59
[[email protected] ~]$ 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
[[email protected] ~]$ rpm -qa | grep rsync
rsync-3.1.3-12.el8.x86_64
[[email protected] ~]$
after execution
ansible-pilot $ ssh [email protected]
Last login: Mon Mar 28 12:24:57 2022 from 192.168.0.59
[[email protected] ~]$ 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
[[email protected] ~]$ tree example-backup/
example-backup/
`-- examples
|-- report2.txt
`-- report.txt
1 directory, 2 files
[[email protected] ~]$ cat example-backup/examples/report.txt
test report.txt
[[email protected] ~]$ cat example-backup/examples/report2.txt
test report.txt
[[email protected] ~]$ 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
[[email protected] ~]$
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 demo] ********************************************************************
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 [email protected]
Last login: Mon Mar 28 18:44:10 2022 from 192.168.0.59
[[email protected] ~]$ 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
[[email protected] ~]$ 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
[[email protected] ~]$
Recap
Now you know how to Backup with rsync with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate