Copy vs Featch

In Ansible, both fetch and copy modules are used for file transfers, but they work in opposite directions.

Feature

fetch

copy

Direction

Pulls files from remote to the control machine

Pushes files from control machine to remote

Use Case

Used to retrieve logs, backups, or config files from remote servers

Used to deploy scripts, configs, or binaries to remote servers

Example

fetch: src=/var/log/syslog dest=/tmp/

copy: src=/local/file.txt dest=/remote/path/

Destination Path

Destination is always on the control node

Destination is on the remote node

Permissions

Doesn't preserve permissions by default

Can preserve permissions (mode, owner, group)

Example Usage:

  1. fetch (Pull from remote to control machine)

    - name: Fetch logs from remote server
      fetch:
        src: /var/log/syslog
        dest: /tmp/
        flat: yes  # Avoids creating hostname-based directories
  2. copy (Push from control machine to remote server)

    - name: Copy configuration file to remote server
      copy:
        src: /local/config.yaml
        dest: /etc/app/config.yaml
        mode: '0644'

Let me know if you need more details! 🚀

Last updated