Verify File Integrity With Checksum
In this step, you will use the Ansible get_url module to verify the integrity of the downloaded file using a checksum.
First, modify the existing playbook file by removing all content and adding the following content to the playbook file:
- hosts: localhost
tasks:
- name: Download a file and verify checksum
get_url:
url: "https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz"
dest: "/tmp/ansible-2.9.25-2.tar.gz"
checksum: "sha256:https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz.sha"
get_url
: This is a module in Ansible used to download files from the internet.
url
: This parameter specifies the URL from which to download the file. In this case, it's downloading Ansible
version 2.9.25 from the official Ansible releases.
dest
: This parameter specifies the destination path on the target host where the downloaded file will be saved. In this case, it's saving the file to /tmp/ansible-2.9.25-2.tar.gz
.
checksum
: This parameter is used to specify the checksum of the file being downloaded, along with its type. In this case, it's set to sha256:https://releases.ansible.com/ansible/ansible-2.9.25.tar.gz.sha
, indicating that Ansible should verify the SHA256
checksum of the downloaded file against the checksum provided at the specified URL. This ensures the integrity of the downloaded file.
In summary, this playbook will download the Ansible version 2.9.25 tarball from the specified URL, save it as ansible-2.9.25-2.tar.gz
in the /tmp
directory on the local host, and verify its integrity by comparing its SHA256
checksum with the checksum provided at the specified URL.
Then, run the playbook with the following command:
ansible-playbook get_url-module-playbook.yaml
Observe the output to see if the file was successfully downloaded and saved on the remote host, and check if the checksum verification passed.
Example output:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Download a file and verify checksum] *************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Verify that the file has been successfully downloaded and saved on the remote host.
ll /tmp/ansible-2.9.25-2.tar.gz
Example output:
-rw-rw-r-- 1 labex labex 14M Mar 15 13:31 /tmp/ansible-2.9.25-2.tar.gz