How to Start and Stop Services in Windows by Using Ansible Playbook on Oracle Linux 8.5

To Start and Stop Services in windows by using Ansible Playbook on Oracle Linux 8.5

Introduction:

Windows Services is a core feature of the Microsoft Windows operating system that enables the creation and management of long-running processes Using the win_service ansible module, you can start, stop, and restart services

Procedure:

Master Server Requirements: ansible python3-pip pywinrm (python package)

Windows Requirements: powershell 3+ Dot net 4

Installation Procedure:

Step 1: Check the OS version by using the below command

[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.5"
ID="ol"
ID_LIKE="fedora"

Step 2: Check the availability of Ansible package, python3-pip package, by using the below command

[root@linuxhelp ~]# yum list ansible
ansible.noarch                        2.9.27-1.el8                         @ol8_developer_EPEL
Available Packages
ansible.src                           2.9.27-1.el8                         ol8_developer_EPEL 

[root@linuxhelp ~]# yum list python3
Last metadata expiration check: 1:28:13 ago on Sun 11 Sep 2022 03:22:41 PM IST.
Available Packages
python3.src                         3.6.8-45.0.1.el8                         ol8_baseos_latest
python3.src                         3.6.8-45.0.1.el8                         ol8_appstream    

[root@linuxhelp ~]# pip3 list | grep pywinrm
pywinrm (0.4.3)

Step 3: Create inventory for Windows node system by using the below command

[root@linuxhelp ~]# vim /etc/ansible/hosts
[windows]
192.168.6.51

[windows:vars]
ansible_user=Admin
ansible_password=Linuxc#4
ansible_port=5986
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore

Step 4: Create a playbook to Start the Service in windows by using the below command

root@linuxhelp:~# vim start.yml
---
- hosts: windows

  tasks:

   - name: start service windows update

     win_service:

       name: wuauserv

       state: started

Step 5: Check the syntax of the start.yml ansible playbook by using the below command

root@linuxhelp:~# ansible-playbook start.yml --syntax-check

playbook: start.yml

Step6: Run the start.yml playbook by using the below command

[root@linuxhelp ~]# ansible-playbook start.yml

PLAY [windows] *****************************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [192.168.6.51]

TASK [start service windows update] ********************************************************
changed: [192.168.6.51]

PLAY RECAP *********************************************************************************
192.168.6.51               : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Step 7: Create a playbook to Stop the Service in windows by using the below command

root@linuxhelp:~# vim stop.yml
---
- hosts: windows

  tasks:

   - name: stop service windows update

     win_service:

       name: wuauserv

       state: stopped

Step 8: Check the syntax of the stop.yml ansible playbook by using the below command

root@linuxhelp:~# ansible-playbook stop.yml --syntax-check
playbook: stop.yml

Step 9: Run the stop.yml playbook by using the below command

[root@linuxhelp ~]# ansible-playbook stop.yml
PLAY [windows] *****************************************************************************

TASK [Gathering Facts] *********************************************************************
ok: [192.168.6.51]

TASK [Stop service windows update] *********************************************************
changed: [192.168.6.51]

PLAY RECAP *********************************************************************************
192.168.6.51               : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Conclusion

We have reached the end of this article. In this guide, we have walked you through the steps required to Start and Stop services in windows by using Ansible Playbook on Oracle Linux 8.5. Your feedback is much welcome.

FAQ
Q
How do I check services?
A
Press the Win + R keys on your keyboard, to open the Run window. Then, type "services. msc" and hit Enter or press OK.
Q
Can a Windows service launch an application?
A
Windows Services cannot start additional applications because they are not running in the context of any particular user
Q
Where are Windows services located?
A
The Services file is typically located in %windir%\System32\drivers\etc\services.
Q
When should you use Windows services?
A
You should create a Windows Service to run code in the background, without user interaction
Q
How do Windows services work?
A
Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions.