cancel
Showing results for 
Search instead for 
Did you mean: 
Kailas
Cadet
Cadet
  • 985 Views

Error in playbook to check files checusum

I have below file whcih has some files md5sum checksum and this file i am calling in playbook as a variable to cehck given files checksum/integrity.

# cat verify.yml
file_list:
- file1:
checksum: 4885c805b7c2da6dc7bdb407997eade1
name: /root/inventory
- file2:
checksum: c06a058db27a3637d7e606758f0aad01
name: /root/ansible.cfg
- file2:
checksum: d41d8cd98f00b204e9800998ecf8427e
name: /root/test

#########################################################

And below is my playbook which i am tryingt to run but its gives error like  below:

ansible-playbook checksum.yml

PLAY [play to check integrity of files] *********************************************************************************************************************

TASK [Check given file checksums] ***************************************************************************************************************************
skipping: [localhost]

TASK [print and verify] *************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "'stat_results' is undefined"}

PLAY RECAP **************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=1 rescued=0 ignored=0

 

 

cat checksum.yml

---
- name: play to check integrity of files
hosts: localhost
vars_files:
- verify.yml
tasks:
- name: Check given file checksums
stat:
path: "{{ item.file }}"
loop: "{{ stat_results.results }}"
register: stat_results.results

- name: print and verify
debug:
msg: "Checksum PASS: {{ file_list }}"
when: item.stat.checksum == item.item.checksum.split()[0]
loop: "{{ stat_results.results }}"
loop_control:
label: "{{ item.stat.path }}"

- name: print and verify
debug:
msg: "Checksum FAIL: {{ file_list }}"
when: item.stat.checksum != item.item.checksum.split()[0]
loop: "{{ stat_results.results }}"
loop_control:
label: "{{ item.stat.path }}"

 

Can some cehck this please and let me know where i am making mistake.

 

 

0 Kudos
3 Replies
RajveerSingh
Mission Specialist
Mission Specialist
  • 956 Views

In first task, you are looping it with variable stat_results.results which is undefined. you loop should have variable defined in verify.yml

0 Kudos
Kailas
Cadet
Cadet
  • 949 Views

many Thanks Rajveer . Actually i am new to ansible so not sure . As i have give my variable file can you please tell me what exacltly i can define in verify.yml. Appreciate your response  

 

0 Kudos
RajveerSingh
Mission Specialist
Mission Specialist
  • 940 Views

Hi Kailash,

I'll always suggest to experiment with basic stuff and build the logic based on the understanding.  Below example can be used as reference on how to loop your verify.yml variable values which is list of dictionaries:-
- name: Check given file checksums
  stat:
    path: "{{ item.name }}"
  register: stat_results
  loop: "{{ file_list | flatten(levels=1) }}"

0 Kudos
Join the discussion
You must log in to join this conversation.