In Ansible, we have access to the “ansible.posix.synchronize” module as part of the “ansible.posix” collection. This module provides a wrapper interface around the rsync command in Unix-like systems.
If you are not familiar, the rsync utility provides a powerful and efficient method of synchronizing the files and directories from one location to another with minimal data transfer by using the delta encoding where it is applicable.
Using this module, we have an easy and user-friendly method of integrating the rsync operations in a declarative Ansible playbook, making it easier to automate the file and directory synchronization process.
Prerequisites:
To follow along, ensure that you have the following:
- Installed Ansible
- The target machine must have the rsync installed
Installing the Ansible.Posix Collection
Before we can use the “ansible.posix.synchronize” module, we need to install the “ansible.posix” collection.
We can do this by running the ansible-galaxy command as follows:
Ansible.Posix.Synchronize Module Parameters
The “ansible.posix.synchronize” module provides a variety of parameters which allow us to customize its behavior. The following are some important parameters that you should know when working with the synchronize module.
src – It specifies the path to the source directory that you wish to synchronize.
dest – It specifies the path to the destination directory to sync.
dest_port – It specifies an alternate SSH port for the destination host.
delete – If set to yes, the module deletes the files in the destination that don’t exist in the source.
rsync_path – It sets the rsync command to run on the remote host. It is useful if it’s not in the PATH.
recursive – If set to yes, it synchronizes the directories recursively.
compress – Use compression during the rsync operation.
archive – If set to yes, rsync archives the files (preserve permissions, times, owner, group, ACLs, and xattrs).
The given parameters are some commonly used parameters when working with the synchronize module. You can refer to the documentation for a more detailed information on the other available parameters.
Basic Usage:
The most basic usage of the synchronize module is to sync the files or directories from a source to a destination. An example playbook on how to use this module to synchronize the files or directories is shown in the following:
- hosts: all
tasks:
- name: Sync dir from src to dest
ansible.posix.synchronize:
src: /opt/src
dest: /home/debian/opt/dest
In this case, we use the synchronize module to synchronize the specified source directory to the destination directory.
Sync the Directory from Local to Remote
We can also use this module to sync a local directory to a remote machine as demonstrated in the following playbook:
- hosts: db_server
tasks:
- name: Sync local dir to remote
ansible.posix.synchronize:
src: /opt/configs/latest
dest: /etc/mysqld/config/latest
This should sync the specified directory on the local machine to the specified path on the remote host.
Sync from Remote to Local
As demonstrated in the example playbook, we can also sync from a remote machine to the local machine.
- hosts: db_server
tasks:
- name: Sync remote directory to local
ansible.posix.synchronize:
src: /etc/mysqld/config/latest
dest: /opt/configs/latest
delegate_to: localhost
Set the Rsync Path
In some cases, rsync may not be available in the PATH environment variable in your remote system or you may wish to use a specific version. You can specify the path as follows:
- hosts: all
tasks:
- name: Sync with specific rsync path
ansible.posix.synchronize:
src: /local/path/
dest: /remote/path/
rsync_path: /path/to/rsync
Sync Between Two Remote Machines
We can also sync the directories between two systems using the delegate_to parameter. An example playbook is shown in the following:
tasks:
- name: Sync between two remote machines
ansible.posix.synchronize:
src: /path/on/db_master/
dest: remote_host_2:/path/on/db_slave/
delegate_to: db_master
Exclude the Files/Directories
To exclude specific files or directories from sync, we can use the exclude parameter as follows:
- hosts: all
tasks:
- name: Sync while excluding specific files/directories
ansible.posix.synchronize:
src: /local/path/
dest: /remote/path/
exclude:
- some_directory/
- "*.tmp"
Using Compression
To use compression for the sync operation, set the compress parameter to yes as demonstrated in the following example:
- hosts: all
tasks:
- name: Sync with compression
ansible.posix.synchronize:
src: /local/path/
dest: /remote/path/
compress: yes
Conclusion
You learned how to work with the synchronize module from the “ansible.posix” collection to use the rsync utility in your systems. We also covered the practical examples on how to work with this module effectively.