ansible 比较 2 个目录中的所有文件并打印差异

ansible 比较 2 个目录中的所有文件并打印差异

我有2个目录

$ tree dir{1..2}
dir1
├── file1
└── file2
dir2
├── file1
└── file2

我想使用 ansible 比较 dir1 中的所有文件与 dir2 中的所有文件并打印差异,如下所示

output:
${file1} from ${dir1}:
diff content

${file1} from ${dir2}:
diff content

它将循环遍历所有文件以打印它们的差异

下面是需要修改的 ansible 片段

---
- name: Compare files in two directories
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Find files in directory 1
      find:
        paths: ~/dir1
        file_type: file
      register: dir1_files

    - name: Find files in directory 2
      find:
        paths: ~/dir2
        file_type: file
      register: dir2_files

    - name: Compare files
      shell: diff dir1/file1 dir2/file1 ## how can I make sure path changes but filenames stay same using variables
      loop: "{{ dir1_files.files }}"
      register: diff_output
      changed_when: false
      failed_when: diff_output.rc == 1

    - name: Print differences
      debug:
        msg: |
          {{ item.item.path }} from dir1:
          {{ item.stdout }}
          {{ item.item.path }} from dir2:
          {{ item.stdout }}
      loop: "{{ diff_output.results }}"
      when: item.stdout_lines | length > 0

对于弗拉基米尔答案中的建议代码,我得到以下输出

TASK [debug] *****************************************************************************************************************************************
ok: [localhost] => {
    "msg": "file2 from dir1: \n  1,2c1,2\n  < abc123\n  < def456\n  ---\n  > abc101\n  > def111\nfile2 from dir2: \n  1,2c1,2\n  < abc123\n  < def456\n  ---\n  > abc101\n  > def111\nfile1 from dir1: \n  1,2c1,2\n  < 123abc\n  < 456def\n  ---\n  > 101abc\n  > 111def\nfile1 from dir2: \n  1,2c1,2\n  < 123abc\n  < 456def\n  ---\n  > 101abc\n  > 111def\n"
}

答案1

例如,给定文件

shell> ssh admin@test_11 ls -1 /tmp/dir[1,2]
/tmp/dir1:
file1
file2

/tmp/dir2:
file1
file2
shell> ssh admin@test_13 ls -1 /tmp/dir[1,2]
/tmp/dir1:

/tmp/dir2:

以及差异

shell> ssh admin@test_11 diff /tmp/dir1/file1 /tmp/dir2/file1
31,32d30
< User1:*:1002:1004:My User1:/home/User1:/bin/sh
< MyUser1:*:1003:1005:My User1:/home/MyUser1:/bin/sh
shell> ssh admin@test_11 diff /tmp/dir1/file2 /tmp/dir2/file2
33,34d32
< alice:*:1004:1006:Alice:/home/alice:/bin/sh
< bob:*:1005:1007:Bob:/home/bob:/bin/sh

声明变量

  dir1: /tmp/dir1
  dir2: /tmp/dir2
  dir1_files: "{{ out_dir1.files|map(attribute='path') }}"
  dir2_files: "{{ out_dir2.files|map(attribute='path') }}"

并找到文件

    - find:
        paths: "{{ dir1 }}"
        file_type: file
      register: out_dir1
    - find:
        paths: "{{ dir2 }}"
        file_type: file
      register: out_dir2

找到共享的文件。声明变量

  files_common: "{{ dir1_files|map('basename')|
                    intersect(dir2_files|map('basename')) }}"

分别给出 test_11 和 test_13 的(删节)

  files_common:
  - file1
  - file2

  files_common: []

比较共享文件。您必须忽略这些错误,因为差异回报rc=1如果文件不同

  - command: "diff {{ dir1 }}/{{ item }} {{ dir2 }}/{{ item }}"
    loop: "{{ files_common }}"
    ignore_errors: true
    register: out_diff

并创建报告

    - debug:
        msg: |
          {% for i in out_diff.results %}
          {{ i.cmd|join(' ') }}: |
            {{ i.stdout|indent(2) }}
          {% endfor %}

分别给出 test_11 和 test_13 的(删节)

  msg: |-
    diff /tmp/dir1/file1 /tmp/dir2/file1: |
      31,32d30
      < User1:*:1002:1004:My User1:/home/User1:/bin/sh
      < MyUser1:*:1003:1005:My User1:/home/MyUser1:/bin/sh
    diff /tmp/dir1/file2 /tmp/dir2/file2: |
      33,34d32
      < alice:*:1004:1006:Alice:/home/alice:/bin/sh
      < bob:*:1005:1007:Bob:/home/bob:/bin/sh

  msg: ""

用于测试的完整剧本示例

- hosts: all

  vars:

    dir1: /tmp/dir1
    dir2: /tmp/dir2
    dir1_files: "{{ out_dir1.files|map(attribute='path') }}"
    dir2_files: "{{ out_dir2.files|map(attribute='path') }}"
    files_common: "{{ dir1_files|map('basename')|
                      intersect(dir2_files|map('basename')) }}"
    
    
  tasks:

    - find:
        paths: "{{ dir1 }}"
        file_type: file
      register: out_dir1
    - find:
        paths: "{{ dir2 }}"
        file_type: file
      register: out_dir2
    - debug:
        var: files_common

    - command: "diff {{ dir1 }}/{{ item }} {{ dir2 }}/{{ item }}"
      loop: "{{ files_common }}"
      ignore_errors: true
      register: out_diff
    - debug:
        msg: |
          {% for i in out_diff.results %}
          {{ i.cmd|join(' ') }}: |
            {{ i.stdout|indent(2) }}
          {% endfor %}

问:“我不喜欢 msg 模块的输出,我更喜欢类似于 Q 在输出部分中给出的格式,可以实现吗?”

答:当然可以。尝试下面的模板。如果这不是您喜欢的,请编辑您的问题并提供您更喜欢的示例

    - debug:
        msg: |
          {% for i in out_diff.results %}
          {{ i.cmd.1|basename }} from {{ i.cmd.1|dirname }}: 
            {{ i.stdout|indent(2) }}
          {{ i.cmd.2|basename }} from {{ i.cmd.2|dirname }}: 
            {{ i.stdout|indent(2) }}
          {% endfor %}

输出的格式取决于回调。看DEFAULT_STDOUT_CALLBACK。如果你使用ansible.builtin.default回调设置 ANSIBLE_CALLBACK_RESULT_FORMAT=yaml

shell> ANSIBLE_STDOUT_CALLBACK=default ANSIBLE_CALLBACK_RESULT_FORMAT=yaml ansible-playbook pb.yml

  ...
ok: [test_11] => 
    msg: |-
        file1 from /tmp/dir1:
          31,32d30
          < User1:*:1002:1004:My User1:/home/User1:/bin/sh
          < MyUser1:*:1003:1005:My User1:/home/MyUser1:/bin/sh
        file1 from /tmp/dir2:
          31,32d30
          < User1:*:1002:1004:My User1:/home/User1:/bin/sh
          < MyUser1:*:1003:1005:My User1:/home/MyUser1:/bin/sh
        file2 from /tmp/dir1:
          33,34d32
          < alice:*:1004:1006:Alice:/home/alice:/bin/sh
          < bob:*:1005:1007:Bob:/home/bob:/bin/sh
        file2 from /tmp/dir2:
          33,34d32
          < alice:*:1004:1006:Alice:/home/alice:/bin/sh
          < bob:*:1005:1007:Bob:/home/bob:/bin/sh
ok: [test_13] => 
    msg: ""

shell> ansible-doc -t callback ansible.builtin.default

相关内容