avatar

Hi, I'm Mayeu

I mostly do computering, tea, and travelling


What's a good way of sending a big directory structure to a server using Ansible?

Ansible has a copy module that works really well for moving a small set of files around. But it can rapidly slow down your playbook since it does not scale well with hundreds of files. So how can you move a big set of files and folder without losing too much time? Well, you use another module named synchronize for this.

synchronize uses a tool named rsync under the hood, so you first have to ensure it is installed on your machine, and on the targeted host (it comes with most Unixes). If you are using macOS or Ubuntu, it should be already there. Otherwise start by installing it on your computer.

To be sure it is installed on your targeted server you should add a task to install it before you use the synchronize module in your playbook. Here it is an example for Ubuntu or Debian hosts:

1
2
3
4
- name: Install rsync running synchronize later
  apt:
    name: rsync
    state: present

By default synchronize just require a src and a dest, but like a lot of Ansible modules there are many more options for finer control.

Here is a minimal example of using the module:

1
2
3
4
- name: Synchronize a local folder to my remote server
  synchronize:
    src: path/to/my/local/folder
    dest: /absolute/path/to/my/destination/folder

And with that you can copy a gazillion files (at least) without worrying that your copy will be slow!

Happy syncing 👋


Did you enjoy this article? Don't want to miss any new posts and get exclusive content? Then hop into my newsletter :)

You can also contact me directly via email: m [-at-] mayeu [-dot-] me