I have a service that overwrites config when it stops. Is it possible to tell ansible that if a config file changes it should stop the service first before copying the file?
Not sure what you mean. You are saying a service call to stop can change a configuration file that is not really related. This is what I think you mean. someservice can sometimes change /etc/sometimesrc
Capture the MD5 or something similar in one task before stopping the service (Linux, OS X and other may have an md5
command)- command: md5sum /etc/sometimesrc
register: original_md5
changed_when: false # Never report changed for this task
- service: name=someservice state=stopped # Can cause file to be changed?
line in stdout should be exactly the same unless the MD5 sum changes
- command: md5sum /etc/sometimesrc
register: md5sum
changed_when: new_md5.stdout != original_md5.stdout
Copy the file conditionally, only if the MD5 sum changed because of stopping the process
- copy: src=mysometimesrc dest=/etc/sometimesrc
when: md5sum.changed
Here’s a way to do it using the stat module that may contain some errors, as I wrote this a little quickly
-
copy: src=foo dest=/path/one.new
-
stat: path=/path/one get_md5=yes
register: one -
stat: path=/path/one.new get_md5=yes
register: two -
shell: mv /path/one.new /path/one
when: one.stat.md5 != two.stat.md5
notify: -
start blarg
start should be ‘restart’.
And if you wanted to run it now, stick a “- meta: flush_handlers” in there after the last shell task.