Updated May 15, 2023
Introduction to Ansible Get_Url
Ansible get_url is a module that is used to download files on remote target nodes over ftp, http, and https, in which case the remote server is accessible to the internet to make it work. There is a similar module named win_get_url to do similar work in the windows environment. These modules play a significant role when you are working on a file that is available online, and you need to take them offline. Another use can be downloading a vendor software or package from their site or repos and install on your machine. In this article, we will try to learn the behavior of this Ansible get_url module by explaining its acceptable parameters and some examples.
Explain the Ansible Get_Url
As we already saw, this is an Ansible module to download files on internet-connected remote machines. But even this small task can involve a lot of possibilities when working in a real environment. The parameters acceptable by this module cover many such possibilities. Also, some parameters have their default values already set, even if they are not mentioned in the playbook. So it becomes more important to learn about such parameters, acceptable values, and default values. Also as return values are as equally important as parameters are. So we will list some important, if not all, parameters of this module.
- validate_certs: –acceptable values are yes and no. The default is yes. Used on your controlled sites for self-signed certificate validation before download.
- use_proxy: –acceptable values are yes and no. The default is yes. To use a proxy.
- url_username: –For http basic authentication, this username will be used.
- url_password: –For http basic authentication, this password is used along with the value mentioned in
- url: –file location over http/https/ftp. The format of it can be like below: –
<http/https/ftp>://<user<:pass]>>@<website_location><:port>/<filepath>
- tmp_dest: Where the file will be downloaded too.
- timeout: Default is 10 seconds. Timeout for URL request.
- sha256sum: When it is provided, then the digest of the downloaded file is calculated and checked against it to verify successful download and file integrity.
- seuser, serole, setype, selevel: These are used to set the SELinux file context.
- owner: Similar to chown, to set the owner of the file.
- mode: To set the permission of the target file or directory. A better practice is to use 4 octal numbers inside single quotes to represent the permission like ‘0777’ or ‘0644’.
- group: This is used to set the group ownership of a file or directory.
- force: Acceptable values are yes and no. Default is no. when this is yes and destination is not a directory but an existing file, then it will replace any existing file if any content looks changed. When it is no, then it will download only when the destination does not exist.
- dest: This is the path where the file will get download.
- checksum: When provided, the digest of the downloaded file is calculated and checked against it to verify successful download and file integrity. Its format is as below: –
<algorithm>:<url| checksum>
- client_cert: This is a pem format certificate chain file for SSL client authentication.
- client_key: This is a pem format private key for SSL client authentication.
- attributes: To set the attributes of the downloaded file. The acceptable flags are the same as chatter. This can be seen by using
- backup: Acceptable values are yes and no. To create a backup of an existing file in case of replacing
Also, please have a look on the below return values. This will make you understand the output when you have executed the playbook in verbose mode.
- url: Requested URL.
- uid: Owner id of file.
- status_code: HTTP status code.
- state: State of the target.
- src: The source file which is used after download.
- size: Downloaded file size.
- secontext: SELinux security context
- owner: File owner.
- msg: HTTP request message.
- mode: File permission.
- md5sum: After download md5 checksum.
- group: File’ s group ownership.
- gid: File gid.
- elapsed: Time elapsed from start to end.
- checksum_src: Sha1sum of the file at the source.
- checksum_dest: Sha1sum of the file at the destination.
- dest: Path where downloaded.
- backup_file: Backup file name if any created.
How Does Ansible Get_Url Works?
Ansible get_url module can be used on the command line with ansible command, or it can be mentioned in playbooks which are then executed by the ansible-playbook command. This module has similar syntax like other Ansible modules; a sample is given below: –
- name: <some comment on this task>
get_url:
<key2 or parameter2>: <value2>
<key2 or parameter2>: <value2>
...
Examples of Ansible Get_Url
Now by using examples, we will try to learn about the Ansible file module, which you might have to use in day-to-day operations. We will take some examples, but before going there, we first understand our lab, we used for testing purpose which contains an ansible-controller and two targets named host-one and host-two. Then, we will create playbooks and run Ansible commands on the ansible-controller node and see the results on remote hosts.
Example #1
Here we have a playbook like below, where we are simply trying to download a file from the internet on remote machines.
Code:
---
- hosts: all
tasks:
- name: Here we are simply downloding a file from internet.
get_url:
url: http://mirror.centos.org/centos/7/os/x86_64/Packages/apache-commons-cli-1.2-13.el7.noarch.rpm
dest: /var/tmp
mode: '0755'
Output:
ansible-playbook ansible_get_url.yaml -vv
Example #2
In this example, we have a playbook like below where we are checking sha256sum and set the owner, group, and permission for the downloaded file.
Code:
---
- hosts: all
gather_facts: no
tasks:
- name: Here we download the file, check checksum and set mode as well as ownership.
get_url:
url: "http://mirror.centos.org/centos/7/os/x86_64/Packages/vim-minimal-7.4.629-6.el7.x86_64.rpm"
dest: /var/tmp/
mode: '0700'
owner: testuser
group: testgroup
tmp_dest: /tmp/
checksum: sha256:B76A8EEA8F48EA317507990E323DA3602F683EDB2D71935E761F0EC12114B711
Output:
ansible-playbook ansible_get_url_file_parameters.yaml -vv
Also, we shall check the downloaded file on target hosts, to do this, we can run a for loop lie below:
for i in {host-one,host-two}; do ssh $i "hostname;ls -l /var/tmp/vim-minimal-7.4.629-6.el7.x86_64.rpm";done
Output:
Conclusion
As we saw in this article, Ansible get_url is an important module, especially when your remote target machines are connected to the internet and your needs, be it documents, configuration file software packages, are available online only. So you should have good knowledge of it, at least what are possibilities when using it. So learn it first and then use it.
Recommended Articles
This is a guide to Ansible get_url. Here we discuss How Does Ansible Get_Url Works and Examples along with codes and outputs. You may also have a look at the following articles to learn more –