Ansible is an open-source tool for configuration management, application deployment, and task automation.
Ansible is powered by a variety of modules that provides a ton of functionalities for automating the tasks. For example, you will find the modules for copying the files, SSH, package management, and more.
One of the many modules provided by Ansible is the fetch module. This module lets us fetch the files from remote nodes to the local machine.
You can think of the fetch module as the opposite of the copy module which allows us to push the files from a local machine into a remote node.
The goal of this post is to explore the fetch module in Ansible. We start by discussing what it does, its various standard parameter, and finally explore the practical examples on how to use it.
What Is the Ansible Fetch Module?
As we mentioned, the fetch module is essentially the inverse of the Ansible copy module.
Hence, the fetch module retrieves the files from the managed nodes to the control machine. This can be extremely useful when you need to backup the configuration files, logs, or any other files from a remote host to the local machine.
Module Parameters
The following are some key parameters that you need to interact with when working with the Ansible fetch module:
Src – This parameter specifies the file that you wish to fetch on the remote system. This must be a file and not a directory. The fetch module does not allow you to copy the files in a directory recursively.
Dest – It specifies a directory on the control node where the files will be saved. The module copies the file with the same name to the destination directory.
Fail_on_missing – This is a Boolean parameter that specifies whether the operation will fail if the target file is missing on the remote host. By default, the value is set to true.
Flat – This is also a Boolean parameter that specifies whether the file will be placed directly in the destination without replacing the directory structure. This is especially useful when you need to fetch the files from multiple hosts.
validate_checksum – If it is set to yes, this module verifies the checksum of the file on the remote machine to the fetched file to ensure the data integrity.
Practical Examples:
Let us explore the various examples of using the ansible fetch module.
Example 1: Basic Example
The following is a basic example that demonstrates how to use the fetch module to download a file from the remote host to the local controller node:
- hosts: web_servers
tasks:
- name: Fetch nginx configuration from servers
fetch:
src: /etc/nginx/nginx.conf
dest: /tmp/
The given example playbook fetches the “nginx.conf” from the “web_servers” group of hosts and saves it in the /tmp/ directory on the control machine.
Example 2: Using the Flat Parameter
We can also use the flat parameter to avoid a directory restructuring as shown in the following example playbook:
- hosts: web_servers
tasks:
- name: Fetch logs without directory structure
fetch:
src: /var/log/nginx/access.log
dest: /tmp/nginx-logs/
flat: yes
In this example, we use the fetch module to download the “access.log” from the “web_servers” group and save it in /tmp/nginx-logs/ without replicating the directory structure.
If fetching from multiple hosts, each file overwrites the previous file unless we use the templating like a Jinja2 expression to make the filename unique per host.
Example 3: File Integrity by Validating the Checksum
To verify the file integrity, we can compare the checksum of the “src” and “dest” files as shown in the following example:
- hosts: web_servers
tasks:
- name: Fetch files with checksum validation
fetch:
src: /var/log/nginx/error.log
dest: /tmp/nginx-logs/
validate_checksum: yes
This playbook tells the fetch module to validate the checksum of the fetched “error.log” to ensure the data integrity.
Conclusion
You learned the role of the Ansible fetch module. The fetch module is essential to Ansible’s toolkit to manage the files across your infrastructure.