# Do not restart service if it was just started?

**URL:** <https://forum.ansible.com/t/do-not-restart-service-if-it-was-just-started/17857>\
**Category:** Ansible Project\
**Created:** [March 27, 2015, 1:53pm UTC](https://forum.ansible.com/t/do-not-restart-service-if-it-was-just-started/17857 "2015-03-27T13:53:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyle](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@Kyle](https://forum.ansible.com/u/Kyle)\
**Post date:** [March 27, 2015, 1:53pm UTC](https://forum.ansible.com/t/do-not-restart-service-if-it-was-just-started/17857/1 "2015-03-27T13:53:58Z")

</div>

Newish to Ansible.

I’m setting up a playbook for Splunk and it works great, the only part I’m unsure of is how to prevent a restart after splunk had just started. The whole play and the handler is below.

So on initial installation, I’m just trying to prevent the following, which is needless:

TASK: [splunk | Start Splunk] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [host1]

NOTIFIED: [splunk | restart splunk] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [host1]

# config/roles/splunk/tasks/main.yml

- name: Evaluating forwarder package  
yum: name=splunkforwarder state=latest  
tags: splunk

- name: configure outputs.conf  
template:  
src=outputs.conf.j2 dest=/opt/splunkforwarder/etc/system/local/outputs.conf owner=splunk group=splunk mode=0644  
notify:

- restart splunk  
tags: splunk

- name: configure inputs.conf  
template:  
src=linux.inputs.conf.j2 dest=/opt/splunkforwarder/etc/system/local/inputs.conf owner=splunk group=splunk mode=0644  
notify:

- restart splunk  
tags: splunk

- name: Accept license and configure boot start  
command: /opt/splunkforwarder/bin/splunk enable boot-start -user splunk --accept-license creates=/etc/init.d/splunk  
tags: splunk

- name: Correct permissions on init script  
file: path=/etc/init.d/splunk mode=0755  
tags: splunk

- name: Correct permissions on splunk directory  
file: path=/opt/splunkforwarder state=directory owner=splunk group=splunk recurse=yes  
tags: splunk

- name: Start Splunk  
service: name=splunk state=started  
tags: splunk

And my handler looks like this:

# config/roles/splunk/handlers/main.yml

- name: restart splunk  
service: name=splunk state=restarted

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [March 28, 2015, 3:00pm UTC](https://forum.ansible.com/t/do-not-restart-service-if-it-was-just-started/17857/2 "2015-03-28T15:00:19Z")

</div>

The template module ensures idempotence. So, if there is no diff between the files and the templates, the task should not return “changed”, thus not invoking the handler. However, sometimes things may change on the server (i.e. permissions, ownership) because of various reasons. For that, you can use _changed\_when_ to rule out “false positives”.

You could also simply remove the “Start Splunk” task and only rely on config files changes to trigger the restart of the service. This could make sense if you don’t install the service through a package manager.
