Ansible get_url Module: Download Files from URLs (ansible.builtin.get_url Guide)
By Luca Berton · Published 2024-01-01 · Category: installation
Complete guide to Ansible get_url module (ansible.builtin.get_url). Download files from HTTP, HTTPS, and FTP URLs with checksum verification and authentication.
How to download a file with Ansible? I'm going to show you a live Playbook and some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible download a file Today we're talking about the Ansible module get_url. The full name is ansible.builtin.get_url, which means that is part of the collection of modules "builtin" with ansible and shipped with it, part of ansible-core. It's a module pretty stable and out for years. It works in a different variety of operating systems. It downloads files from HTTP, HTTPS, or FTP to node For Windows targets, use the ansible.windows.win_get_url module instead.
Main Parameters
• url string - URL
• dest string - path
• force string - no/yes
• checksum string - \
This module has some parameters to perform any tasks. The two required parameters are url and dest. The url parameter specifies the URL of the resource you're going to download. The dest parameter specifies the filesystem path where the resource is going to be saved on the target node. Let's deep dive in the "force" parameter. If "dest" is a file, Ansible is going to download every time the file. If "dest" is a directory the default behavior is not to replace a file, until you toggle to yes the force parameter. The parameter "checksum" is very useful to validate the consistency of the downloaded file. You could specify the algorithm, usually sha1 or sha256, and directly the checksum or a URL for checksum. You might need the third-party hashlib library for access to additional algorithms. Another interesting parameter is "headers" which allow you to specify some custom HTTP headers. Ansible presents himself as "ansible-httpget" in the web server logs, but you cust customize it in the "http_agent" parameter. There are some additional parameters for authentication for example to handle HTTP basic authentication with username and password or for more complex GSSAPI-Kerberos scenarios using httplib2 Python library. Let me also highlight that we could also specify the permission and SELinux properties.
## Playbook Let's jump in a real-life playbook on how to download a file with Ansible. • get_url.yml
Conclusion Now you know how to download a tarball, verify the checksum, assign some permission with Ansible.
Download Examples
Download with checksum
Download with authentication
Download with retries
Download only if changed (conditional)
Download through proxy
Complete Install Pattern
Key Parameters
| Parameter | Description | |-----------|-------------| | url | Download URL | | dest | Local destination path | | checksum | Verify integrity (sha256:xxx) | | mode | File permissions | | force | Re-download if exists | | timeout | Connection timeout (seconds) | | validate_certs | Verify SSL (default: true) | | headers | Custom HTTP headers | | url_username/password | HTTP authentication |
get_url vs uri vs fetch
| Module | Direction | Use Case | |--------|-----------|----------| | get_url | URL to remote | Download files | | uri | URL to controller | API calls, small responses | | fetch | Remote to controller | Copy files from remote | | copy | Controller to remote | Upload local files |
FAQ
How do I download to the controller?
Use delegate_to: localhost:
Why does it re-download every time?
Check force parameter. Also, the module compares checksums - if the remote file changed, it downloads again.
How do I handle redirects?
get_url follows redirects automatically (up to 10 by default).
Basic Download
With Checksum Verification
Set Permissions
Authentication
Download Only If Missing
Download and Extract
Timeout and Retries
Key Parameters
| Parameter | Description | |-----------|-------------| | url | Download URL | | dest | Destination path | | checksum | Verify integrity | | mode | File permissions | | owner/group | File ownership | | force | Re-download even if exists | | timeout | Request timeout (seconds) | | url_username | Basic auth user | | url_password | Basic auth password | | headers | Custom HTTP headers | | validate_certs | SSL verification | | tmp_dest | Temp download location | | backup | Backup existing file |
get_url vs uri
| Feature | get_url | uri | |---------|---------|-----| | Purpose | Download files | HTTP requests | | Saves to disk | ✅ | Optional | | Checksum verify | ✅ | ❌ | | POST/PUT/DELETE | ❌ | ✅ | | File permissions | ✅ | ❌ |
FAQ
How do I download from S3?
Use amazon.aws.aws_s3 module instead — it handles authentication properly.
"Certificate verify failed"?
Can I download to the controller?
Use delegate_to: localhost:
Basic Download
With Checksum Verification
Download with Permissions
Authentication
Conditional Download
Download from GitHub Release
Download Then Extract
Timeout and Retries
get_url vs uri vs unarchive
| Module | Best For | |--------|----------| | get_url | Download files to disk | | uri | API calls, return content in variable | | unarchive | Download + extract (with src: URL) |
FAQ
How to skip download if file exists?
force: false (default) skips if dest exists. Use checksum to re-download if content changed.
Self-signed certificates?
How to download multiple files?
Related Articles • Ansible Become Guide • Ansible for Windows Guide
Category: installation
Watch the video: Ansible get_url Module: Download Files from URLs (ansible.builtin.get_url Guide) — Video Tutorial