From 47e412cc5cfe8b08247246324d573788a6b533c4 Mon Sep 17 00:00:00 2001 From: vzhestkov Date: Wed, 28 Aug 2024 14:21:56 +0200 Subject: [PATCH] Better handling output of systemctl --version --- salt/grains/core.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/salt/grains/core.py b/salt/grains/core.py index 119e78c8e9bb..b7f99c0fa526 100644 --- a/salt/grains/core.py +++ b/salt/grains/core.py @@ -2517,10 +2517,31 @@ def _systemd(): """ Return the systemd grain """ - systemd_info = __salt__["cmd.run"]("systemctl --version").splitlines() + systemd_version = "UNDEFINED" + systemd_features = "" + try: + systemd_output = __salt__["cmd.run_all"]("systemctl --version") + except Exception: # pylint: disable=broad-except + log.error("Exception while executing `systemctl --version`", exc_info=True) + return { + "version": systemd_version, + "features": systemd_features, + } + if systemd_output.get("retcode") == 0: + systemd_info = systemd_output.get("stdout", "").splitlines() + try: + if systemd_info[0].startswith("systemd "): + systemd_version = systemd_info[0].split()[1] + systemd_features = systemd_info[1] + except IndexError: + pass + if systemd_version == "UNDEFINED" or systemd_features == "": + log.error( + "Unexpected output returned by `systemctl --version`: %s", systemd_output + ) return { - "version": systemd_info[0].split()[1], - "features": systemd_info[1], + "version": systemd_version, + "features": systemd_features, }