Downloading Files from a Specific URL
Basic Usage of the get_url Module
To download a file from a specific URL using the get_url
module, you can use the following basic syntax:
- name: Download a file
get_url:
url: https://example.com/file.zip
dest: /path/to/local/file.zip
In this example, the url
parameter specifies the URL from which the file should be downloaded, and the dest
parameter defines the local path where the downloaded file should be stored.
Handling Authentication
If the URL requires authentication, you can provide the necessary credentials using the following parameters:
- name: Download a file with authentication
get_url:
url: https://example.com/protected-file.zip
dest: /path/to/local/file.zip
url_username: myusername
url_password: mypassword
Alternatively, you can use the force_basic_auth
parameter to force basic authentication, even if the server does not explicitly require it:
- name: Download a file with forced basic authentication
get_url:
url: https://example.com/protected-file.zip
dest: /path/to/local/file.zip
force_basic_auth: yes
Verifying File Integrity
To ensure the integrity of the downloaded file, you can use the checksum
parameter to specify the expected checksum value:
- name: Download a file and verify checksum
get_url:
url: https://example.com/file.zip
dest: /path/to/local/file.zip
checksum: sha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234
In this example, the checksum
parameter specifies the expected SHA-256 checksum for the downloaded file.
Handling Timeouts and Retries
If the download process takes a long time or encounters issues, you can configure the timeout and number of retries using the following parameters:
- name: Download a file with timeout and retries
get_url:
url: https://example.com/large-file.zip
dest: /path/to/local/file.zip
timeout: 60
retries: 3
delay: 10
In this example, the timeout
parameter sets the maximum time (in seconds) for the download to complete, the retries
parameter specifies the number of times to retry the download if it fails, and the delay
parameter sets the time (in seconds) to wait between retries.
By understanding these various options and techniques, you can effectively use the get_url
module to download files from specific URLs within your Ansible-based automation workflows.