使用ansible playbook 实现检查目标主机的文件状态并校验md5值
【摘要】 使用ansible playbook 实现检查目标主机的文件状态并校验md5值以下内容使用Cursor AI自动生成YAML。---- name: 检查文件是否存在并校验MD5 hosts: wlsm gather_facts: false vars: file_path: "/home/data/Config.json" # 请替换为实际的文件路径 expected...
使用ansible playbook 实现检查目标主机的文件状态并校验md5值
以下内容使用Cursor AI自动生成YAML。
---
- name: 检查文件是否存在并校验MD5
hosts: wlsm
gather_facts: false
vars:
file_path: "/home/data/Config.json" # 请替换为实际的文件路径
expected_md5: "b283cf6ee2d3e1af35a72a29c120fb3a" # 请替换为预期的MD5哈希值
tasks:
- name: 检查文件是否存在
stat:
path: "{{ file_path }}"
register: file_stat
- name: 如果文件存在,计算MD5
command: "md5sum {{ file_path }}"
register: md5_output
when: file_stat.stat.exists
- name: 显示文件状态和MD5校验结果
debug:
msg:
- "文件存在: {{ file_stat.stat.exists }}"
- "计算的MD5: {{ md5_output.stdout.split()[0] if md5_output.stdout is defined else 'N/A' }}"
- "预期的MD5: {{ expected_md5 }}"
- "MD5校验结果: {{ 'Pass' if md5_output.stdout is defined and md5_output.stdout.split()[0] == expected_md5 else 'Fail' }}"
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)