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

188 Zeilen
5.7 KiB
Python

#!/usr/bin/python3
import pathlib
from typing import List
2023-04-15 22:10:13 +00:00
try:
2023-04-23 08:32:42 +00:00
from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, modspec
2023-04-20 22:17:36 +00:00
from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule
2023-04-15 22:10:13 +00:00
except ImportError:
2023-04-23 08:32:42 +00:00
from plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, modspec
2023-04-15 22:10:13 +00:00
from plugins.module_utils.module import SystemdUnitModule
2023-04-23 08:32:42 +00:00
class Module(SystemdUnitModule): # type: ignore
"""generates an systemd-networkd link"""
2023-04-20 20:09:58 +00:00
name = "link"
2023-04-23 08:32:42 +00:00
module_spec = modspec(
argument_spec=dict(
mac=Types.str(help="The Mac address of the device"),
2023-04-20 22:17:36 +00:00
permanentmac=Types.str(help="The Permanent Mac address advertised by the device"),
path=Types.str(help="A shell-style glob matching the persistent path, as exposed by the udev property ID_PATH."),
driver=Types.str(help="A glob matching the driver currently bound to the device"),
type=Types.str(help="A glob matching the device type, as exposed by networkctl list"),
kind=Types.str(help="a glob matching the device kind, as exposed by networkctl status INTERFACE or ip -d link show INTERFACE."),
2023-04-15 10:33:19 +00:00
description=Types.str(help="The description for the link"),
name=Types.str(required=True, help="The new name of the device"),
mtu=Types.int(help="The maximum Transmission unit for the link"),
),
required_one_of=(
("mac", "permanentmac", "path", "driver", "type", "kind"),
("name", "mac", "permanentmac"),
),
)
def prepare(self):
self.__unit = None
2023-04-20 22:17:36 +00:00
newname = self.get("name", "") or self.get("mac", "") or self.get("permanentmac", "")
newname = newname.replace(":", "").replace("/", "-").lower()
2023-04-20 22:17:36 +00:00
self.unitfile = SYSTEMD_NETWORK_CONFIG.joinpath("50-" + newname).with_suffix(".link")
def unit(self) -> str:
if self.__unit is None:
self.__unit = "\n".join((self.match(), self.link()))
return self.__unit
def match(self) -> str:
2023-04-23 08:32:42 +00:00
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 = []
if self.get("description", False):
options.append("Description={}\n".format(self.get("description", False)))
if self.get("name", False):
options.append("Name={}\n".format(self.get("name", False)))
if self.get("mtu", False):
options.append("MTUBytes={}\n".format(self.get("mtu", False)))
return "[Link]\n" + "".join(options)
def post(self):
if not self.changed:
return
args = [
"/usr/bin/udevadm",
"trigger",
"-c",
"add",
]
if self.module.check_mode:
args.append("-n")
if self.get("mac", False):
args.append("--attr-match=address={}".format(self.get("mac")))
if self.get("path", False):
args.append(self.get("path"))
self.module.run_command(args, check_rc=True)
DOCUMENTATION = """---
description:
- generates an systemd-networkd link
2023-04-20 20:09:58 +00:00
module: link
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:
- The description for the link
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
driver:
description:
- A glob matching the driver currently bound to the device
required: false
type: str
kind:
description:
- a glob matching the device kind, as exposed by networkctl status INTERFACE or
ip -d link show INTERFACE.
required: false
type: str
mac:
description:
- The Mac address of the device
required: false
type: str
mtu:
description:
- The maximum Transmission unit for the link
required: false
type: int
name:
description:
- The new name of the device
2023-04-15 10:37:03 +00:00
required: true
type: str
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
path:
description:
- A shell-style glob matching the persistent path, as exposed by the udev property
ID_PATH.
required: false
type: str
permanentmac:
description:
- The Permanent Mac address advertised by the device
required: false
type: str
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
type:
description:
- A glob matching the device type, as exposed by networkctl list
required: false
type: str
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
short_description: generates an systemd-networkd link
"""
if __name__ == "__main__":
Module()()