From e006d945848157b2d1a3e5e9b4c751ddce87fb8d Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sun, 23 Apr 2023 09:33:47 +0200 Subject: [PATCH] added an mixin for reloading and restarting changed services --- plugins/module_utils/module.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugins/module_utils/module.py b/plugins/module_utils/module.py index aa3fe1b..d890f82 100644 --- a/plugins/module_utils/module.py +++ b/plugins/module_utils/module.py @@ -345,3 +345,16 @@ def installable(_class: Type[SystemdUnitModule]): _class.install = install _class.module_spec = specs return _class + + +class SystemdReloadMixin(SystemdUnitModule): + def post(self): + if not self.changed: + return + systemctl = self.module.get_bin_path("systemctl", required=True) + self.module.run_command([systemctl, "daemon-reload"], check_rc=True) + if self.unitfile.stem.endswith("@"): + return + (rc, _, _) = self.module.run_command([systemctl, "is-enabled", self.unitfile.name], check_rc=False) + if rc == 0: + self.module.run_command([systemctl, "restart", self.unitfile.name], check_rc=True)