Download a file - Ansible module get_url
How to download a tarball from URL, verify the checksum, assign some permission with Ansible


How to download a file with Ansible?
I’m going to show you a live demo 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 - <algorithm>:<checksum|url>
- force_basic_auth/url_username/url_password/use_gssapi - HTTP basic auth/GSSAPI-Kerberos
- headers dictionary - custom HTTP headers
- http_agent string - “ansible-httpget”
- owner/group/mode string - permission
- setype/seuser/selevel - SELinux
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.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
Let’s jump in a real-life playbook on how to download a file with Ansible.
- get_url.yml
---
- name: get_url module demo
hosts: all
become: false
vars:
myurl: "https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz"
mycrc: "sha256:https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz.sha"
mydest: "/home/devops"
tasks:
- name: download file
ansible.builtin.get_url:
url: "{{ myurl }}"
dest: "{{ mydest }}"
checksum: "{{ mycrc }}"
mode: '0644'
owner: devops
group: wheel
Recap
Now you know how to download a tarball, verify the checksum, assign some permission with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate