Get stopped service name using Ansible playbook

Hello,

I’m trying to get stopped ServiceNow MID server services name using following ansible playbook.
(The services should be on path containing servicenow_load or ServiceNow_Prod and state: stopped.)

tasks:

  • name: Check if MID Server service is installed
    win_command: wmic service where “name like ‘snc%%’”
    register: service_info

  • name: Get MID server services which are installed in path servicenow_load or ServiceNow_Prod
    debug:
    msg: “{{ service_info.stdout_lines | select(‘search’, ‘(servicenow_load|ServiceNow_Prod)’) | list }}”
    register: mid_service

  • name: Get stopped services from variable used before
    debug:
    msg: “{{ mid_service.stdout[0] | regex_findall(‘\S+\s+Stopped\b.') | regex_findall('snc_mid_[a-zA-Z0-9_]’) }}”

Sample of mid_service variable lines:

"FALSE TRUE ServiceNow MID Server_MID4 0 Win32_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server_MID4 Normal 0 snc_mid_MID4 C:\servicenow_load\mid2\agent\bin\wrapper-windows-x86-64.exe -s C:\servicenow_load\mid2\agent\conf\wrapper.conf – – 1752 0 Own Process TRUE Auto LocalSystem Running OK Win32_ComputerSystem 0 0 ",
"FALSE FALSE ServiceNow MID Server_MID3 0 Win32_Service FALSE ServiceNow MID Server FALSE ServiceNow MID Server_MID3 Normal 0 snc_mid_MID3 C:\servicenow_load\mid1\agent\bin\wrapper-windows-x86-64.exe -s C:\servicenow_load\mid1\agent\conf\wrapper.conf – – 0 0 Own Process FALSE Auto LocalSystem Stopped OK Win32_ComputerSystem 0 0 ",

I’m getting this error:

{
“msg”: “Unexpected templating type error occurred on ({{ mid_service.stdout[0] | regex_findall(‘\\S+\\s+Stopped\\b.') | regex_findall('snc_mid_[a-zA-Z0-9_]’) }}): expected string or bytes-like object”,
“_ansible_no_log”: false
}

I would like to get service name column of services stopped (column 12 → snc_mid_MID3). Could you help me on this?

Thank you!! :slight_smile: