#!/usr/bin/python3 import pathlib from typing import List, Optional try: from ansible_module.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, modspec from ansible_module.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable except ImportError: from ansible.module_utils.sebastian.base.module_utils.generic import SYSTEMD_SERVICE_CONFIG, Types, modspec from ansible.module_utils.sebastian.base.module_utils.module import SystemdReloadMixin, SystemdUnitModule, installable SYSTEMD_SERVICE_CONFIG = pathlib.Path("/etc/systemd/system") OPTION_MAPPING = dict( required_by="RequiredBy", wanted_by="WantedBy", ) @installable class Module(SystemdUnitModule, SystemdReloadMixin): # type: ignore """Creates an systemd mount""" name = "mount" 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"), what=Types.str(required=True, help="The device or an string that will be mounted"), state=Types.str( choices=("present", "absent"), default="present", help="the state the mount is", ), options=Types.list(elements=str, help="The options for the mount"), ), ) def prepare(self): self.mountdir = pathlib.Path(self.params["where"]) self.unitfile = SYSTEMD_SERVICE_CONFIG.joinpath(self.mountdir.relative_to("/").as_posix().replace("/", "-")).with_suffix(".mount") self.__unit = None if self.get("description", False) is False: self.params["description"] = "Mount for {}".format(self.mountdir.as_posix()) def unit(self) -> str: if self.__unit is None: self.__unit = "\n".join( ( self.header(), self.mount(), self.install(), ) ) return self.__unit def header(self) -> str: return "[Unit]\nDescription={}\n".format(self.get("description", "Mount for {}".format(self.get("where")))) def mount(self) -> str: output = "[Mount]\n" output += "Where={}\n".format(self.get("where")) output += "What={}\n".format(self.get("what")) output += "Type={}\n".format(self.get("fs")) if self.get("options", False): output += "Options={}\n".format(",".join(self.get("options"))) return output DOCUMENTATION = """--- description: - Creates an systemd mount module: mount options: after: default: [] description: - list of units that this unit wants to be started after this unit elements: str required: false type: list before: default: [] description: - list of units that this unit needs to be started before this unit. elements: str required: false type: list description: description: - An description for programs that access systemd required: false type: str documentation: default: [] description: - Paths where documentation can be found elements: str required: false type: list fs: description: - The filesystem that is used for the mount required: true type: str options: default: [] description: - The options for the mount elements: str required: false type: list partof: default: [] description: - list of units that this unit is part of. - If the restart this unit does it too, but if this restarts it does not affect the other units. elements: str required: false type: list required_by: default: [] description: - systemd units that require this mount elements: str required: false type: list requires: default: [] description: - list of units that this unit requires. If it fails or can't be started this unit fails. without before/after this is started at the same time elements: str required: false type: list state: choices: - present - absent default: present description: - the state the mount is required: false type: str wanted_by: default: [] description: - systemd units that want the mount, but not explicitly require it. Commonly used for target if not service explicitly require it. elements: str required: false type: list wants: default: [] description: - list of units that this unit wants. If it fails or can't be started it does not affect this unit elements: str required: false type: list what: description: - The device or an string that will be mounted required: true type: str where: description: - The Path where the filesystem is mounted to required: true type: path short_description: Creates an systemd mount """ if __name__ == "__main__": Module()()