@ -89,6 +89,7 @@ ansible_facts:
'''
'''
import os
import platform
import platform
import re
import re
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . basic import AnsibleModule
@ -104,16 +105,19 @@ class BaseService(object):
class ServiceScanService ( BaseService ) :
class ServiceScanService ( BaseService ) :
def _list_sysvinit ( self , services ) :
def _list_sysvinit ( self , services ) :
rc , stdout , stderr = self . module . run_command ( " %s --status-all " % self . service_path )
rc , stdout , stderr = self . module . run_command ( " %s --status-all 2>&1 | grep -E \" \\ [ ( \\ +| \\ -) \\ ] \" " % self . service_path , use_unsafe_shell = True )
if rc == 4 and not os . path . exists ( ' /etc/init.d ' ) :
# This function is not intended to run on Red Hat but it could happen
# if `chkconfig` is not installed. `service` on RHEL9 returns rc 4
# when /etc/init.d is missing, add the extra guard of checking /etc/init.d
# instead of solely relying on rc == 4
return
if rc != 0 :
if rc != 0 :
self . module . warn ( " Unable to query ' service ' tool ( %s ): %s " % ( rc , stderr ) )
self . module . warn ( " Unable to query ' service ' tool ( %s ): %s " % ( rc , stderr ) )
for line in stdout . split ( " \n " ) :
p = re . compile ( r ' ^ \ s* \ [ (?P<state> \ +| \ -) \ ] \ s+(?P<name>.+)$ ' , flags = re . M )
line_data = line . split ( )
for match in p . finditer ( stdout ) :
if len ( line_data ) < 4 :
service_name = match . group ( ' name ' )
continue # Skipping because we expected more data
if match . group ( ' state ' ) == " + " :
service_name = " " . join ( line_data [ 3 : ] )
if line_data [ 1 ] == " + " :
service_state = " running "
service_state = " running "
else :
else :
service_state = " stopped "
service_state = " stopped "