# Ansible module to run window service

**URL:** <https://forum.ansible.com/t/ansible-module-to-run-window-service/22102>\
**Category:** Ansible Developer\
**Tags:** windows\
**Created:** [July 24, 2016, 7:59pm UTC](https://forum.ansible.com/t/ansible-module-to-run-window-service/22102 "2016-07-24T19:59:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HY1](https://avatars.discourse-cdn.com/v4/letter/h/2bfe46/32.png) [@HY1](https://forum.ansible.com/u/HY1)\
**Post date:** [July 24, 2016, 7:59pm UTC](https://forum.ansible.com/t/ansible-module-to-run-window-service/22102/1 "2016-07-24T19:59:47Z")

</div>

Hello, I’m very new to Ansible.

I noticed that there is a win\_service module to create window service but none to run a window service. I have a powershell script to do that. Is it better to create a new module or integrate this function ( running windows service) with the existing win\_service module?

Thanks in advance!

---

<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:** [July 25, 2016, 10:09am UTC](https://forum.ansible.com/t/ansible-module-to-run-window-service/22102/2 "2016-07-25T10:09:07Z")

</div>

Hi

Generally I would say creating modules is well worth it, however you might not need to in this case.

win\_service is actually just for controlling whether an existing service is started or stopped, (or restarted).

If you need to make a .exe run as a windows service there is the win\_nssm module. You have to install nssm (which is easy as its just a small exe file you can put in place using win\_copy), then you can use win\_nssm to create the service.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![HY1](https://avatars.discourse-cdn.com/v4/letter/h/2bfe46/32.png) [@HY1](https://forum.ansible.com/u/HY1)\
**Post date:** [July 26, 2016, 1:12pm UTC](https://forum.ansible.com/t/ansible-module-to-run-window-service/22102/3 "2016-07-26T13:12:45Z")

</div>

Thank you! That is very helpful! According to the win\_nssm document, it should be install using win\_chocolatey, I wonder if you meant the same thing? Would you care to elaborate more about the install nssm part? Thank you so much for your help.

在 2016年7月25日星期一 UTC-4上午6:09:08，jhawkesworth写道：

---

<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:** [July 26, 2016, 1:18pm UTC](https://forum.ansible.com/t/ansible-module-to-run-window-service/22102/4 "2016-07-26T13:18:56Z")

</div>

win\_chocolatey is a convenient way to install nssm but it is quite small so you can probably download straight from the nssm website using win\_get\_url

[https://nssm.cc/release/nssm-2.24.zip](https://nssm.cc/release/nssm-2.24.zip)

Then use win\_unzip to unzip it somewhere. If I remember there is 32bit and 64bit version in the zip file.

Hope this helps.

Jon

By the way, questions like this are probably best asked on ansible-project group or #ansible irc channel as there are more ansible users there (this group is for ansible developers).

---

<div class="post-metadata">

**Author:** ![HY1](https://avatars.discourse-cdn.com/v4/letter/h/2bfe46/32.png) [@HY1](https://forum.ansible.com/u/HY1)\
**Post date:** [July 27, 2016, 2:04pm UTC](https://forum.ansible.com/t/ansible-module-to-run-window-service/22102/5 "2016-07-27T14:04:03Z")

</div>

Thank you! So I wrote following task:

- name: install win\_nssm  
win\_chocolatey:  
name: nssm

- name: create and run service name service  
win\_nssm:  
name: servicename  
application: path  
start\_mode: auto

The service is created but not started( service status is blank ). I was able to start it manually (Hit the start button in windows services console) though. Then I tried to add another task:

- name: run servicename  
win\_service:  
name: servicename  
start\_mode: auto  
state: started

Then the service status is ‘Paused’ and I can’t even started it manually.

As I understand, win\_nssm suppose to not only create the service but also start the service correct? Am I using the module wrong?

Thank you so much for your help!

在 2016年7月26日星期二 UTC-4上午9:18:56，jhawkesworth写道：

---

<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:** [July 27, 2016, 11:46pm UTC](https://forum.ansible.com/t/ansible-module-to-run-window-service/22102/6 "2016-07-27T23:46:42Z")

</div>

I think if you add state: started to your win\_nssm module parameters that should be enough to get it to start without the need to add win\_service module to your playbook.

If the service is paused, though, its possible the program you are trying to run as a service is trying to do something it doesn’t have permission to do. For example recent versions of Windows do not allow services to display user interfaces by default. If that applies there are some workarounds described here: [http://www.slideshare.net/jhawkesworth/ansible-20-windows-and-no-powershell-this-year-i-promise-ansiblefest-london-2016](http://www.slideshare.net/jhawkesworth/ansible-20-windows-and-no-powershell-this-year-i-promise-ansiblefest-london-2016)

Its possible other things are not correct, such as the user that the service is running as - I think you will have to debug why the service enters the paused state.

Hope this helps,

Jon
