0

I have stored checksum of local and remote files in two different variables. Now I want to compare those checksum and fail if they don't match. Below is code.

   - name: Get cksum some of files copied locally
     stat:
       path : "{{ item.src }}/{{ item.file }}"
       checksum_algorithm: sha1
     delegate_to: localhost
     with_items: "{{ files }}"
     register: local_files

   - name: Get cksum of remote files 
    stat:
      path : "{{ item.dest }}/{{ item.file }}_{{ item.package }}_NEW"
      checksum_algorithm: sha1
    with_items: "{{ files }}"
    register: remote_files

  - name : Compare local and remote cksums. Fail if not matched
    debug:
      msg="Checksum don't match"
    failed_when:  item[0].results.stat.checksum !=  item[1].results.stat.checksum
    with_items:
      - "{{ local_files.results }}"
      - "{{ remote_files.results }}"

When I run this, I get below error.

FAILED! => {"failed": true, "msg": "The conditional check 'item[0].results.stat.checksum !=  item[1].results.stat.checksum' failed. The error was: error while evaluating conditional (item[0].results.stat.checksum !=  item[1].results.stat.checksum): dict object has no element 0"}

How can I correct it to compare checksum?

2
  • You already know debug module ー start with checking what item[0] and item[1] contain to see how far you are from what you imagined. Commented May 25, 2018 at 6:56
  • And this is way too broad for StackOverflow. Define your problem first: you have two lists of arrays, from which you need to extract some of the key-value pairs (because they contain more differing information than you want), and then to compare these two resulting lists - likely with set-theory filters. All this is already covered in separate questions on SO. Commented May 25, 2018 at 7:20

1 Answer 1

3

Do this:

- name: Get cksum some of files copied locally
  stat:
    path : "{{ item.src }}/{{ item.file }}"
  delegate_to: localhost
  with_items: "{{ files }}"
  register: local_files

- name: Get cksum of remote files 
  stat:
    path : "{{ item.dest }}/{{ item.file }}"
  with_items: "{{ files }}"
  register: remote_files

- name: Compare local and remote cksums. Fail if not matched
  fail:
    msg: "Checksum don't match"
  when:  item[0].stat.checksum != item[1].stat.checksum
  with_together:
    - "{{ local_files.results }}"
    - "{{ remote_files.results }}"
Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.