AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible fetch Module: Download Files from Remote Hosts (Complete Guide) — Video Tutorial
Complete guide to Ansible fetch module (ansible.builtin.fetch). Download and retrieve files from remote hosts to local machine with flat, dest.
What You'll Learn
- How to copy files from remote hosts with Ansible?
- Ansible copy files from remote hosts
- Parameters
- Demo
- Conclusion
- Advanced Fetch Examples
- Download with flat directory structure
- Collect logs from all servers
- Download and verify checksum
- Fetch only if file exists
Full Tutorial Content
How to copy files from remote hosts with Ansible?
I'm going to show you a live Playbook with some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot
Ansible copy files from remote hosts
Today we're talking about Ansible module `fetch`.
The full name is `ansible.builtin.fetch` which means is part of the collection of modules "builtin" with ansible and shipped with it.
This module is pretty stable and out for years.
The purpose is to copy files from remote locations. Please note that the opposite is done by [Ansible copy module](/articles/copy-files-to-remote-hosts-ansible-module-copy).
Parameters
- `dest` _path_
- `src` _string_
- `fail_on_missing` _boolean_
- `validate_checksum` _boolean_
- `flat` _boolean_
The parameter list is pretty wide but I'll summarize the most useful.
The only required parameter is "`dest`" which specifies a directory to save the file into and the "`src`" specifies the source files in the remote hosts. It
must be a file, not a directory.
The "`fail_on_missing`" boolean is set to true so the task is going to fail if the file doesn't exist.
The file is going to be transferred and validate in the source and the destination with a checksum. If we don't want this behavior we could override with
the "validate_checksum" option.
The "`flat`" option allows you to override the default behavior of appending hostname/path/to/file to the destination.
Demo
Let's jump in a real-life playbook to copy files from remote hosts with Ansible
- _fetch.yml_
```yaml
---
- name: fetch module Playbook
hosts: all
become: true
vars:
log_file: "/var/log/messages"
dump_dir: "logs"
tasks:
- name: fetch log
ansible.builtin.fetch:
src: "{{ log_file }}"
dest: "{{ dump_dir }}"
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/copy%20files%20from%20remote%20hosts)
Conclusion
Now you know how to Copy files to remote hosts with Ansible.
Advanced Fetch Examples
Download with flat directory structure
By default, `fetch` creates a directory tree like `host/path/to/file`. Use `flat: true` for simpler output:
```yaml
- name: Download config (flat structure)
ansible.builtin.fetch:
src: /etc/nginx/nginx.conf
dest: /tmp/configs/{{ inventory_hostname }}_nginx.conf
flat: true
```
Collect logs from all servers
```yaml
- name: Fetch application logs
ansible.builtin.fetch:
src: /var/log/myapp/app.log
dest: ./collected-logs/
# Creates: ./collected-logs/server1/var/log/myapp/app.log
# ./collected-logs/server2/var/log/myapp/app.log
```
Download and verify checksum
```yaml
- name: Fetch binary with validation
ansible.builtin.fetch:
src: /opt/myapp/bin/myapp
dest: ./downloads/
validate_checksum: true # Verify file integrity after download
```
Fetch only if file exists
```yaml
- name: Check if log exists
ansible.builtin.stat:
path: /var/log/myapp/error.log
register: error_log
- n
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 7 min
- Category: troubleshooting
Read the full written article: Ansible fetch Module: Download Files from Remote Hosts (Complete Guide)
Related Video Tutorials
- Ansible fetch Module: Copy Files from Remote Hosts to Control Node — How to copy files from remote hosts with Ansible fetch module (ansible.builtin.fetch). Download logs, configs, backups from Linux and Windows.
- Ansible Copy Multiple Files: fileglob Lookup & with_fileglob Examples — Discover how to use Ansible fileglob lookup plugin and copy module to efficiently transfer multiple files to remote hosts. Explore practical examples and steps.
- Ansible win_copy Module: Copy Files to Windows Hosts (ansible.windows.win_copy) — How to copy files to Windows remote hosts with Ansible win_copy module (ansible.windows.win_copy). Transfer files, directories, set permissions.
- Ansible copy Module: Copy Files to Remote Hosts (ansible.builtin.copy Guide) — How to copy files to remote hosts with Ansible copy module (ansible.builtin.copy). Transfer files, set permissions, use content parameter, backup.
- Ansible slurp Module: Read File Content from Remote Hosts (ansible.builtin.slurp) — Complete guide to ansible.builtin.slurp module. Read and decode file content from remote hosts as base64. Practical examples with register and b64decode.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.