Blame SOURCES/0001-Use-python-re-to-parse-service-output-in.patch

3e6a63
From 1a583e3c0d67a63cef1b1a433f176b7983d39812 Mon Sep 17 00:00:00 2001
3e6a63
From: Matt Martz <matt@sivel.net>
3e6a63
Date: Wed, 26 Oct 2022 16:06:43 -0500
3e6a63
Subject: [PATCH] [stable-2.13] Use python re to parse service output instead
3e6a63
 of grep (#79015) (#79051)
3e6a63
3e6a63
* Use python re to parse service output instead of grep. Fixes #78541
3e6a63
3e6a63
* Add clog frag
3e6a63
3e6a63
* Add an extra guard to abort if rc is 4, and /etc/init.d is missing
3e6a63
(cherry picked from commit 4458128)
3e6a63
3e6a63
Co-authored-by: Matt Martz <matt@sivel.net>
3e6a63
---
3e6a63
 .../fragments/78541-service-facts-re.yml      |  3 +++
3e6a63
 lib/ansible/modules/service_facts.py          | 20 +++++++++++--------
3e6a63
 2 files changed, 15 insertions(+), 8 deletions(-)
3e6a63
 create mode 100644 changelogs/fragments/78541-service-facts-re.yml
3e6a63
3e6a63
diff --git a/changelogs/fragments/78541-service-facts-re.yml b/changelogs/fragments/78541-service-facts-re.yml
3e6a63
new file mode 100644
3e6a63
index 0000000000..b96d584246
3e6a63
--- /dev/null
3e6a63
+++ b/changelogs/fragments/78541-service-facts-re.yml
3e6a63
@@ -0,0 +1,3 @@
3e6a63
+bugfixes:
3e6a63
+- service_facts - Use python re to parse service output instead of grep
3e6a63
+  (https://github.com/ansible/ansible/issues/78541)
3e6a63
diff --git a/lib/ansible/modules/service_facts.py b/lib/ansible/modules/service_facts.py
3e6a63
index 996b47fd59..60555fdc4a 100644
3e6a63
--- a/lib/ansible/modules/service_facts.py
3e6a63
+++ b/lib/ansible/modules/service_facts.py
3e6a63
@@ -89,6 +89,7 @@ ansible_facts:
3e6a63
 '''
3e6a63
 
3e6a63
 
3e6a63
+import os
3e6a63
 import platform
3e6a63
 import re
3e6a63
 from ansible.module_utils.basic import AnsibleModule
3e6a63
@@ -104,16 +105,19 @@ class BaseService(object):
3e6a63
 class ServiceScanService(BaseService):
3e6a63
 
3e6a63
     def _list_sysvinit(self, services):
3e6a63
-
3e6a63
-        rc, stdout, stderr = self.module.run_command("%s --status-all 2>&1 | grep -E \"\\[ (\\+|\\-) \\]\"" % self.service_path, use_unsafe_shell=True)
3e6a63
+        rc, stdout, stderr = self.module.run_command("%s --status-all" % self.service_path)
3e6a63
+        if rc == 4 and not os.path.exists('/etc/init.d'):
3e6a63
+            # This function is not intended to run on Red Hat but it could happen
3e6a63
+            # if `chkconfig` is not installed. `service` on RHEL9 returns rc 4
3e6a63
+            # when /etc/init.d is missing, add the extra guard of checking /etc/init.d
3e6a63
+            # instead of solely relying on rc == 4
3e6a63
+            return
3e6a63
         if rc != 0:
3e6a63
             self.module.warn("Unable to query 'service' tool (%s): %s" % (rc, stderr))
3e6a63
-        for line in stdout.split("\n"):
3e6a63
-            line_data = line.split()
3e6a63
-            if len(line_data) < 4:
3e6a63
-                continue  # Skipping because we expected more data
3e6a63
-            service_name = " ".join(line_data[3:])
3e6a63
-            if line_data[1] == "+":
3e6a63
+        p = re.compile(r'^\s*\[ (?P<state>\+|\-) \]\s+(?P<name>.+)$', flags=re.M)
3e6a63
+        for match in p.finditer(stdout):
3e6a63
+            service_name = match.group('name')
3e6a63
+            if match.group('state') == "+":
3e6a63
                 service_state = "running"
3e6a63
             else:
3e6a63
                 service_state = "stopped"
3e6a63
-- 
3e6a63
2.30.2
3e6a63