Ansible:如何检查shell命令中的文件?

时间:2015-03-26 10:01:05

标签: ansible

我有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

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

2 个答案:

答案 0 :(得分:6)

- 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}}'"

如果你真正想要的是报告任务&#39;写account.ring.gz文件&#39;根据{{​​1}}的结果更改或未更改,则必须运行迷你shell脚本。这样的事情(未经测试):

swift-ring-builder

或者我是否正确记住了md5sum选项:

- 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

答案 1 :(得分:0)

按照@ Kashyap的回答你也可以在两个文件之间进行差异,但是如果这两个文件不同,diff会导致错误并且会停止你的playbook运行。