20

What are the pros and cons to using Ansible Synchronize vs Copy modules. As far as I can tell synchronize has all the functionality that copy does but may be much faster so I'm considering changing everything to use synchronize. The only downside of synchronize is that rsync is required, which seems fairly ubiquitous in the Linux environment.

Alex Ethier
  • 497
  • 2
  • 7
  • 18

4 Answers4

12

The differences are pretty similar to traditional rsync vs scp. Rsync has more features and is often faster, however it's a little bit trickier to setup and has more knobs to turn.

Additionally, https://docs.ansible.com/ansible/copy_module.html states:

The “copy” module recursively copy facility does not scale to lots (>hundreds) of files. For alternative, see synchronize module, which is a wrapper around rsync.

Mxx
  • 8,979
  • 4
  • 27
  • 37
5

As of Ansible v2.8, synchronize is still in “preview” status:

This module is not guaranteed to have a backwards compatible interface. [preview]

In Ansible v2.10, the copy module is moved to the Builtin Collection, which is officially maintained by the core team (i.e. Red Hat), and distributed with ansible-base (a.k.a. ansible-core). In contrast, synchronize is moved to the POSIX Collection maintained by community.

I would use copy when I don’t need the performance and functionality of synchronize. I suggest starting with copy, and move to synchronize only when this is the bottleneck. (Verify this with benchmark!)

Franklin Yu
  • 8,920
  • 6
  • 43
  • 57
3

There is one big difference that was a showstopper for us: synchronize does not reuse the ssh session from ansible. When using a load-balancer, this caused us quite a headache to find the root cause:

  • ansible would start an ssh-session to a load-balancer, and would end up at machine1
  • all tasks would thus work wrt machine1
  • the synchronize task uses rsync under the hood which starts a new ssh session and might end up connecting to machine2 (randomly)
  • when other tasks depend on the files from the synchronize being present, they will fail because the files are present on another machine.
Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • Thanks for the explanation. I also found the synchronize task to unpredictably fail on rare occasions and have avoided using it since. This is probably the underlying reason. – Alex Ethier Jul 15 '20 at 23:42
  • 2
    I think your failing there is using a load balancer, not the synchronize module. If you're copying files, you should be explicitly specifying hostnames, not rolling dice with a load balancer. – Asa Stallard Dec 15 '22 at 23:45
0

Using ansible 2.4.2.0, this works flawlessly. I should probably not be using ansible from the CentOS repos. Too far behind the curve, no offense to CentOS.

- hosts: all
  become: true
  tasks:

  - name: copy icinga2 2.7.2
    synchronize:
      src: /home/ansible/playbooks/files/icinga2.7
      dest: /home/ansible
      owner: yes
Chen A.
  • 10,140
  • 3
  • 42
  • 61
mr.zog
  • 533
  • 1
  • 7
  • 26