gpt4 book ai didi

Ansible:如何在 shell 命令中检查文件是否更改?

转载 作者:行者123 更新时间:2023-12-02 12:21:27 24 4
gpt4 key购买 nike

我有文件,由 shell 命令生成

- stat: path=/etc/swift/account.ring.gz get_md5=yes
register: account_builder_stat

- name: write account.ring.gz file
shell: swift-ring-builder account.builder write_ring <--- rewrite account.ring.gz
chdir=/etc/swift
changed_when: ??? account_builder_stat.changed ??? <-- no give desired effect

如何检查文件是否已更改?

最佳答案

- stat: path=/etc/swift/account.ring.gz get_md5=yes
register: before

- name: write account.ring.gz file
shell: swift-ring-builder account.builder write_ring # update account.ring.gz
chdir=/etc/swift
changed_when: False # without this, as long as swift-ring-builder exits with
# return code 0 this task would always be reported as changed

- stat: path=/etc/swift/account.ring.gz get_md5=yes
register: after

- debug: msg='report this task as "changed" if file changed'
changed_when: "'{{before.stat.md5}}' != '{{after.stat.md5}}'"

- debug: msg='execute this task if file changed'
when: "'{{before.stat.md5}}' != '{{after.stat.md5}}'"

如果您真正想要的是根据 swift-ring-builder 的结果报告任务“写入 account.ring.gz 文件”已更改或未更改,那么您必须运行一个迷你shell 脚本。像这样的东西(未经测试):

- name: write account.ring.gz file
shell: bfr=`md5sum account.ring.gz`; swift-ring-builder account.builder write_ring; aftr=`md5sum account.ring.gz`; test $bfr -eq $aftr
chdir=/etc/swift

或者如果我正确地记得 md5sum 选项:

- name: write account.ring.gz file
shell: echo `md5sum account.ring.gz` account.ring.gz > /tmp/ff; swift-ring-builder account.builder write_ring; md5sum -c /tmp/ff
chdir=/etc/swift

关于Ansible:如何在 shell 命令中检查文件是否更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29275576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com