Curious Minds

Clear answers to science and history questions

Why Copying a Directory with Ansible Is So Slow

· 6 min read · Science

Copying a directory with Ansible is slow primarily because the copy module does not transfer a directory as a single archive; instead, it walks the source tree and sends each file individually over the connection, and it also computes checksums for every file to decide whether a change is needed. This per-file overhead, combined with SSH connection setup and Ansible's default settings, makes directory copies far slower than a single tar or rsync transfer.

How Ansible's copy module handles directories

When you pass a directory to the copy module with src, Ansible recursively lists every file and subdirectory under that path. For each file, it performs a checksum comparison (by default SHA-1) between the local and remote versions to determine if the file needs to be transferred. If the checksums differ, the file is sent over the connection. This means that even when nothing has changed, Ansible still reads every local file to compute its checksum and may also read the remote file to compare, depending on the remote_src and checksum settings.

The result is that copying a directory with 1,000 small files involves 1,000 separate checksum calculations, 1,000 potential file transfers, and 1,000 remote file operations. Each of these operations has overhead: opening a file, reading it, computing a hash, and communicating the result. For large directories, this overhead dominates the total time, even if the total data size is modest.

The role of SSH and connection setup

Ansible typically connects to remote hosts over SSH. While SSH multiplexing (ControlPersist) can reuse a single connection for multiple tasks, each file transfer within a single copy task still involves multiple round trips: checking the remote file's existence and checksum, sending the file data, and verifying the result. With many small files, the latency of these round trips adds up quickly. On high-latency links, the effect is pronounced: a directory of 500 tiny files can take minutes to copy even though the total size is only a few megabytes.

Additionally, Ansible's default connection plugin may not use the most efficient transfer method. The copy module uses the put_file method of the connection plugin, which for SSH typically uses SFTP or SCP. These protocols have their own per-file overhead, and SFTP in particular can be slower for many small files than a single streamed archive.

Checksumming and idempotence

Ansible is designed to be idempotent: running the same playbook twice should not change the system if nothing has changed. To achieve this for file copies, the copy module compares checksums. By default, it computes the checksum of the local file and, if the remote file exists, the checksum of the remote file. If they match, the file is skipped. This is good for correctness but costly for performance, because it requires reading both files entirely.

You can adjust this behavior with the checksum parameter. Setting checksum: '' (empty string) disables checksum comparison and makes Ansible rely on file size and modification time instead, which is faster but less reliable. Alternatively, you can use the force: no parameter to skip the copy if the remote file exists, but that can lead to stale files if the source changes.

Common scenarios where slowness is noticeable

  • Many small files: Directories with hundreds or thousands of small files (e.g., source code, configuration trees, static assets) are the worst case because per-file overhead dominates.
  • High-latency connections: Copying to remote hosts across a WAN or VPN amplifies round-trip delays.
  • Large directories with few changes: Even if only one file changed, Ansible still checksums every file, so the task takes almost as long as a full copy.
  • Using copy with remote_src: no (default): The module must transfer each file from the control node, which involves reading the local file and sending it over the connection.

How to make directory copies faster

Several strategies can reduce the time required to copy a directory with Ansible. The best choice depends on your environment and requirements.

  1. Use the synchronize module: This module wraps rsync, which is highly efficient for directory trees. It transfers only changed files and uses delta encoding to minimize data transfer. However, it requires rsync to be installed on both the control node and the target host, and it may not work well with all connection plugins.
  2. Archive the directory and copy a single file: Use the archive module to create a tarball on the control node, then copy that single file with the copy module, and finally extract it on the remote host with the unarchive module. This reduces the number of file operations to three, regardless of the number of files in the directory. The trade-off is that you need temporary disk space and the archive must be created and transferred in full each time.
  3. Use unarchive with remote_src: no: The unarchive module can copy an archive from the control node and extract it on the remote host in one step. This is essentially the same as the previous approach but packaged in a single task.
  4. Adjust checksum settings: If you are confident that file sizes and modification times are sufficient to detect changes, set checksum: '' to skip checksumming. Be aware that this can miss changes if a file's size and mtime are unchanged but content differs.
  5. Increase parallelism: If you are copying to multiple hosts, Ansible runs tasks in parallel across hosts (controlled by forks). Increasing the number of forks can improve overall throughput, but it does not speed up a single host's copy.
  6. Use a faster connection plugin: For large transfers, consider using ssh with pipelining enabled (in ansible.cfg, set pipelining = True) to reduce the number of SSH operations. Pipelining can significantly speed up module execution by avoiding temporary file creation on the remote host.

Comparing copy, synchronize, and unarchive

Each method has trade-offs. The copy module is simple and works out of the box, but it is the slowest for directories with many files. The synchronize module is fast and efficient for repeated copies, but it requires rsync and may not be suitable for all environments (e.g., Windows targets without rsync). The unarchive approach is fast for one-time copies but creates a full archive each time, which can be slow for very large directories and uses extra disk space.

In practice, many Ansible users combine these: use synchronize for ongoing synchronization of large directory trees, and use unarchive for initial deployment of application code.

Frequently Asked Questions

Why does Ansible copy take longer than scp for the same directory?

Because Ansible's copy module processes each file individually, including checksumming and separate transfer operations, whereas scp can stream a directory recursively in a single command with less per-file overhead. Ansible also adds Python module execution overhead on the remote host.

Does Ansible copy the whole directory every time, even if nothing changed?

No, it checks each file's checksum and only transfers files that differ. However, it still reads every file to compute checksums, so the task takes time proportional to the number of files even when no transfers occur.

Can I disable checksumming to speed up copies?

Yes, by setting checksum: '' in the copy task. This makes Ansible rely on file size and modification time, which is faster but less reliable for detecting changes.

Is the synchronize module always faster than copy?

For directories with many files, synchronize is usually much faster because it uses rsync's efficient delta-transfer algorithm and handles the directory as a single operation. However, it requires rsync on both ends and may not work with all connection types.

Why is copying a directory to a Windows host especially slow?

Ansible's Windows support uses WinRM or SSH, and the copy module for Windows may have additional overhead. The synchronize module is not available for Windows targets, so alternatives like win_copy (which also processes files individually) or using unarchive with a zip file are common.

Does increasing forks help with a single directory copy?

No, forks controls parallelism across hosts, not within a single host's task. To speed up a single host's copy, you need to change the method (e.g., use synchronize or unarchive) or adjust module parameters.

In this article
  1. How Ansible's copy module handles directories
  2. The role of SSH and connection setup
  3. Checksumming and idempotence
  4. Common scenarios where slowness is noticeable
  5. How to make directory copies faster
  6. Comparing copy, synchronize, and unarchive
  7. Frequently Asked Questions