Bootstrap FreeKB - Ansible - Copy remote files and directories using the synchronize module
Ansible - Copy remote files and directories using the synchronize module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

synchronize is used to copy files or directories, and is based off of rsync. The copy can be done locally, meaning a file or directory on a system is copied to some other directory on the same system. Or the copy can be done remotely, meaning the file or directory is copied from (pull) or to (push) a remote system.

In this example, the contents of the /tmp/foo directory on the control node (that's your Ansible system) will be copied to /tmp/foo on each managed node.

---
hosts: all
  tasks:
  - name: copy /tmp/foo
    synchronize:
      src: /tmp/foo
      dest: /tmp/foo
...

 

Following are examples of how to push (upload) and pull (download) files on a remote system. Let's say you run the playbook with the --ask-pass option.

ansible-playbook rsync.yml --ask-pass

 

Under the hood, the sshpass and rsync commands will be used, something like this.

sshpass -d8 /usr/bin/rsync --delay-updates -F --compress --archive --out-format=<<CHANGED>>%i %n%L /tmp/foo.txt rsync://server1.example.com/tmp/

 

On the other hand, if passwordless SSH authentication via a public/private keypair has been setup and you run the playbook without the --ask-pass option.

ansible-playbook rsync.yml

 

Then only the rsync command will be used, something like this.

/usr/bin/rsync --delay-updates -F --compress --archive --out-format=<<CHANGED>>%i %n%L /tmp/foo.txt rsync://server1.example.com/tmp/

 

It is also noteworthy that these commands expect the rsync daemon to be installed and running on the remote system. You may need to install the rsync-daemon package.

sudo dnf install rsync-daemon

 

And then start and enable the rsyncd service.

sudo systemctl enable rsyncd
sudo systemctl start rsyncd

 

synchronize is part of the ansible.posix collection thus you will need to install the ansible.posix collection to use synchronize. Check out my article on Install a collection using the ansible-galaxy collection install command.

In this example, the /tmp/foo directory on each managed node will be copied to /tmp/foo on server1.example.com. This would be a push (upload / PUT ) operation.

---
hosts: all
  tasks:
  - name: push /tmp/foo on {{ inventory_hostname }} to /tmp/foo on server1.example.com
    ansible.posix.synchronize:
      mode: push
      src: /tmp/foo
      dest: rsync://server1.example.com/tmp/foo
...

 

In this example, the /tmp/foo directory server1.example.com will be copied to /tmp/foo on each managed node. This would be a pull (download / GET ) operation.

---
hosts: all
  tasks:
  - name: pull /tmp/foo from server1.example.com to /tmp/foo on {{ inventory_hostname }}
    ansible.posix.synchronize:
      mode: pull
      src: rsync://server1.example.com/tmp/foo
      dest: /tmp/foo
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 0030b6 in the box below so that we can be sure you are a human.