簡體   English   中英

如何比較 Ansible 中的多個文件

[英]How to compare multiple files in Ansible

我正在嘗試找到一種方法來將目錄中的多個文件與另一個目錄中的文件進行比較。 例如 - 目錄 /home/ansible/properties/ 中有 10 個文件,遠程主機上的另一個目錄(如 /opt/apps/properties/)中有類似的文件集。 我想比較它們中的每一個,可能使用校驗和,即使其中一個文件被更改,然后做一些任務 -

我的劇本下面可以比較一個文件,但是有沒有辦法比較所有文件或目錄?

- name: Get checksum of my First file
  stat:
    path : "/home/ansible/properties/my.properties"
  register: myfirstfile

- name: Current SHA1
  set_fact:
    1stFilechecksum: "{{ myfirstfile.stat.checksum }}"

- name: Get checksum of my Second File
  stat:
    path : "/opt/aaps/properties/my.properties"
  register: mysecondfile

- name: Current SHA1
  set_fact:
    2ndFilechecksum: "{{ mysecondfile.stat.checksum }}"

- name: Comparing 2 files
  debug:
    msg: "Do Something....."
  when:  1stFilechecksum != 2ndFilechecksum

根據您的描述測試文件樹

……或者至少我從中了解到的。

/tmp/test/a          /tmp/test/b
├── file10.txt       ├── file10.txt
├── file1.txt        ├── file1.txt
├── file2.txt        ├── file2.txt
├── file3.txt        ├── file3.txt
├── file4.txt        ├── file4.txt
├── file5.txt        ├── file5.txt
├── file6.txt        ├── file6.txt
├── file7.txt        ├── file7.txt
├── file8.txt        ├── file8.txt
├── file9.txt        ├── file9.txt
└── toto.txt         └── titi.txt

如您所見,所有文件都是通用的,除了每個目錄中的文件。 這些將被排除在比較之外。 對於通用文件,除了file2.txtfile5.txt具有相同的內容外,其他文件都是不同的。

用於解決方案的鹼基

  • 我們使用find循環搜索兩個目錄中的所有文件。 a中的所有文件都在results[0]中, b中的所有文件都在results[1]中。 get_checksum選項收集我們需要的信息。
  • 在下一個任務中:
    • 我們循環a .
    • 在每次迭代中,我們通過在替換 dir 路徑后使用selectattr僅選擇具有相同path名的列表元素來計算b中的相應文件。 保留結果的第一個元素給出了我們的文件。 如果結果為空(不是公共文件),我們默認為空 object
    • when 子句檢查文件是否存在(常見的我們跳過)以及它們是否不同(校驗和不相等)
    • loop_control用於自定義循環變量名稱以提高可讀性(即 item => file1)並將循環項目的 output 限制為可以理解的內容。

劇本

---
- name: compare files in two dirs
  hosts: localhost
  gather_facts: false

  vars:
    dir1: /tmp/test/a
    dir2: /tmp/test/b

  tasks:

    - name: "Find all files in {{ dir1 }} and {{ dir2 }}"
      find:
        paths:
          - "{{ item }}"
        get_checksum: true
      register: search_files
      loop:
        - "{{ dir1 }}"
        - "{{ dir2 }}"


    - name: "Compare checksums from {{ dir1 }} to {{ dir2 }}. do something if different"
      vars:
        file2: >-
          {{
            search_files.results[1].files |
            selectattr('path', 'eq', file1.path | replace(dir1, dir2)) |
            list |
            first |
            default({})
          }}
      debug:
        msg: "Files differ. Do something"
      loop: "{{ search_files.results[0].files }}"
      loop_control:
        loop_var: file1
        label: "Comparing {{ file1.path }} to {{ file2.path | default('N/A') }}"
      when:
        - file2.path is defined
        - file1.checksum != file2.checksum

結果

$ ansible-playbook test.yml 

PLAY [compare files in two dirs] ***************************************************************************************************************

TASK [Find all files in /tmp/test/a and /tmp/test/b] *******************************************************************************************
ok: [localhost] => (item=/tmp/test/a)
ok: [localhost] => (item=/tmp/test/b)

TASK [Compare checksums from /tmp/test/a to /tmp/test/b. do something if different] ************************************************************
ok: [localhost] => (item=Comparing /tmp/test/a/file1.txt to /tmp/test/b/file1.txt) => {
    "msg": "Files differ. Do something"
}
skipping: [localhost] => (item=Comparing /tmp/test/a/file2.txt to /tmp/test/b/file2.txt) 
ok: [localhost] => (item=Comparing /tmp/test/a/file3.txt to /tmp/test/b/file3.txt) => {
    "msg": "Files differ. Do something"
}
ok: [localhost] => (item=Comparing /tmp/test/a/file4.txt to /tmp/test/b/file4.txt) => {
    "msg": "Files differ. Do something"
}
skipping: [localhost] => (item=Comparing /tmp/test/a/file5.txt to /tmp/test/b/file5.txt) 
ok: [localhost] => (item=Comparing /tmp/test/a/file6.txt to /tmp/test/b/file6.txt) => {
    "msg": "Files differ. Do something"
}
ok: [localhost] => (item=Comparing /tmp/test/a/file7.txt to /tmp/test/b/file7.txt) => {
    "msg": "Files differ. Do something"
}
ok: [localhost] => (item=Comparing /tmp/test/a/file8.txt to /tmp/test/b/file8.txt) => {
    "msg": "Files differ. Do something"
}
ok: [localhost] => (item=Comparing /tmp/test/a/file9.txt to /tmp/test/b/file9.txt) => {
    "msg": "Files differ. Do something"
}
ok: [localhost] => (item=Comparing /tmp/test/a/file10.txt to /tmp/test/b/file10.txt) => {
    "msg": "Files differ. Do something"
}
skipping: [localhost] => (item=Comparing /tmp/test/a/toto.txt to N/A) 

PLAY RECAP *************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM