# Ansible playbook to get ServiceNow MID servers service and restart if needed

**URL:** <https://forum.ansible.com/t/ansible-playbook-to-get-servicenow-mid-servers-service-and-restart-if-needed/33676>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [August 25, 2020, 8:27pm UTC](https://forum.ansible.com/t/ansible-playbook-to-get-servicenow-mid-servers-service-and-restart-if-needed/33676 "2020-08-25T20:27:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![eh1](https://avatars.discourse-cdn.com/v4/letter/e/3bc359/32.png) [@eh1](https://forum.ansible.com/u/eh1)\
**Post date:** [August 25, 2020, 8:27pm UTC](https://forum.ansible.com/t/ansible-playbook-to-get-servicenow-mid-servers-service-and-restart-if-needed/33676/1 "2020-08-25T20:27:11Z")

</div>

Hello, I’m trying to get several mid server services which contain on the path of the service servicenow\_load or ServiceNow\_Prod to check if are running and if not restart the service using ansible playbook.

Tasks in playbook:

tasks:

- name: Check if needed MID Servers are installed

win\_command: wmic service where “name like ‘snc%%’”

register:service\_info

- name: teststdout

debug:

msg: “{{ service\_info.stdout\_lines | select(‘search’, ‘(servicenow\_load|ServiceNow\_Prod)’) | list }}”

register:mid\_service

Output of mid\_service variable:

{

“msg”: [

"FALSE TRUE ServiceNow MID Server\_SNLOAD\_MID4 0 Win32\_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server\_SNLOAD\_MID4 Normal 0 snc\_mid\_SNLOAD\_MID4 C:\servicenow\_load\mid2\agent\bin\wrapper-windows-x86-64.exe -s C:\servicenow\_load\mid2\agent\conf\wrapper.conf – – 9956 0 Own Process TRUE Auto LocalSystem Running OK Win32\_ComputerSystem 0 0 ",

"FALSE TRUE ServiceNow MID Serve\_SNLoad\_MID3 0 Win32\_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server\_SNLoad\_MID3 Normal 0 snc\_mid\_SNLoad\_MID3 C:\servicenow\_load\mid1\agent\bin\wrapper-windows-x86-64.exe -s C:\servicenow\_load\mid1\agent\conf\wrapper.conf – – 6812 0 Own Process TRUE Auto LocalSystem Running OK Win32\_ComputerSystem 0 0 ",

"FALSE FALSE ServiceNow MID Server\_SN\_MID3 0 Win32\_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server\_SN\_MID3 Normal 1066 snc\_mid\_SN\_MID3 C:\ServiceNow\_Prod\Agent1\agent\bin\wrapper-windows-x86-64.exe -s C:\ServiceNow\_Prod\Agent1\agent\conf\wrapper.conf – – 0 1 Own Process FALSE Auto LocalSystem Stopped OK Win32\_ComputerSystem 0 0 ",

"FALSE TRUE ServiceNow MID Server\_SN\_MID4 0 Win32\_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server\_SN\_MID4 Normal 0 snc\_mid\_SN\_MID4 C:\ServiceNow\_Prod\Agent2\agent\bin\wrapper-windows-x86-64.exe -s C:\ServiceNow\_Prod\Agent2\agent\conf\wrapper.conf – – 1756 0 Own Process TRUE Auto LocalSystem Running OK Win32\_ComputerSystem 0 0 "

],

“\_ansible\_verbose\_always”: true,

“\_ansible\_no\_log”: false,

“changed”: false

}

I’m getting the correct services in the output but from that result I just need to obtain the columns of service Name, PathName and State.

stdout\_lines columns on output:

“stdout\_lines”: [

"AcceptPause AcceptStop Caption CheckPoint CreationClassName DelayedAutoStart Description DesktopInteract DisplayName ErrorControl ExitCode InstallDate Name PathName ProcessIdServiceSpecificExitCode ServiceType Started StartMode StartName State Status SystemCreationClassName TagId WaitHint ",

Columns names and output together:

“AcceptPause AcceptStop Caption CheckPoint CreationClassName DelayedAutoStart Description DesktopInteract DisplayName ErrorControl ExitCode InstallDate Name PathName ProcessId ServiceSpecificExitCode ServiceType Started StartMode StartName State StatusSystemCreationClassNameTagIdWaitHint”,

"FALSE TRUE ServiceNow MID Server\_SNLOAD\_MID4 0 Win32\_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server\_SNLOAD\_MID4 Normal 0 snc\_mid\_SNLOAD\_MID4 C:\servicenow\_load\mid2\agent\bin\wrapper-windows-x86-64.exe -s C:\servicenow\_load\mid2\agent\conf\wrapper.conf – – 9956 0 Own Process TRUE Auto LocalSystem Running OK Win32\_ComputerSystem 0 0 ",

"FALSE TRUE ServiceNow MID Serve\_SNLoad\_MID3 0 Win32\_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server\_SNLoad\_MID3 Normal 0 snc\_mid\_SNLoad\_MID3 C:\servicenow\_load\mid1\agent\bin\wrapper-windows-x86-64.exe -s C:\servicenow\_load\mid1\agent\conf\wrapper.conf – – 6812 0 Own Process TRUE Auto LocalSystem Running OK Win32\_ComputerSystem 0 0 ",

"FALSE FALSE ServiceNow MID Server\_SN\_MID3 0 Win32\_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server\_SN\_MID3 Normal 1066 snc\_mid\_SN\_MID3 C:\ServiceNow\_Prod\Agent1\agent\bin\wrapper-windows-x86-64.exe -s C:\ServiceNow\_Prod\Agent1\agent\conf\wrapper.conf – – 0 1 Own Process FALSE Auto LocalSystem Stopped OK Win32\_ComputerSystem 0 0 ",

"FALSE TRUE ServiceNow MID Server\_SN\_MID4 0 Win32\_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server\_SN\_MID4 Normal 0 snc\_mid\_SN\_MID4 C:\ServiceNow\_Prod\Agent2\agent\bin\wrapper-windows-x86-64.exe -s C:\ServiceNow\_Prod\Agent2\agent\conf\wrapper.conf – – 1756 0 Own Process TRUE Auto LocalSystem Running OK Win32\_ComputerSystem 0 0 "
