Check authenticity of file in ansible

Viewed 9561

I have ansible role that downloads a script file, how can i check the authenticity of the file using md5sum before executing?

- name: Add xx official repository for ubuntu/debain
  get_url:
     url:  https://script.deb.sh
     dest: /opt/script.db.sh

- name: Execute the script
  script: /opt/script.db.sh
  • i want to check the authenticity before downloading the file - can this be achieved in ansible?
3 Answers

you can use the "checksum" parameter "get_url" module. I show you an example of a playbook that executes a "role" to download OpenJDK8 only if the md5sum is correct.

File: playbook.yml

---
- name: "Download binaries"
  hosts: localhost
  roles:
  - openjdk

File: openjdk/tasks/main.yml

- name: "Download OpenJDK {{ openjdk_version }} binaries"
  get_url:
    url: https://download.java.net/openjdk/jdk8u40/ri/{{ openjdk_file }}
    dest: "{{ download_destination }}"
    checksum: "{{ openjdk_md5 }}"
    mode: 0750
  tags:
    - always

File: openjdk/vars/main.yml

---
download_destination: /var/tmp
openjdk_version: "8u40-b25"
openjdk_file: "openjdk-{{ openjdk_version }}-linux-x64-10_feb_2015.tar.gz"
openjdk_md5: "md5: 4980716637f353cfb27467d57f2faf9b"

The available cryptographic algorithms in Ansible 2.7 are: sha1, sha224, sha384, sha256, sha512, md5.

It works for me, I hope for you too.

Related