bluesky
bluesky

Reputation: 11

Ansible check sha1sum

Have tryied to check sha1sum, but don't think is the right way to do it.

- name: "Check Sha1sum"
  stat:
    path: "/path/to/my/package.tar.gz"
    checksum_algorithm: sha1
    get_checksum: yes

Would check file sha1sum and compare the read sha1sum with a variable, is that possible?

Upvotes: 0

Views: 971

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68144

Q: "Check file sha1sum and compare the read sha1sum with a variable."

A: For example, the play below will fail if the checksum of the file doesn't match the checksum stored in the variable test_sha1

shell> sha1sum /etc/passwd
7c73e9f589ca1f0a1372aa4cd6944feec459c4a8  /etc/passwd
- hosts: localhost
  vars:
    test_sha1: 7c73e9f589ca1f0a1372aa4cd6944feec459c4a8
  tasks:
    - stat:
        path: /etc/passwd
      register: result
    - debug:
        var: result.stat.checksum
    - assert: 
        that: test_sha1 == result.stat.checksum
        fail_msg: '[ERR] Checkum failed.'

gives (abridged)

TASK [stat] ****
ok: [localhost]

TASK [debug] ****
ok: [localhost] => 
  result.stat.checksum: 7c73e9f589ca1f0a1372aa4cd6944feec459c4a8

TASK [assert] ****
ok: [localhost] => changed=false 
  msg: All assertions passed

  • In stats checksum_algorithm=sha1 and get_checksum=yes are defaults.

  • The checksum of the file will be returned in the attribute stat.checksum.

  • See assert.

Upvotes: 2

Related Questions