Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ansible/ansible.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Martz <matt@sivel.net>2022-10-27 00:06:43 +0300
committerGitHub <noreply@github.com>2022-10-27 00:06:43 +0300
commit1a583e3c0d67a63cef1b1a433f176b7983d39812 (patch)
tree168f5e23086fdb037854eeaf9d79b12b7a4aed0e
parent65fd4e182ba1664deec04418af085db92cc6029b (diff)
[stable-2.13] Use python re to parse service output instead of grep (#79015) (#79051)
* Use python re to parse service output instead of grep. Fixes #78541 * Add clog frag * Add an extra guard to abort if rc is 4, and /etc/init.d is missing (cherry picked from commit 4458128) Co-authored-by: Matt Martz <matt@sivel.net>
-rw-r--r--changelogs/fragments/78541-service-facts-re.yml3
-rw-r--r--lib/ansible/modules/service_facts.py20
2 files changed, 15 insertions, 8 deletions
diff --git a/changelogs/fragments/78541-service-facts-re.yml b/changelogs/fragments/78541-service-facts-re.yml
new file mode 100644
index 00000000000..b96d5842466
--- /dev/null
+++ b/changelogs/fragments/78541-service-facts-re.yml
@@ -0,0 +1,3 @@
+bugfixes:
+- service_facts - Use python re to parse service output instead of grep
+ (https://github.com/ansible/ansible/issues/78541)
diff --git a/lib/ansible/modules/service_facts.py b/lib/ansible/modules/service_facts.py
index 996b47fd59c..60555fdc4aa 100644
--- a/lib/ansible/modules/service_facts.py
+++ b/lib/ansible/modules/service_facts.py
@@ -89,6 +89,7 @@ ansible_facts:
'''
+import os
import platform
import re
from ansible.module_utils.basic import AnsibleModule
@@ -104,16 +105,19 @@ class BaseService(object):
class ServiceScanService(BaseService):
def _list_sysvinit(self, services):
-
- rc, stdout, stderr = self.module.run_command("%s --status-all 2>&1 | grep -E \"\\[ (\\+|\\-) \\]\"" % self.service_path, use_unsafe_shell=True)
+ rc, stdout, stderr = self.module.run_command("%s --status-all" % self.service_path)
+ 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:
self.module.warn("Unable to query 'service' tool (%s): %s" % (rc, stderr))
- for line in stdout.split("\n"):
- line_data = line.split()
- if len(line_data) < 4:
- continue # Skipping because we expected more data
- service_name = " ".join(line_data[3:])
- if line_data[1] == "+":
+ p = re.compile(r'^\s*\[ (?P<state>\+|\-) \]\s+(?P<name>.+)$', flags=re.M)
+ for match in p.finditer(stdout):
+ service_name = match.group('name')
+ if match.group('state') == "+":
service_state = "running"
else:
service_state = "stopped"