Ansible download_file: Download Files from URL (get_url Module Guide)
By Luca Berton · Published 2024-01-01 · Category: installation
How to download files from URL with Ansible using the get_url module (ansible.builtin.get_url). Download packages, archives, scripts with checksum validation. Practical YAML playbook examples.
Ansible download_file: Download Files from URL (get_url Module Guide)
The ansible.builtin.get_url module downloads files from HTTP, HTTPS, and FTP URLs to remote hosts. It's the Ansible equivalent of wget or curl, with built-in checksum validation, authentication, and idempotency.
Basic File Download
Key Parameters
| Parameter | Description | Example | |-----------|-------------|---------| | url | URL to download (required) | https://example.com/file.tar.gz | | dest | Destination path (required) | /tmp/file.tar.gz | | mode | File permissions | '0755' | | owner | File owner | root | | group | File group | root | | checksum | Verify file integrity | sha256:abc123... | | timeout | Download timeout (seconds) | 30 | | force | Always re-download | true | | headers | HTTP headers | {Authorization: "Bearer token"} | | url_username | HTTP basic auth username | admin | | url_password | HTTP basic auth password | secret | | validate_certs | Verify SSL certificates | true (default) | | backup | Backup existing file | true | | tmp_dest | Temporary download location | /tmp |
Download with Checksum Validation
Always verify integrity for security-critical downloads:
Download and Extract Archive
Combine get_url with unarchive:
Or download and extract in one step:
Download Scripts and Execute
Authenticated Downloads
HTTP Basic Auth
Bearer Token / Custom Headers
Conditional Downloads
Download Multiple Files
Error Handling
Proxy Configuration
get_url vs uri vs command curl
| Method | Use Case | |--------|----------| | get_url | Download files to disk (best for most cases) | | uri | API calls, REST requests (returns response body) | | command: curl | Last resort (not idempotent) |
FAQ
How do I download a file with Ansible?
Use the ansible.builtin.get_url module with url and dest parameters. It downloads from HTTP/HTTPS/FTP and supports checksum validation, authentication, and idempotent operation.
Is Ansible get_url idempotent?
Yes. By default, get_url checks if the destination file exists and has the correct size. It skips re-downloading unchanged files. Use force: true to always re-download, or checksum for content-based validation.
How do I verify downloaded file integrity in Ansible?
Use the checksum parameter: checksum: sha256:abc123.... You can also point to a checksum file URL. This ensures the downloaded file matches the expected hash before placing it.
What is the Ansible equivalent of wget?
The ansible.builtin.get_url module is the Ansible equivalent of wget and curl. It downloads files from URLs with built-in checksum validation, authentication, and proxy support.
How do I download and extract a tar.gz with Ansible?
Use get_url to download, then unarchive with remote_src: true to extract. Or use unarchive directly with an HTTP URL as src to download and extract in one step.
Conclusion
The ansible.builtin.get_url module is the standard way to download files in Ansible. Always use checksum for security-critical files, combine with unarchive for archives, and leverage retries for unreliable sources.
Related Articles • Ansible uri Module: HTTP REST API Calls • Ansible unarchive Module: Extract Archives • Ansible copy Module: Copy Files Local to Remote
Category: installation