EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Ansible Synchronize

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Ansible Tutorial » Ansible Synchronize

Ansible Synchronize

Introduction to Ansible Synchronize

Ansible Synchronize is the module that can be used to do some of the common tasks which we do by using rsync on Linux machines. Though it does not have the full power of Linux rsync, but we know that those special features of rsync are needed only sometimes. We can still use command and shell module to call rsync, but that needs some other host facts and parameters to make it work successfully. Also, it needs to be done carefully. From Linux documentation, we can say that Ansible synchronize is a wrapper around rsync to perform the common task easily and smoothly via playbook.

Explaining the Ansible Synchronize

Ansible synchronizes work like rsync in many ways. But we must remember the below points while using it in our environments. These will explain this module and show its limitations:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • To work with this module, rsync must be installed on both source and target systems.
  • By default, the source of files is localhost i.e. the controller machine and destination of files is the machine where the connection is made to transfer files. This default feature can be changed by using parameter delegate_to, which allows you to change your source from localhost to some other host. Thus you can copy files from a remote machine to another remote machine.
  • On synchronize source machine, file’s permissions are of the user who is running the tasks on localhost or remote_user in the case when delegate_to is used.
  • On synchronize destination machine, the file’s permission will be of remote_user on destination host or of become_user if become=yes is given in parameters, but to elevate the permission, password less sudo should be set up, as rsync itself does not give a way to pass sudo credentials.
  • Currently, we have only below few connection types to work with Ansible synchronize viz are ssh, paramiko, local, and docker.
  • Always give the full path of the destination host location, as there may be cases where you used sudo, but files will be copied to remote_user home directory.
  • Linux rsync limitations related to hard links are also applied here.
  • Ansible synchronizes module forces -delay-updates to avoid the broken state in case of connection failure.

How does Ansible Synchronize Works?

Ansible synchronize user below parameters and their acceptable values are listed as below. Using the combination of these parameters, we can decide the behavior and output of Ansible synchronize.

  • archive: The acceptable values are yes and no. The default value is set to yes. This mirrors the rsync flag, enable recursive, links, perm, group, owner, time flag.
  • hecksum: The acceptable values are yes and no. The default value is set to no. This is used to skip based on checksum.
  • compress: The acceptable values are yes and no. The default value is set to yes. This is used to compress files during transfer to speed up the transfer.
  • copy_links: The acceptable values are yes and no. The default value is set to no. This is used to copy the referenced items rather than links.
  • delete: The acceptable values are yes and no. The default value is set to no. This is used to delete files in dest location, which does not exist on the source somehow when the transfer is completed. This works when recursive=yes is set.
  • dest: The absolute or relative path on the destination machine. Which will be synced from the source.
  • src: The absolute or relative path on the source machine. Which will be synced from the destination.
  • dirs: The acceptable values are yes and no. The default value is set to no. This is used to transfer directories without recursive.
  • dest_port: Port number of ssh on destination.
  • link_dest: Default value is null. This is used to add a destination to hard links against during the rsync.
  • links: This is used to copy syslinks as syslinks, not referenced items.
  • mode: Acceptable values are push and pull. The default value is push.
  • owner: The acceptable values are yes and no. This is used to preserve owner.
  • rsync_path: This is used to specify the rsync command path on remote hosts.
  • times: To preserve the modification times. The acceptable values are yes and no.

Examples of Ansible Synchronize

Now by using examples, we will try to learn about Ansible synchronize, which you might have to use in day to day operations. We will take some examples, but before going there, we first understand our lab, we used for testing purpose. Here we have an Ansible control server named ansible-controller and few remote hosts named host- remote, host-one, and host-two. We will create playbooks and run Ansible commands on the ansible- controller node and see the results on the remote hosts.

In this example, we will do the synchronization of files from source machine viz. Ansible controller node to the remote host. We have a set of files under /var/tmp/sync_folder on the Ansible controller node which will be transferred to remote host via Ansible synchronize module. For this we have a playbook like below:

---
- hosts: host-remote tasks:
- name: here we sync /var/tmp/sync_folder directory to remote host synchronize:
src: /var/tmp/sync_folder dest: /var/tmp/

ansible-playbook ansible_synchronize.yaml

Ansible Synchronize-1.1

On the remote machine, we can cross-check to confirm the files have been transferred.

Popular Course in this category
Ansible Training (1 Course, 4 Projects)1 Online Courses | 4 Hands-on Projects | 8+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (3,206 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

pwd
ls -l

Ansible Synchronize-1.2

In this example, we will do the synchronization of files from one remote host to another remote host. For this we have a playbook like below:

---
- hosts: host-one tasks:
- name: here we sync items from one remote host to another remote host synchronize:
src: /var/tmp/sync_folder dest: /var/tmp/ delegate_to: host-two

ansible-playbook ansible_synchronize_remote_src.yaml -v

Example-1.3

On the remote machine, we can cross-check to confirm the files have been transferred.

ls -l

Example-1.4

Conclusion

As we saw, Ansible synchronize is powerful but easy to use the module, but we must also acknowledge that this is not the replacement of rsync in Linux systems. So keeping in mind, you must also remember all the limitations mentioned above in this article to avoid unexpected outcomes. So learn it first and then use it carefully.

Recommended Articles

This is a guide to Ansible Synchronize. Here we also discuss the Introduction and how does ansible synchronize works along with examples and its code implementation. You may also have a look at the following articles to learn more –

  1. Ansible Roles
  2. Ansible Tags
  3. Ansible Loop
  4. Ansible Versions
  5. Terraform vs Ansible

Ansible Training (1 Course)

1 Online Courses

4 Hands-on Projects

8+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Ansible Tutorial
  • Ansible Basics
    • What is Ansible
    • Is Ansible free
    • How to Install Ansible
    • Ansible Versions
    • Ansible Architecture
    • Ansible Commands
    • Ansible ad-hoc Commands
    • Ansible Playbooks
    • Ansible Roles
    • Ansible Tags
    • Ansible Ping
    • Ansible Apt
    • Ansible Facts
    • Ansible Tower
    • Ansible Galaxy
    • Ansible Handlers
    • Ansible Loop
    • Ansible Block
    • Ansible Conditional
    • Ansible YAML
    • Ansible Debug
    • Ansible Synchronize
    • Ansible Template
    • Ansible group_vars
    • Ansible Lookup
    • Ansible File Module
    • Ansible Yum Module
    • Ansible Shell Module
    • Ansible lineinfile
    • Ansible Service Module
    • Ansible User Module
    • Ansible Windows Modules
    • ansible-doc
    • Ansible Filters
    • Ansible Add User to Group
    • Ansible Register
    • Ansible Set Fact
    • Ansible Hosts File
    • Ansible add_host
    • Ansible Collections
    • Ansible with_items
    • Ansible Replace Line in File
    • Ansible inventory_hostname
    • Ansible Dynamic Inventory
    • Ansible local_action
    • Ansible Firewalld
    • Ansible Unarchive
    • Ansible Sudo
    • Ansible Create Directory
    • Ansible Reboot
    • Ansible wait_for
    • Ansible run_once
    • Ansible Authorized_key
    • Ansible Interview Questions

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Ansible Training (1 Course) Learn More