1
0
Fork 0

updated units to the current state

Dieser Commit ist enthalten in:
Sebastian Tobie 2023-04-23 10:32:42 +02:00
Ursprung 7bec7376db
Commit afa7299fcd
5 geänderte Dateien mit 125 neuen und 82 gelöschten Zeilen

Datei anzeigen

@ -3,18 +3,18 @@ import pathlib
from typing import List from typing import List
try: 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 from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule
except ImportError: 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 from plugins.module_utils.module import SystemdUnitModule
class Module(SystemdUnitModule): class Module(SystemdUnitModule): # type: ignore
"""generates an systemd-networkd link""" """generates an systemd-networkd link"""
name = "link" name = "link"
module_spec = dict( module_spec = modspec(
argument_spec=dict( argument_spec=dict(
mac=Types.str(help="The Mac address of the device"), mac=Types.str(help="The Mac address of the device"),
permanentmac=Types.str(help="The Permanent Mac address advertised by the device"), permanentmac=Types.str(help="The Permanent Mac address advertised by the device"),
@ -44,20 +44,15 @@ class Module(SystemdUnitModule):
return self.__unit return self.__unit
def match(self) -> str: def match(self) -> str:
options = [] options = self.map_param(
if self.get("mac", False): mac="MACAddress",
options.append("MACAddress={}\n".format(self.get("mac", False))) permanentmac="PermanentAddress",
if self.get("permanentmac", False): path="Path",
options.append("PermanentAddress={}\n".format(self.get("permanentmac", False))) driver="Driver",
if self.get("path", False): type="Type",
options.append("Path={}\n".format(self.get("path", False))) kind="Kind",
if self.get("driver", False): )
options.append("Driver={}\n".format(self.get("driver", False))) return "[Match]\n" + "\n".join(options)
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)
def link(self) -> str: def link(self) -> str:
options = [] options = []

Datei anzeigen

@ -3,11 +3,11 @@ import pathlib
from typing import List, Optional from typing import List, Optional
try: try:
from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types 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 SystemdUnitModule, installable from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable
except ImportError: except ImportError:
from plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types from plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, modspec
from plugins.module_utils.module import SystemdUnitModule, installable from plugins.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable
SYSTEMD_SERVICE_CONFIG = pathlib.Path("/etc/systemd/system") SYSTEMD_SERVICE_CONFIG = pathlib.Path("/etc/systemd/system")
@ -18,11 +18,11 @@ OPTION_MAPPING = dict(
@installable @installable
class Module(SystemdUnitModule): class Module(SystemdUnitModule, SystemdReloadMixin): # type: ignore
"""Creates an systemd mount""" """Creates an systemd mount"""
name = "mount" name = "mount"
module_spec = dict( module_spec = modspec(
argument_spec=dict( argument_spec=dict(
fs=Types.str(required=True, help="The filesystem that is used for the mount"), 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"), 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"))) output += "Options={}\n".format(",".join(self.get("options")))
return output 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 = """--- DOCUMENTATION = """---
description: description:

Datei anzeigen

@ -3,25 +3,25 @@ import pathlib
from typing import List, Union from typing import List, Union
try: 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, systemdbool
from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule, systemdbool from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule
except ImportError: 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 from plugins.module_utils.module import SystemdUnitModule
class Module(SystemdUnitModule): class Module(SystemdUnitModule): # type: ignore
"""Sets up the systemd network unit""" """Sets up the systemd network unit"""
name = "network" name = "network"
module_spec = dict( module_spec = modspec(
argument_spec=dict( argument_spec=dict(
mac=Types.str(help="The MAC-Address of the device"), mac=Types.str(help="The MAC-Address of the device"),
device=Types.str(help="The name of the network device"), device=Types.str(help="The name of the network device"),
name=Types.str(required=True, help="name of the unit"), 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"), 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( 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"), dns=Types.list(elements=str, help="List of DNS-Servers"),
domain=Types.list(elements=str, help="List of domains that are on this device"), domain=Types.list(elements=str, help="List of domains that are on this device"),
@ -151,7 +151,11 @@ options:
required: false required: false
type: list type: list
dnssec: 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 type: bool
documentation: documentation:
default: [] default: []

Datei anzeigen

@ -4,22 +4,22 @@ from typing import List, Union
try: try:
from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types 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: except ImportError:
from plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types 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 @installable
class Module(SystemdUnitModule): class Module(SystemdUnitModule, SystemdReloadMixin):
"""Creates System Services units""" """Creates System Services units"""
name = "system_service" name = "system_service"
module_spec = dict( module_spec = dict(
argument_spec=dict( argument_spec=dict(
name=Types.str(required=True), name=Types.str(required=True, help="Name of the service"),
user=Types.str(), serviceuser=Types.str(help="Username of under which the commands run at.", default="root"),
group=Types.str(), servicegroup=Types.str(help="Group of under which the commands run at.", default="root"),
type=Types.str( type=Types.str(
choices=("simple", "exec", "forking", "oneshot", "dbus", "notify", "notify-reload", "idle"), choices=("simple", "exec", "forking", "oneshot", "dbus", "notify", "notify-reload", "idle"),
default="simple", 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", 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."), 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): 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 self.__unit = None
if self.get("type", "simple") != "oneshot" and len(self.get("start")) > 1: 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) self.module.fail_json("only oneshot services are allowed to have multiple start commands", **self.result)
def service(self): def service(self):
section = "[Service]\n" section = "[Service]\n"
if self.get("type"): section += "\n".join(
section += "Type=".format(self.get("type")) self.map_param(
if self.get("pre", False): type="Type",
for cmd in self.get("pre"): pre="ExecStartPre",
section += "ExecStartPre={}".format(cmd) start="ExecStart",
if self.get("start", False): post="ExecStartPost",
for cmd in self.get("start"): serviceuser="User",
section += "ExecStart={}".format(cmd) servicegroup="Group",
if self.get("post", False): workingdirectory="WorkingDirectory",
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"))
return section return section
def unit(self) -> str: def unit(self) -> str:
@ -109,10 +116,35 @@ options:
elements: str elements: str
required: false required: false
type: list 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 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: name:
description:
- Name of the service
required: true required: true
type: str type: str
partof: partof:
@ -154,6 +186,18 @@ options:
elements: str elements: str
required: false required: false
type: list 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: start:
description: description:
- command or list of commands that are started as main programm. Multiple commands - 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. - idle is similar to simple, but it can delay the start up by a few seconds.
required: false required: false
type: str type: str
user:
required: false
type: str
wanted_by: wanted_by:
default: [] default: []
description: description:
@ -206,6 +247,11 @@ options:
elements: str elements: str
required: false required: false
type: list 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 short_description: Creates System Services units
""" """

Datei anzeigen

@ -4,39 +4,39 @@ from typing import List, Union
try: try:
from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, systemdbool 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: except ImportError:
from plugins.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, systemdbool 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 @installable
class Module(SystemdUnitModule): class TargetModule(SystemdUnitModule, SystemdReloadMixin):
"""Creates Target units""" """Creates Target units"""
name = "target" name = "target"
module_spec = dict( module_spec = dict(
argument_spec=dict( argument_spec=dict(
description=Types.str(), description=Types.str(help="description of the target"),
name=Types.str(required=True), name=Types.str(required=True, help="name of the target"),
allow_isolate=Types.bool(default=False), 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): def prepare(self):
try: self.unitfile = (SYSTEMD_SERVICE_CONFIG / self.get("name")).with_suffix(".target")
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.__unit = None self.__unit = None
def header(self) -> str: def header(self) -> str:
section = super().header() section = super().header()
section += "AllowIsolate={}".format(systemdbool(self.get("allow_isolate", False))) section += "AllowIsolate={}\n".format(systemdbool(self.get("allow_isolate", False)))
return section return section
def unit(self) -> str: def unit(self) -> str:
if self.__unit is None: if self.__unit is None:
self.__unit = "\n".join( self.__unit = "\n".join(
@ -62,6 +62,9 @@ options:
type: list type: list
allow_isolate: allow_isolate:
default: false default: false
description:
- allows admins to restrict the system to only start units that are wanted by
this unit and subsequent units
required: false required: false
type: bool type: bool
before: before:
@ -72,7 +75,9 @@ options:
required: false required: false
type: list type: list
description: description:
required: true description:
- description of the target
required: false
type: str type: str
documentation: documentation:
default: [] default: []
@ -82,6 +87,8 @@ options:
required: false required: false
type: list type: list
name: name:
description:
- name of the target
required: true required: true
type: str type: str
partof: partof:
@ -128,4 +135,4 @@ short_description: Creates Target units
""" """
if __name__ == "__main__": if __name__ == "__main__":
Module()() TargetModule()()