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.
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: yesWhen 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.yamlVerification :
# List files in the destination directory
cd /usr/local/apps/
llThe output shows the transferred grafana-9.0.2-1.x86_64.rpm file with its size and permissions, confirming the successful fetch operation.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.