Ansible > 文件与 stat 模块进行比较:需要检查文件是否具有相同的值

Ift*_*fti 0 linux stat ansible devops ansible-2.x

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

$ cat test.txt
Release_P1.11_2017-08-02-094316
02/08/2017
Run Code Online (Sandbox Code Playgroud)

我需要检查:

  1. 如果文件包含相同,则继续执行进一步的任务。
  2. 如果文件包含不相同,则停止任务。
   ---
   - name: latest file check
     stat:
        path: /tmp/test.txt
        get_checksum: yes
     register: test_file_check

   - debug:
        var: test_file_check.stat.checksum
Run Code Online (Sandbox Code Playgroud)

现在,如果文件包含相同,则校验和值相等,否则不相同。但我不知道解决办法。

Ole*_*ian 5

您所需要的只是第二次检查以及何时条件

- 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
Run Code Online (Sandbox Code Playgroud)

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