Score:0

Ansible playbook to read vars from plaintext file and log file checksums

mn flag

In the process of learning ansible I am trying to create a playbook to run on some email hosts to gather sha1 hashes of the files. I have a plaintext file with the path and filenames the playbook needs to check.

It is formatted as follows: files2.txt:

/opt/zimbra/zimlets-deployed/com_zextras_drive_open/assets/refresh.png
/opt/zimbra/zimlets-deployed/com_zextras_drive_open/assets/icon.png
/opt/zimbra/zimlets-deployed/com_zextras_drive_open/assets/sad-face.png
/opt/zimbra/zimlets-deployed/com_zextras_drive_open/com_zextras_drive_open.properties
/opt/zimbra/zimlets-deployed/com_zextras_drive_open/ZimbraDrive.template.js

Now I think I need to use the lookup directive with something like:

---
- hosts: localhost
  gather_facts: no
  vars:
     contents: "{{ lookup('file', '/home/ansible/ansible/files2.txt') }}"

If I do - debug: msg="the value of zcs-files is {{ contents }}"

I see the contents. How can I filter this to only the file name and the checksum?

I also think I need to use stat to get the current file hash, and loop through the file above.

My goal with this playbook, is to gather the sha1 checksum of the files specified in the files2.txt file above, then log the matching hashes, and log the mismatch file hashes to the local control node.

Any guidance would be greatly appreciated. Here is what I have so far, but I don't know if any of this is correct.

---
- hosts: mail
  become: true
  vars_files:
    - files.yml
  tasks:
    - name: Use sha1 to calculate checksum
      stat:
        path: "{{ item.name }}"
        checksum_algorithm: sha1
        get_checksum: yes
      register: sha
      loop: "{{ files }}"

    - set_fact:
        path_hash: "{{ dict(_path|zip(_hash)) }}"
      vars:
        _path: "{{ hash_check.results|map(attribute='stat.path')|list }}"
        _hash: "{{ hash_check.results|map(attribute='stat.checksum')|list }}"

I know I am probably way off on this, but I am trying to learn.

Thank you

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.