Operations 3 min read

Using Ansible fetch Module to Copy Files from Remote Server

This guide explains how to use Ansible's fetch module to copy files from a remote server to a local machine, including configuration, execution, verification, and key parameters such as flat and fail_on_missing.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Using Ansible fetch Module to Copy Files from Remote Server

The Ansible fetch module allows you to copy files from a remote server to the local machine, which is useful for retrieving log files or other artifacts.

By default, the module creates a directory on the local host named after each target host and places the fetched files there; missing remote files do not raise an error unless configured otherwise.

Configuration file (copy_fetch.yaml) :

- hosts: x.x.x.x
  remote_user: root
  gather_facts: false
  tasks:
    - name: Copy files from remote server
      fetch:
        src: /root/grafana-9.0.2-1.x86_64.rpm
        dest: /usr/local/apps/
        mode: 0644
        flat: yes
        fail_on_missing: yes

When flat is set to yes and the filename is not unique, each fetch will overwrite the existing file. Setting fail_on_missing to yes causes the task to fail if the remote file does not exist.

Test execution :

# Dry run
ansible-playbook -C copy_fetch.yaml
# Actual run
ansible-playbook copy_fetch.yaml

Verification :

# List files in the destination directory
cd /usr/local/apps/
ll

The output shows the transferred grafana-9.0.2-1.x86_64.rpm file with its size and permissions, confirming the successful fetch operation.

AutomationOperationsfetchAnsiblefile transfer
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.