需要比较来自不同主机的文件和目录的组合校验和

Need to compare combined checksum for both files and directories from different hosts

提问人:Ashar 提问时间:11/10/2023 最后编辑:Ashar 更新时间:11/10/2023 访问量:52

问:

我试图使用 ansible 来比较集体文件而不是单个文件。cksum

我最初认为将所有文件放入一个 tar 球中并获取源主机和目标主机上 tar 文件的校验和将匹配并确认源和目标上的 tarball 中的所有文件都是匹配的。

不幸的是,我做了一个简单的练习,意识到即使文件/文件夹 cksum 匹配并且如果将它们放在 tar 文件中,tar 文件的 cksum 也永远不会匹配。

[wladmin@source_host ~]$ cksum /tmp/418.txt
30835459 24 /tmp/418.txt
[wladmin@source_host ~]$ tar -cf /tmp/418.tar /tmp/418.txt
tar: Removing leading `/' from member names
[wladmin@source_host ~]$ cksum /tmp/418.tar
4080377059 10240 /tmp/418.tar
 

[wladmin@source_host ~]$ ssh dest_host

 

$ cksum /tmp/418.txt
30835459 24 /tmp/418.txt
$ tar -cf /tmp/418.tar /tmp/418.txt
tar: Removing leading `/' from member names
$ cksum /tmp/418.tar
3804709959 10240 /tmp/418.tar

以下是我的剧本:

---
- name: "Play 1"
  hosts: localhost
  any_errors_fatal: True
  serial: 1
  gather_facts: no

  tasks:
    - add_host:
        name: "sourcehost"
        groups: source_node
        ansible_user: "linuxuser"

    - add_host:
        name: "desthost"
        groups: "dest_node"
        ansible_user: "linuxuser"

- name: "Gather facts from source_node"
  hosts: source_node

  tasks:


   - file:
       path: "~/{{ inventory_hostname }}.tar"
       state: absent
     ignore_errors: yes

   - name: Create tar file using command
     command: "tar -cvf ~/{{ inventory_hostname }}.tar {{ source_files.split(',') | join(' ') }}"
     args:
       chdir: "~"

   - name: Calculate hash for tarball on {{ source_host }}
     command: "cksum ~/{{ inventory_hostname }}.tar"
     register: srctarballhash

- name: "Configure Destination node {{ dest_node }}"
  hosts: dest_node
  any_errors_fatal: True
  gather_facts: false
  serial: 1

  tasks:

   - file:
       path: "~/Are_{{ Latest_Build_Number }}/{{ inventory_hostname }}.tar"
       state: absent
     ignore_errors: yes
 

   - name: Create tar file using command
     command: "tar -cvf ~/Are_{{ Latest_Build_Number }}/{{ inventory_hostname }}.tar {{ source_files.split(',') | join(' ') }}"
     args:
       chdir: "~"
     become: true
     become_user: "{{ copyuserondest }}"

   - name: Calculate hash for tarball on {{ source_host }}
     command: "cksum ~/Are_{{ Latest_Build_Number }}/{{ inventory_hostname }}.tar"
     register: desttarballhash
     become: true
     become_user: "{{ copyuserondest }}"

   - debug:
       msg: "source hash is {{ hostvars[groups.source_node.0].srctarballhash.stdout_lines[0] }} while destination hash is {{ desttarballhash.stdout_lines[0] }}"

我无法使用同步模块,因为 ansible 设置由不同的团队拥有,这需要时间。

我试过了 尝试过,但它不起作用,出现以下错误:tar --format=ustar -cf….

FAILED! => {"changed": true, "cmd": ["tar", "--format=ustar", "-cf", "~/REMOTEHOST.tar", "/u/marsh/UNX/scripts/testscript/test_maillist.txt"], "delta": "0:00:00.020707", "end": "2023-11-10 03:31:39.881268", "msg": "non-zero return code", "rc": 1, "start": "2023-11-10 03:31:39.860561", "stderr": "tar: a: unknown function modifier\nUsage: tar {c|r|t|u|x}[BDeEFhilmnopPTvw@/[0-7]][bf][X...] [j|z|Z] [blocksize] [tarfile] [size] [exclude-file...] {file | -I include-file | -C directory file}...", "stderr_lines": ["tar: a: unknown function modifier", "Usage: tar {c|r|t|u|x}[BDeEFhilmnopPTvw@/[0-7]][bf][X...] [j|z|Z] [blocksize] [tarfile] [size] [exclude-file...] {file | -I include-file | -C directory file}..."], "stdout": "", "stdout_lines": []}

比较源主机和目标主机上文件的 cksum 的简单方法是什么?

哈希 ansible 匹配 tar 校验和

评论


答: 暂无答案