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

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 "