service_facts correct meaning of state for systemd service units (#40914) (#42068)

* service_facts correct meaning of state for systemd service units

Fixes #40809

Previously this module used the commend `systemctl list-unit-files
--type=service` to query state of services but list-unit-files only
shows enabled vs disabled which is not what we want for "state"

Signed-off-by: Adam Miller <admiller@redhat.com>

* make sure to define service_name before referencing it

Signed-off-by: Adam Miller <admiller@redhat.com>
(cherry picked from commit bf1cc2f1f4)
pull/42207/head
Brian Coca 6 years ago committed by Matt Davis
parent ceb14fcc33
commit fdb813766a

@ -0,0 +1,2 @@
bugfixes:
- correct service facts systemd detection of state https://github.com/ansible/ansible/issues/40809

@ -178,16 +178,16 @@ class SystemctlScanService(BaseService):
systemctl_path = self.module.get_bin_path("systemctl", opt_dirs=["/usr/bin", "/usr/local/bin"]) systemctl_path = self.module.get_bin_path("systemctl", opt_dirs=["/usr/bin", "/usr/local/bin"])
if systemctl_path is None: if systemctl_path is None:
return None return None
rc, stdout, stderr = self.module.run_command("%s list-unit-files --type=service | tail -n +2 | head -n -2" % systemctl_path, use_unsafe_shell=True) rc, stdout, stderr = self.module.run_command("%s list-units --no-pager --type service --all" % systemctl_path, use_unsafe_shell=True)
for line in stdout.split("\n"): for line in [svc_line for svc_line in stdout.split('\n') if '.service' in svc_line and 'not-found' not in svc_line]:
line_data = line.split() service_name = line.split()[0]
if len(line_data) != 2: if "running" in line:
continue
if line_data[1] == "enabled":
state_val = "running" state_val = "running"
else: else:
if 'failed' in line:
service_name = line.split()[1]
state_val = "stopped" state_val = "stopped"
services[line_data[0]] = {"name": line_data[0], "state": state_val, "source": "systemd"} services[service_name] = {"name": service_name, "state": state_val, "source": "systemd"}
return services return services

Loading…
Cancel
Save