The get_url modules allows Ansible to download files from specified URLs and save them to a local or remote machine. This is particularly useful when working with remote servers, as it eliminates the need to manually download and transfer files.
Key benefits include:
- Direct downloads from public and private URLs.
- Support for checksum verification (an integrity check).
- Flexibility in handling HTTP headers, proxies, and other request parameters.
In this article, we will dive deep into the Ansible get_url module with examples.
Table of Contents
Basic Syntax
Hre’s the basic code syntax:
- name: Download a file using get_url
hosts: all
tasks:
- name: Download file to destination
ansible.builtin.get_url:
url: URL
dest: destination_path
checksum: checksum_value
mode: permissions
owner: file_owner
group: file_group
Here’s the explanation:
- url: The URL from which the file can be retrieved (must be provided as a string).
- dest: The directory path where the file should be saved.
- checksum: Expected checksum of the file for validation.
- mode: Permissions for the file.
- owner and group: File ownership details.
Example 1: Downloading a File to a Remote Host
This example demonstrates downloading a publicly available file to a remote host.
- name: Download a file to a remote host
hosts: webservers
tasks:
- name: Download file from URL
ansible.builtin.get_url:
url: https://example.com/sample.zip
dest: /tmp/sample.zip
In this example playbook:
- The url parameter specifies the file’s source URL.
- The dest parameter specifies the destination path on the target host.
Example 2: Downloading a File with Checksum Verification
Adding checksum validation ensures that the downloaded file matches the expected integrity.
- name: Download a file with checksum validation
hosts: webservers
tasks:
- name: Download file and verify checksum
ansible.builtin.get_url:
url: https://example.com/sample.zip
dest: /tmp/sample.zip
checksum: "sha256:3b8c33fd6d8985f82c2c1e59ab17d885a3ad5670c3e21968e2e98b840db8ed9b"
In this example:
- The checksum parameter ensures that the file integrity is verified. An error occurs if the checksum doesn’t match.
Example 3: Downloading a File with HTTP Authentication
In this example, we download a file from a secured server using HTTP Basic Authentication.
- name: Download a file with authentication
hosts: webservers
tasks:
- name: Download secure file
ansible.builtin.get_url:
url: https://secure.example.com/secure-file.zip
dest: /opt/secure-file.zip
url_username: user123
url_password: pass123
Here:
- The url_username and url_password parameters provide basic authentication.
Example 4: Skipping Download if the File Exists
To avoid redundant downloads, set the force parameter to no.
- name: Avoid redundant downloads
hosts: webservers
tasks:
- name: Download only if file is missing
ansible.builtin.get_url:
url: https://example.com/sample.zip
dest: /tmp/sample.zip
force: no
In the above example:
- The force: no parameter ensures the file is only downloaded if it does not exist.
Example 5: Using Proxies for Downloads
This example demonstrates downloading a file via an HTTP proxy.
- name: Download file using a proxy
hosts: webservers
tasks:
- name: Download via proxy
ansible.builtin.get_url:
url: https://example.com/sample.zip
dest: /tmp/sample.zip
http_proxy: http://proxy.example.com:3128
validate_certs: no
In this playbook:
- The http_proxy parameter specifies the proxy server.
- The validate_certs: no parameter disables certificate validation for untrusted proxies.
Conclusion
The get_url module is an essential feature in Ansible for automating file downloads. By leveraging its features like authentication, checksum validation, and proxy support, you can ensure secure and efficient file transfers in your automation workflows.
To enhance your playbooks, consider using:
- failed_when to define custom failure conditions
- loop to iterate over multiple URLs
- debug to troubleshoot and verify values
- ignore_errors to allow continued execution despite task failures.
FAQs
1. How can I download multiple files using the get_url module?
You can loop through a list of URLs using Ansible's with_items loop to download multiple files.
2. Is it possible to retry a failed download using get_url?
Yes, you can use Ansible's retries and delay parameters with the get_url task to attempt multiple retries.
3. Can I use custom headers in the get_url module?
Yes, you can pass custom headers to the request using the headers parameter.