Introduction
The fetch
module in Ansible allows you to retrieve files from remote hosts and copy them to the control machine. This module is particularly useful for gathering log files, configuration files, or any other data generated on remote hosts during playbook execution. This blog post explores the usage of the fetch
module, its parameters, and real-world examples to demonstrate its practicality in retrieving files from remote hosts.
Understanding the Concept of fetch
Module
During the execution of Ansible playbooks, remote hosts may generate log files or produce other data that can provide insights into the system’s state. The fetch
module enables you to securely collect these files from remote hosts and bring them to the control machine for analysis or further processing.
How to Use the Ansible fetch
Module
The fetch
module is straightforward to use and provides a convenient way to retrieve files from remote hosts. Let’s explore its usage through practical examples:
Syntax and Parameters
The basic syntax of the fetch
module is as follows:
1 2 3 4 5 6 7 |
- name: Fetch File from Remote Host fetch: src: " dest: " [flat]: [validate_checksum]: [remote_src]: |
The module accepts the following parameters:
src
: (required) Specifies the path of the file on the remote host that needs to be fetched.dest
: (required) Specifies the local directory path on the control machine where the file should be copied.flat
: (optional) If set toyes
, the fetched file will be copied into the destination directory without creating intermediate directories.validate_checksum
: (optional) If set toyes
, Ansible will verify the checksum of the fetched file against the remote file’s checksum to ensure its integrity.remote_src
: (optional) If set toyes
, thesrc
parameter will be treated as a URL instead of a local file path on the remote host.
Fetching a File from a Remote Host
Let’s start with a basic example of using the fetch
module to retrieve a file from a remote host:
1 2 3 4 |
- name: Fetch Log File fetch: src: "/var/log/syslog" dest: "/tmp/fetched_logs" |
In this example, the fetch
module will retrieve the file /var/log/syslog
from the remote host and copy it to the local directory /tmp/fetched_logs
.
Flattening Directory Structure
When fetching files from remote hosts, you may encounter scenarios where the source file resides in a directory structure, and you want to copy only the file without preserving the directory structure. This can be achieved using the flat
parameter:
1 2 3 4 5 |
- name: Fetch Config File fetch: src: "/etc/my_app/config.ini" dest: "/tmp/fetched_configs" flat: yes |
In this case, the fetch
module will copy the file config.ini
from the remote host’s /etc/my_app/
directory to the local directory /tmp/fetched_configs
without preserving the my_app
directory.
Validating Checksum
To ensure the integrity of the fetched file, you can use the validate_checksum
parameter:
1 2 3 4 5 |
- name: Fetch Important File fetch: src: "/opt/data/important.txt" dest: "/tmp/fetched_files" validate_checksum: yes |
In this example, Ansible will verify the checksum of the fetched file important.txt
against the remote file’s checksum, ensuring that the file is not corrupted during transfer.
Real-World Examples
Let’s explore some real-world scenarios where the fetch
module proves useful.
Example 1: Collecting Log Files
In a playbook designed to monitor multiple web servers, you may want to fetch log files generated on each server and store them on the control machine for further analysis:
1 2 3 4 5 6 7 |
- name: Fetch Log Files hosts: webservers tasks: - name: Fetch Web Server Logs fetch: src: "/var/log/apache2/access.log" dest: "/tmp/fetched_logs" |
In this example, the fetch
module will retrieve the access.log
file from the /var/log/apache2/
directory on each web server (defined in the webservers
group) and copy it to the local directory /tmp/fetched_logs
.
Example 2: Backing Up Configuration Files
Before making changes to configuration files on remote hosts, it’s a good practice to back them up. The fetch
module can help in this scenario:
1 2 3 4 5 6 7 |
- name: Backup Configuration Files hosts: application_servers tasks: - name: Fetch Config Backup fetch: src: "/etc/app_config.conf" dest: "/tmp/config_backups" |
In this case, the fetch
module will copy the app_config.conf
file from the /etc/
directory on each application server (defined in the application_servers
group) and store it in the local directory /tmp/config_backups
.
Conclusion
The Ansible fetch
module provides a convenient way to retrieve files from remote hosts and bring them to the control machine. By fetching log files, configuration files, or other generated data, you can gain insights into the system’s state and ensure efficient troubleshooting and analysis. Throughout this blog post, we explored the concept of the fetch
module, its parameters, and provided real-world examples to demonstrate its practical applications.