# Check existence and state of a service on windows machine using win\_service

**URL:** <https://forum.ansible.com/t/check-existence-and-state-of-a-service-on-windows-machine-using-win-service/20887>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [March 16, 2016, 11:46am UTC](https://forum.ansible.com/t/check-existence-and-state-of-a-service-on-windows-machine-using-win-service/20887 "2016-03-16T11:46:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [March 16, 2016, 11:46am UTC](https://forum.ansible.com/t/check-existence-and-state-of-a-service-on-windows-machine-using-win-service/20887/1 "2016-03-16T11:46:49Z")

</div>

I am trying to provision windows server 2012 R2 machine with ansible and for that i need to gather some custom facts about the running services. I tried getting the information via the win\_service module like this:

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [March 16, 2016, 8:43pm UTC](https://forum.ansible.com/t/check-existence-and-state-of-a-service-on-windows-machine-using-win-service/20887/2 "2016-03-16T20:43:52Z")

</div>

I suggest you add a task, before your win\_service task that installs the service if it is not yet installed. Depending on what the service is, you might be able to use win\_package to install it. If it is already installed, there is not much to loose by running the win\_package task a second time, as it will only install if not installed.

When provisioning, I like to write playbooks that drag the machine into the state that I want it in rather than trying to write playbooks that have to handle lots of different possiblities that might exist on the different machines.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [March 17, 2016, 5:26am UTC](https://forum.ansible.com/t/check-existence-and-state-of-a-service-on-windows-machine-using-win-service/20887/3 "2016-03-17T05:26:43Z")

</div>

Agreed with your point - we should drag the machine in the state we want. But in my case the number 1 priority is to make the script idempotent, which seems like quite a thing on windows end.

I not installing anything on the windows end and my application will run as standalone service, no installation required. I am using NSSM to install the services, so win\_package is not going to be of any use to me.

To make my script idempotent, i must first check if the service exists, before i invoke NSSM to create the service. I can live with the complicated conditionals i will have in my scripts, but they must be idempotent in the end.

The solution where i check the service with win\_service works fine except that in the case it is not present, it will give me a lot of red statements. What i am looking for is a better way where i can simply check that if a given service is created or not. I wish NSSM had some API to check for that.

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [March 17, 2016, 9:53am UTC](https://forum.ansible.com/t/check-existence-and-state-of-a-service-on-windows-machine-using-win-service/20887/4 "2016-03-17T09:53:03Z")

</div>

You could run a raw powershell command like

Get-Service nameOfService -ErrorAction SilentlyContinue

and then register the result, then check

You would need the ‘-ErrorAction SilentlyContinue’ bit otherwise when the service doesn’t exist, Get-Service will fail.

also if you prefer you could do something similar with the sc.exe like this

sc.exe query serviceName

Hope this helps.

---

<div class="post-metadata">

**Author:** ![ishan\_jain](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@ishan\_jain](https://forum.ansible.com/u/ishan_jain)\
**Post date:** [March 17, 2016, 11:24am UTC](https://forum.ansible.com/t/check-existence-and-state-of-a-service-on-windows-machine-using-win-service/20887/5 "2016-03-17T11:24:59Z")

</div>

Both get-service and sc.exe returns textual results. I will have to match strings to check if service exists and its state and i never like to base my conditions on pattern matching.  
The best solution i could thing of is to go with win\_service and define a failed\_when situation along with. At least i will have the results in proper json objects.

---

<div class="post-metadata">

**Author:** ![CORLEONE](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@CORLEONE](https://forum.ansible.com/u/CORLEONE)\
**Post date:** [March 19, 2019, 6:11am UTC](https://forum.ansible.com/t/check-existence-and-state-of-a-service-on-windows-machine-using-win-service/20887/6 "2019-03-19T06:11:14Z")

</div>

win\_service returns values such as exists(which is false or true depending whether the service is absent or present).So you can just use something like  
{when: win\_service\_register.exists} to any task which u want to execute only if the service is present. .
