The Ansible Fetch Module is used for copying files from remote nodes to the local machine, specifically in scenarios where the user needs to collect logs, configurations, or other data. It is the reverse of the copy module, which pushes files from the local machine to the remote nodes.
In this article, we will explore the Ansible fetch module in-depth, discuss its syntax, parameters, and use cases, and provide practical examples to demonstrate its functionality.
Why Use the Fetch Module?
The fetch module is particularly useful when working in environments where:
- Logs or configuration files from multiple remote machines need to be collected locally.
- Files need to be backed up before making changes.
- Gathering data for compliance or troubleshooting purposes.
Basic Syntax of Ansible Fetch Module
The basic syntax of the fetch
module is simple. It requires specifying the source file on the remote host and the destination on the local machine.
- name: Fetch a file from a remote host
fetch:
src: /path/to/remote/file
dest: /path/to/local/destination
flat: yes
Here are all available options:
src
: Specifies the absolute path of the file to be fetched from the remote host.dest
: Defines the local path where the fetched file will be stored. The destination must be a directory on the local machine.flat
: Ifyes
, it stores the file directly in the destination directory instead of creating a directory hierarchy based on the remote host.fail_on_missing
: Ifno
, Ansible does not fail if the file is missing on the remote host. The default isyes
.validate_checksum
: If set toyes
, it verifies the checksum of the source file to avoid redundant file transfers.
Example 1: Fetching System Logs from Remote Hosts
In this scenario, let’s assume we need to gather system logs (/var/log/syslog) from several remote hosts for analysis on our local machine.
---
- name: Fetch system logs from remote hosts
hosts: all
tasks:
- name: Fetch syslog file from remote hosts
fetch:
src: /var/log/syslog
dest: /home/ansible/logs/
flat: yes
In this example playbook:
- The
src
is the system log file on the remote machine. - The
dest
is the local path where the log files will be stored. Sinceflat: yes
, the logs will be stored directly in/home/ansible/logs/
without creating subdirectories for each remote host.
Example 2: Collecting Configuration Files from Multiple Servers
In some cases, you need to collect the /etc/nginx/nginx.conf
configuration file from multiple servers for auditing purposes.
Here is an example playbook:
---
- name: Collect Nginx configuration files
hosts: web_servers
tasks:
- name: Fetch Nginx configuration file from each server
fetch:
src: /etc/nginx/nginx.conf
dest: /home/ansible/configs/
flat: no
Here:
src
is the path to the Nginx configuration file.dest
is where the files will be stored on the local machine, but this time, we leave flat as thedefault (no)
. This means that each fetched file will be stored in a subdirectory named after the remote host.
For example, if fetching from a host called web01
, the file will be saved as /home/ansible/configs/web01/etc/nginx/nginx.conf
.
Example 3: Fetching Files Without Failing on Missing Files
In some cases, not all remote hosts may have the required file. To handle such cases, you can set fail_on_missing
to no so that the task does not fail if a file is missing on a host.
In this example, Ansible will attempt to fetch the Apache configuration file from each server, but if a file is missing on one of the hosts, the playbook will not fail.
---
- name: Fetch Apache config from remote hosts
hosts: web_servers
tasks:
- name: Fetch Apache config file, ignore if missing
fetch:
src: /etc/httpd/conf/httpd.conf
dest: /home/ansible/apache_configs/
flat: yes
fail_on_missing: no
Example 4: Fetching Large Files and Validating Checksum
When dealing with large files, you might want to ensure that the fetched file is not re-downloaded if it already exists. You can achieve this by enabling validate_checksum
.
---
- name: Fetch large database backup from remote hosts
hosts: db_servers
tasks:
- name: Fetch database backup
fetch:
src: /var/backups/db_backup.sql
dest: /home/ansible/db_backups/
flat: yes
validate_checksum: yes
In this example:
src
is the database backup file.validate_checksum: yes
ensures that the file is fetched only if it has changed, reducing unnecessary transfers.
Example 5: Backing Up Remote Files Before Making Changes
Suppose you need to make changes to configuration files on remote machines. It’s a good practice to fetch and back up those files before making any modifications.
---
- name: Back up nginx.conf before making changes
hosts: web_servers
tasks:
- name: Fetch and back up nginx.conf
fetch:
src: /etc/nginx/nginx.conf
dest: /home/ansible/nginx_backup/
flat: no
- name: Apply new Nginx configuration
copy:
src: /home/ansible/new_nginx.conf
dest: /etc/nginx/nginx.conf
In this example:
- The
fetch
module first backs up the existingnginx.conf
file from the remote hosts. - Then, the
copy
module is used to upload a new configuration file to the remote hosts.
Conclusion
This Ansible module is handy tool for retrieving files from remote hosts, providing great flexibility for various use cases like collecting logs, backing up configurations, and more.
With this guide, you should be able to confidently use the fetch module to manage your files across multiple hosts.
✍️
Author: Hitesh Jethwa has more than 15+ years of experience with Linux system administration and DevOps. He likes to explain complicated topics in easy to understand way.