1
0
Fork 0
ansible-systemd/plugins/modules/mount.py

173 Zeilen
4.7 KiB
Python

#!/usr/bin/python3
import pathlib
from typing import List, Optional
2023-04-15 17:30:04 +00:00
from ansible_module.generic import SYSTEMD_SERVICE_CONFIG, Types, modspec
from ansible_module.module import SystemdReloadMixin, SystemdUnitModule, installable
2023-04-15 22:10:13 +00:00
SYSTEMD_SERVICE_CONFIG = pathlib.Path("/etc/systemd/system")
OPTION_MAPPING = dict(
required_by="RequiredBy",
wanted_by="WantedBy",
)
2023-04-20 22:19:35 +00:00
@installable
2023-04-23 08:32:42 +00:00
class Module(SystemdUnitModule, SystemdReloadMixin): # type: ignore
"""Creates an systemd mount"""
2023-04-20 20:09:58 +00:00
name = "mount"
2023-04-23 08:32:42 +00:00
module_spec = modspec(
argument_spec=dict(
2023-04-20 22:17:36 +00:00
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"])
2023-04-20 22:17:36 +00:00
self.unitfile = SYSTEMD_SERVICE_CONFIG.joinpath(self.mountdir.relative_to("/").as_posix().replace("/", "-")).with_suffix(".mount")
self.__unit = None
2023-04-20 22:21:55 +00:00
if self.get("description", False) is False:
2023-05-01 08:29:21 +00:00
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:
2023-04-20 22:17:36 +00:00
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
2023-04-20 20:09:58 +00:00
module: mount
options:
2023-04-20 22:19:35 +00:00
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
2023-04-20 22:19:35 +00:00
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
2023-04-20 22:19:35 +00:00
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
2023-04-20 22:19:35 +00:00
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
2023-04-20 22:19:35 +00:00
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()()