From afa7299fcd7cb577fd32cd1c693f982ae2ddc752 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sun, 23 Apr 2023 10:32:42 +0200 Subject: [PATCH] updated units to the current state --- plugins/modules/link.py | 31 ++++----- plugins/modules/mount.py | 21 ++----- plugins/modules/network.py | 18 +++--- plugins/modules/system_service.py | 100 ++++++++++++++++++++++-------- plugins/modules/target.py | 37 ++++++----- 5 files changed, 125 insertions(+), 82 deletions(-) diff --git a/plugins/modules/link.py b/plugins/modules/link.py index 5beedd9..ddb6917 100644 --- a/plugins/modules/link.py +++ b/plugins/modules/link.py @@ -3,18 +3,18 @@ import pathlib from typing import List try: - from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types + from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, modspec from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule except ImportError: - from plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types + from plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, modspec from plugins.module_utils.module import SystemdUnitModule -class Module(SystemdUnitModule): +class Module(SystemdUnitModule): # type: ignore """generates an systemd-networkd link""" name = "link" - module_spec = dict( + module_spec = modspec( argument_spec=dict( mac=Types.str(help="The Mac address of the device"), permanentmac=Types.str(help="The Permanent Mac address advertised by the device"), @@ -44,20 +44,15 @@ class Module(SystemdUnitModule): return self.__unit def match(self) -> str: - options = [] - if self.get("mac", False): - options.append("MACAddress={}\n".format(self.get("mac", False))) - if self.get("permanentmac", False): - options.append("PermanentAddress={}\n".format(self.get("permanentmac", False))) - if self.get("path", False): - options.append("Path={}\n".format(self.get("path", False))) - if self.get("driver", False): - options.append("Driver={}\n".format(self.get("driver", False))) - if self.get("type", False): - options.append("Type={}\n".format(self.get("type", False))) - if self.get("kind", False): - options.append("Kind={}\n".format(self.get("kind", False))) - return "[Match]\n" + "".join(options) + options = self.map_param( + mac="MACAddress", + permanentmac="PermanentAddress", + path="Path", + driver="Driver", + type="Type", + kind="Kind", + ) + return "[Match]\n" + "\n".join(options) def link(self) -> str: options = [] diff --git a/plugins/modules/mount.py b/plugins/modules/mount.py index 7d1c876..38830f4 100644 --- a/plugins/modules/mount.py +++ b/plugins/modules/mount.py @@ -3,11 +3,11 @@ import pathlib from typing import List, Optional try: - from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types - from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule, installable + from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, modspec + from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable except ImportError: - from plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types - from plugins.module_utils.module import SystemdUnitModule, installable + from plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, modspec + from plugins.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable SYSTEMD_SERVICE_CONFIG = pathlib.Path("/etc/systemd/system") @@ -18,11 +18,11 @@ OPTION_MAPPING = dict( @installable -class Module(SystemdUnitModule): +class Module(SystemdUnitModule, SystemdReloadMixin): # type: ignore """Creates an systemd mount""" name = "mount" - module_spec = dict( + module_spec = modspec( argument_spec=dict( fs=Types.str(required=True, help="The filesystem that is used for the mount"), where=Types.path(required=True, help="The Path where the filesystem is mounted to"), @@ -66,15 +66,6 @@ class Module(SystemdUnitModule): output += "Options={}\n".format(",".join(self.get("options"))) return output - 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) - (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) - DOCUMENTATION = """--- description: diff --git a/plugins/modules/network.py b/plugins/modules/network.py index d8f37a4..1d57a23 100644 --- a/plugins/modules/network.py +++ b/plugins/modules/network.py @@ -3,25 +3,25 @@ import pathlib from typing import List, Union try: - from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types - from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule, systemdbool + from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, modspec, systemdbool + from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule except ImportError: - from plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, systemdbool + from plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, modspec, systemdbool from plugins.module_utils.module import SystemdUnitModule -class Module(SystemdUnitModule): +class Module(SystemdUnitModule): # type: ignore """Sets up the systemd network unit""" name = "network" - module_spec = dict( + module_spec = modspec( argument_spec=dict( mac=Types.str(help="The MAC-Address of the device"), device=Types.str(help="The name of the network device"), name=Types.str(required=True, help="name of the unit"), dot=Types.bool(help="if DNS-over-TLS should be required or disabled. If it is unset, it will used if the server supports it"), dnssec=Types.bool( - "if the Domainqueries should require DNSSEC or not. If its missing, domains that have DNSSEC enabled will be validated, all others it will be assumed to be okay." + help="if the Domainqueries should require DNSSEC or not.\nIf its missing, domains that have DNSSEC enabled will be validated, all others it will be assumed to be okay." ), dns=Types.list(elements=str, help="List of DNS-Servers"), domain=Types.list(elements=str, help="List of domains that are on this device"), @@ -151,7 +151,11 @@ options: required: false type: list dnssec: - required: true + description: + - if the Domainqueries should require DNSSEC or not. + - If its missing, domains that have DNSSEC enabled will be validated, all others + it will be assumed to be okay. + required: false type: bool documentation: default: [] diff --git a/plugins/modules/system_service.py b/plugins/modules/system_service.py index a7257f9..8cc69cc 100644 --- a/plugins/modules/system_service.py +++ b/plugins/modules/system_service.py @@ -4,22 +4,22 @@ from typing import List, Union try: from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types - from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule, installable + from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable except ImportError: from plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types - from plugins.module_utils.module import SystemdUnitModule, installable + from plugins.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable @installable -class Module(SystemdUnitModule): +class Module(SystemdUnitModule, SystemdReloadMixin): """Creates System Services units""" name = "system_service" module_spec = dict( argument_spec=dict( - name=Types.str(required=True), - user=Types.str(), - group=Types.str(), + name=Types.str(required=True, help="Name of the service"), + serviceuser=Types.str(help="Username of under which the commands run at.", default="root"), + servicegroup=Types.str(help="Group of under which the commands run at.", default="root"), type=Types.str( choices=("simple", "exec", "forking", "oneshot", "dbus", "notify", "notify-reload", "idle"), default="simple", @@ -38,32 +38,39 @@ class Module(SystemdUnitModule): help="command or list of commands that are started as main programm. Multiple commands are only allowed in a oneshot command", ), post=Types.list(str, help="Command or list of commands that are started after the main command(s) stopped without problems."), + environmentfile=Types.list( + str, help="List of file that are containing environment variables. They are evaluated before each pre/start/post command" + ), + environment=Types.list( + Types.dict( + name=Types.str(help="name of the Environment variable", required=True), + value=Types.str(help="value of the Environment variable", required=True), + ), + help="List of environment variables that are set to each command before they run", + ), + workingdirectory=Types.str(help="The Directory that is used for the processes as current working directory"), ), ) def prepare(self): - self.unitfile = (SYSTEMD_SERVICE_CONFIG / self.get("name")).with_stem(".service") + self.unitfile = (SYSTEMD_SERVICE_CONFIG / self.get("name")).with_suffix(".service") self.__unit = None if self.get("type", "simple") != "oneshot" and len(self.get("start")) > 1: self.module.fail_json("only oneshot services are allowed to have multiple start commands", **self.result) def service(self): section = "[Service]\n" - if self.get("type"): - section += "Type=".format(self.get("type")) - if self.get("pre", False): - for cmd in self.get("pre"): - section += "ExecStartPre={}".format(cmd) - if self.get("start", False): - for cmd in self.get("start"): - section += "ExecStart={}".format(cmd) - if self.get("post", False): - for cmd in self.get("post"): - section += "ExecStartPost={}".format(cmd) - if self.get("user", False): - section += "User={}".format(self.get("user")) - if self.get("group", False): - section += "Group={}".format(self.get("group")) + section += "\n".join( + self.map_param( + type="Type", + pre="ExecStartPre", + start="ExecStart", + post="ExecStartPost", + serviceuser="User", + servicegroup="Group", + workingdirectory="WorkingDirectory", + ) + ) return section def unit(self) -> str: @@ -109,10 +116,35 @@ options: elements: str required: false type: list - group: + environment: + default: [] + description: + - List of environment variables that are set to each command before they run + elements: dict + options: + name: + description: + - name of the Environment variable + required: true + type: str + value: + description: + - value of the Environment variable + required: true + type: str required: false - type: str + type: list + environmentfile: + default: [] + description: + - List of file that are containing environment variables. They are evaluated before + each pre/start/post command + elements: str + required: false + type: list name: + description: + - Name of the service required: true type: str partof: @@ -154,6 +186,18 @@ options: elements: str required: false type: list + servicegroup: + default: root + description: + - Group of under which the commands run at. + required: false + type: str + serviceuser: + default: root + description: + - Username of under which the commands run at. + required: false + type: str start: description: - command or list of commands that are started as main programm. Multiple commands @@ -187,9 +231,6 @@ options: - idle is similar to simple, but it can delay the start up by a few seconds. required: false type: str - user: - required: false - type: str wanted_by: default: [] description: @@ -206,6 +247,11 @@ options: elements: str required: false type: list + workingdirectory: + description: + - The Directory that is used for the processes as current working directory + required: false + type: str short_description: Creates System Services units """ diff --git a/plugins/modules/target.py b/plugins/modules/target.py index e06f35a..8e275ce 100644 --- a/plugins/modules/target.py +++ b/plugins/modules/target.py @@ -4,39 +4,39 @@ from typing import List, Union try: from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, systemdbool - from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule, installable + from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable except ImportError: from plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, systemdbool - from plugins.module_utils.module import SystemdUnitModule, installable + from plugins.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable + +__module_name__ = "TargetModule" @installable -class Module(SystemdUnitModule): +class TargetModule(SystemdUnitModule, SystemdReloadMixin): """Creates Target units""" name = "target" module_spec = dict( argument_spec=dict( - description=Types.str(), - name=Types.str(required=True), - allow_isolate=Types.bool(default=False), + description=Types.str(help="description of the target"), + name=Types.str(required=True, help="name of the target"), + allow_isolate=Types.bool( + default=False, + help="allows admins to restrict the system to only start units that are wanted by this unit and subsequent units", + ), ), ) def prepare(self): - try: - self.unitfile = (SYSTEMD_SERVICE_CONFIG/ self.get("name")).with_stem(".target") - self.set("name", self.get("name")) - except KeyError: - self.set("params", self.params) + self.unitfile = (SYSTEMD_SERVICE_CONFIG / self.get("name")).with_suffix(".target") self.__unit = None def header(self) -> str: section = super().header() - section += "AllowIsolate={}".format(systemdbool(self.get("allow_isolate", False))) + section += "AllowIsolate={}\n".format(systemdbool(self.get("allow_isolate", False))) return section - def unit(self) -> str: if self.__unit is None: self.__unit = "\n".join( @@ -62,6 +62,9 @@ options: type: list allow_isolate: default: false + description: + - allows admins to restrict the system to only start units that are wanted by + this unit and subsequent units required: false type: bool before: @@ -72,7 +75,9 @@ options: required: false type: list description: - required: true + description: + - description of the target + required: false type: str documentation: default: [] @@ -82,6 +87,8 @@ options: required: false type: list name: + description: + - name of the target required: true type: str partof: @@ -128,4 +135,4 @@ short_description: Creates Target units """ if __name__ == "__main__": - Module()() + TargetModule()()