# Stop service before copying config file?

**URL:** <https://forum.ansible.com/t/stop-service-before-copying-config-file/15623>\
**Category:** Ansible Project\
**Created:** [July 8, 2014, 2:32pm UTC](https://forum.ansible.com/t/stop-service-before-copying-config-file/15623 "2014-07-08T14:32:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen\_Huang1](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@Glen\_Huang1](https://forum.ansible.com/u/Glen_Huang1)\
**Post date:** [July 8, 2014, 2:32pm UTC](https://forum.ansible.com/t/stop-service-before-copying-config-file/15623/1 "2014-07-08T14:32:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Andrew\_Udvare](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Andrew\_Udvare](https://forum.ansible.com/u/Andrew_Udvare)\
**Post date:** [July 9, 2014, 8:55am UTC](https://forum.ansible.com/t/stop-service-before-copying-config-file/15623/2 "2014-07-09T08:55:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 9, 2014, 7:36pm UTC](https://forum.ansible.com/t/stop-service-before-copying-config-file/15623/3 "2014-07-09T19:36:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 9, 2014, 7:36pm UTC](https://forum.ansible.com/t/stop-service-before-copying-config-file/15623/4 "2014-07-09T19:36:48Z")

</div>

start should be ‘restart’.

And if you wanted to run it now, stick a “- meta: flush\_handlers” in there after the last shell task.
