# Start service if is not running

**URL:** <https://forum.ansible.com/t/start-service-if-is-not-running/31477>\
**Category:** Ansible Project\
**Created:** [October 17, 2019, 9:18am UTC](https://forum.ansible.com/t/start-service-if-is-not-running/31477 "2019-10-17T09:18:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oxs](https://avatars.discourse-cdn.com/v4/letter/o/ebca7d/32.png) [@Oxs](https://forum.ansible.com/u/Oxs)\
**Post date:** [October 17, 2019, 9:18am UTC](https://forum.ansible.com/t/start-service-if-is-not-running/31477/1 "2019-10-17T09:18:26Z")

</div>

Guys,

I would like to start a service if is not running on my target.

What is the best practice to do that?

Example:

when: my\_agent == False ?  
when\_false

Thanks

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 17, 2019, 9:27am UTC](https://forum.ansible.com/t/start-service-if-is-not-running/31477/2 "2019-10-17T09:27:58Z")

</div>

Use the service module with "state: started".

---

<div class="post-metadata">

**Author:** ![Oxs](https://avatars.discourse-cdn.com/v4/letter/o/ebca7d/32.png) [@Oxs](https://forum.ansible.com/u/Oxs)\
**Post date:** [October 17, 2019, 9:46am UTC](https://forum.ansible.com/t/start-service-if-is-not-running/31477/3 "2019-10-17T09:46:37Z")

</div>

Thanks fro your feedback.  
Yes I know… but I would like something like that:

If my service is running I don’t want to reinstall all package.  
I would like to manage that with a “register” output from my service state.

-name= check if service is running  
service: name=ctmagent.service state=started  
register=ctmagent

-name = Clean Install  
file: example  
when: ctmagent \>\> ( Idon’t know how to manage the when)

Thanks

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [October 17, 2019, 9:51am UTC](https://forum.ansible.com/t/start-service-if-is-not-running/31477/4 "2019-10-17T09:51:44Z")

</div>

Hi

I don't understand... making sure a service is running is distinctly  
different from making sure it's installed.  
The only dependency I can see is that it has to be installed before  
you can ensure it's running (or not).

Dick
