How do you read a file's checksum with ansible.builtin.stat, and where in the result is it?
Run stat with checksum_algorithm, register the result, and read result.stat.checksum; every value stat returns sits under the stat key.
- name: Verify the checksum of a file
ansible.builtin.stat:
path: /path/to/file
checksum_algorithm: md5
register: result
- ansible.builtin.debug:
msg: "The checksum of the file is {{ result.stat.checksum }}"
Output: "msg": "The checksum of the file is 5f76590425303022e933c43a7f2092a3".
stat never changes anything; it reports. The registered variable holds the usual task keys plus one dictionary stat with the file's data: exists, isdir, mode, pw_name (owner), size, mtime, checksum and more. The checksum is computed by default (get_checksum: true) with SHA-1; checksum_algorithm picks another, such as sha256. MD5 is fine for spotting a change, not for security.
Note the colon: - ansible.builtin.debug: with msg: indented under it. Without the colon after debug, the YAML doesn't parse.
Go deeper:
ansible.builtin.stat module (official docs) โ all return values and
checksum_algorithm.