title: How to compare output of two different hosts in ansible playbook?
body: <pre><code>- hosts: remote_1
    tasks:
      - name: first file check
        stat:
          path: /root/abc.txt
          #get_checksum: yes
          checksum_algorithm: sha256
        register: test_file_check_1
  
      - debug:
          var: test_file_check_1.stat.checksum
        when: test_file_check_1.stat.exists
  
  - hosts: remote_2
    tasks: 
      - name: next check
        stat:
          path: /root/abc.txt
          #get_checksum: yes
          checksum_algorithm: sha256
        register: test_file_check_2
  
      - debug: 
          var: test_file_check_2.stat.checksum
        when: test_file_check_2.stat.exists
  
  - name: Block run only if file has no changes
    command: /bin/true
    when: test_file_check_1.stat.checksum != test_file_check_2.stat.checksum
  </code></pre>
  
  <p>Above code gives error for last block.
  ERROR! 'when' is not a valid attribute for a Play</p>
  
  <p>The error appears to have been in '/root/two_file_stat.yml': line 27, column 3, but may
  be elsewhere in the file depending on the exact syntax problem.</p>
  
  <p>The offending line appears to be:</p>
  
  <ul>
  <li>name: Block run only if file has no changes
  ^ here</li>
  </ul>