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.
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.
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:
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:
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.
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:
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:
Exclude the Files/Directories
To exclude specific files or directories from sync, we can use the exclude parameter as follows:
To use compression for the sync operation, set the compress parameter to yes as demonstrated in the following example:
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.