2017-08-07 95 views
-1

在遠程1和遠程2上有一個文件:test.txt其版本與日期和這些文件包含不固定。Ansible>文件與stat模塊比較:需要檢查文件是否具有相同的值

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

我需要檢查:

  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 

現在,如果文件中包含相同,校驗值相等,否則它不具有相同的。但我不知道解決方案。

+0

到底是什麼問題了嗎?因爲它看起來像「爲我做!」 - 請求 – techraf

+0

@techraf我可以知道文件的校驗和值,但如何檢查? –

+0

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

回答

1

所有你需要的是第二次檢查,並條件

- 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

相關問題