Ansible > file compare with stat module: 需要检查文件是否具有相同的值

Ansible > file compare with stat module: Need to check file has the same value or not

remote1 和 remote2 上有一个文件:test.txt,它的版本有日期,这些文件包含的内容不固定。

$ cat test.txt
Release_P1.11_2017-08-02-094316
02/08/2017

我需要检查:

  1. if file contains are same, then move on further tasks.
  2. if file contains are not same, then stop the tasks.
   ---
   - name: latest file check
     stat:
        path: /tmp/test.txt
        get_checksum: yes
     register: test_file_check

   - debug:
        var: test_file_check.stat.checksum

现在如果文件包含相同,则校验和值相等,否则不相同。但我没有找到解决方案。

您只需要第二次检查和何时条件

- hosts: remote_1
  tasks:
    - name: first file check
      stat:
        path: /tmp/test.txt
        get_checksum: yes
      register: test_file_check_1

...........................................
- hosts: remote_2
  tasks: 
    - name: next check
      stat:
        path: /tmp/test.txt
        get_checksum: yes
      register: test_file_check_2
...........................................

- 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

http://docs.ansible.com/ansible/latest/playbooks_conditionals.html