我希望能够根据本地目录中的文件列表进行校验和。然后能够获取这些文件的校验和,并将其与远程系统上相同文件的校验和进行比较。
我知道我可以使用以下命令进行获取:
# Local File
- stat:
path: "{{ playbook_dir }}/roles/common/files/myfile.dat"
checksum_algorithm: sha1
delegate_to: localhost
run_once: true
register: localsha_result
# Remote file
- stat:
path: "{{ rmt_dest_dir }}/myfile.dat"
checksum_algorithm: sha1
register: sha_result
我尝试循环遍历我想要生成校验和的文件:
# Gather Files
- name: gather names of files
local_action: shell ls {{ playbook_dir }}/roles/common/files/*.dat | awk -F '/' '{ print $NF }'
register: datfiles
# Local File
- stat:
path: "{{ playbook_dir }}/roles/common/files/{{ item }}"
checksum_algorithm: sha1
with_items: "{{ datfiles.stdout_lines }}"
delegate_to: localhost
run_once: true
register: localsha_result
# Remote file
- stat:
path: "{{ rmt_dest_dir }}/{{ item }}"
checksum_algorithm: sha1
with_items: "{{ datfiles.stdout_lines }}"
register: sha_result
- name: check sha1
fail: msg="SHA1 checksum fails"
when: not sha_result.stat.checksum is defined or not sha_result.stat.checksum == "{{ item.stat.checksum }}"
with_items: "{{ datfiles.stdout_lines}}"
failed_when
非常有帮助。 非常感谢您!! - Cale