From d66aedcce798d680f37af17dd6465a0fdffe2d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Martin?= Date: Fri, 26 Nov 2021 10:30:28 +0100 Subject: [PATCH 1/3] Add openrc support to service_facts --- lib/ansible/modules/service_facts.py | 30 ++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/service_facts.py b/lib/ansible/modules/service_facts.py index 8fb598dbf56056..1e01093918e044 100644 --- a/lib/ansible/modules/service_facts.py +++ b/lib/ansible/modules/service_facts.py @@ -15,7 +15,7 @@ description: - Return service state information as fact data for various service management utilities. version_added: "2.5" -requirements: ["Any of the following supported init systems: systemd, sysv, upstart, AIX SRC"] +requirements: ["Any of the following supported init systems: systemd, sysv, upstart, openrc, AIX SRC"] extends_documentation_fragment: - action_common_attributes - action_common_attributes.facts @@ -112,9 +112,11 @@ def gather_services(self): return None initctl_path = self.module.get_bin_path("initctl") chkconfig_path = self.module.get_bin_path("chkconfig") + rc_status_path = self.module.get_bin_path("rc-status") + rc_update_path = self.module.get_bin_path("rc-update") # sysvinit - if service_path is not None and chkconfig_path is None: + if service_path is not None and chkconfig_path is None and rc_status_path is None: rc, stdout, stderr = self.module.run_command("%s --status-all 2>&1 | grep -E \"\\[ (\\+|\\-) \\]\"" % service_path, use_unsafe_shell=True) for line in stdout.split("\n"): line_data = line.split() @@ -191,6 +193,30 @@ def gather_services(self): service_state = 'stopped' service_data = {"name": service_name, "state": service_state, "status": service_status, "source": "sysv"} services[service_name] = service_data + # openrc + elif rc_status_path is not None and rc_update_path is not None: + all_services_runlevels = {} + rc, stdout, stderr = self.module.run_command("%s -a -s -m 2>&1 | grep '^ ' | tr -d '[]'" % rc_status_path, use_unsafe_shell=True) + rc_u, stdout_u, stderr_u = self.module.run_command("%s show -v 2>&1 | grep '|'" % rc_update_path, use_unsafe_shell=True) + for line in stdout_u.split('\n'): + line_data = line.split('|') + if len(line_data) < 2: + continue + service_name = line_data[0].strip() + runlevels = line_data[1].strip() + if not runlevels: + all_services_runlevels[service_name] = None + else: + all_services_runlevels[service_name] = runlevels.split() + for line in stdout.split('\n'): + line_data = line.split() + if len(line_data) < 2: + continue + service_name = line_data[0] + service_state = line_data[1] + service_runlevels = all_services_runlevels[service_name] + service_data = {"name": service_name, "runlevels": service_runlevels, "state": service_state, "source": "openrc"} + services[service_name] = service_data return services From 902ed3a4702f41e4dcec0af608cfaca624de3ca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Martin?= Date: Tue, 30 Nov 2021 23:17:20 +0100 Subject: [PATCH 2/3] Add changelog for #76373 --- .../fragments/76373-add-openrc-support-to-service_facts.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/76373-add-openrc-support-to-service_facts.yaml diff --git a/changelogs/fragments/76373-add-openrc-support-to-service_facts.yaml b/changelogs/fragments/76373-add-openrc-support-to-service_facts.yaml new file mode 100644 index 00000000000000..d407f669cc5c18 --- /dev/null +++ b/changelogs/fragments/76373-add-openrc-support-to-service_facts.yaml @@ -0,0 +1,2 @@ +minor_changes: + - services_facts: Add support for openrc (https://github.com/ansible/ansible/pull/76373). From 30b86febb099cbb39b17e414ec069eaf5a990e0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Martin?= Date: Tue, 30 Nov 2021 23:36:48 +0100 Subject: [PATCH 3/3] Fix changelog message --- .../fragments/76373-add-openrc-support-to-service_facts.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/76373-add-openrc-support-to-service_facts.yaml b/changelogs/fragments/76373-add-openrc-support-to-service_facts.yaml index d407f669cc5c18..166449a43b0d4c 100644 --- a/changelogs/fragments/76373-add-openrc-support-to-service_facts.yaml +++ b/changelogs/fragments/76373-add-openrc-support-to-service_facts.yaml @@ -1,2 +1,2 @@ minor_changes: - - services_facts: Add support for openrc (https://github.com/ansible/ansible/pull/76373). + - services_facts - Add support for openrc (https://github.com/ansible/ansible/pull/76373).