2023-04-14 22:58:51 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
import pathlib
|
|
|
|
from typing import List
|
|
|
|
|
2024-02-11 13:35:00 +00:00
|
|
|
try:
|
2024-02-11 13:39:52 +00:00
|
|
|
from ansible_module.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, modspec
|
|
|
|
from ansible_module.module_utils.module import SystemdUnitModule
|
2024-02-11 13:35:00 +00:00
|
|
|
except ImportError:
|
|
|
|
from ansible.module_utils.sebastian.base.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, modspec
|
|
|
|
from ansible.module_utils.sebastian.base.module_utils.module import SystemdUnitModule
|
2023-04-15 22:10:13 +00:00
|
|
|
|
2023-04-14 22:58:51 +00:00
|
|
|
|
2023-04-23 08:32:42 +00:00
|
|
|
class Module(SystemdUnitModule): # type: ignore
|
2023-04-14 22:58:51 +00:00
|
|
|
"""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(
|
2023-04-14 22:58:51 +00:00
|
|
|
argument_spec=dict(
|
2023-07-15 20:25:59 +00:00
|
|
|
mac=Types.str(help="The Mac address of the device." "An ! before the value matches anything but this value."),
|
|
|
|
permanentmac=Types.str(
|
|
|
|
help="The Permanent Mac address advertised by the device. " "An ! before the value matches anything but this value."
|
|
|
|
),
|
|
|
|
path=Types.str(
|
|
|
|
help="A shell-style glob matching the persistent path, as exposed by the udev property ID_PATH. "
|
|
|
|
"An ! before the value matches anything but this value."
|
|
|
|
),
|
|
|
|
driver=Types.str(
|
|
|
|
help="A glob matching the driver currently bound to the device. " "An ! before the value matches anything but this value."
|
|
|
|
),
|
|
|
|
type=Types.str(
|
|
|
|
help="A glob matching the device type, as exposed by networkctl list. "
|
|
|
|
"An ! before the value matches anything but this value."
|
|
|
|
),
|
|
|
|
kind=Types.str(
|
|
|
|
help="a glob matching the device kind, as exposed by networkctl status INTERFACE or ip -d link show INTERFACE. "
|
|
|
|
"An ! before the value matches anything but this value."
|
|
|
|
),
|
|
|
|
virtualization=Types.str(help="The virtualization type. An ! before the value matches anything but this value."),
|
2023-04-15 10:33:19 +00:00
|
|
|
description=Types.str(help="The description for the link"),
|
2023-04-14 22:58:51 +00:00
|
|
|
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", "")
|
2023-04-14 22:58:51 +00:00
|
|
|
newname = newname.replace(":", "").replace("/", "-").lower()
|
2023-04-20 22:17:36 +00:00
|
|
|
self.unitfile = SYSTEMD_NETWORK_CONFIG.joinpath("50-" + newname).with_suffix(".link")
|
2023-04-14 22:58:51 +00:00
|
|
|
|
|
|
|
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",
|
2023-07-15 20:25:59 +00:00
|
|
|
virtualization="Virtualization",
|
2023-04-23 08:32:42 +00:00
|
|
|
)
|
2023-04-26 16:58:30 +00:00
|
|
|
return "[Match]\n" + "".join(options)
|
2023-04-14 22:58:51 +00:00
|
|
|
|
|
|
|
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
|
2023-04-14 22:58:51 +00:00
|
|
|
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
|
2023-04-14 22:58:51 +00:00
|
|
|
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
|
2023-04-14 22:58:51 +00:00
|
|
|
driver:
|
|
|
|
description:
|
2023-07-15 20:25:59 +00:00
|
|
|
- A glob matching the driver currently bound to the device. An ! before the value
|
|
|
|
matches anything but this value.
|
2023-04-14 22:58:51 +00:00
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
kind:
|
|
|
|
description:
|
|
|
|
- a glob matching the device kind, as exposed by networkctl status INTERFACE or
|
2023-07-15 20:25:59 +00:00
|
|
|
ip -d link show INTERFACE. An ! before the value matches anything but this value.
|
2023-04-14 22:58:51 +00:00
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
mac:
|
|
|
|
description:
|
2023-07-15 20:25:59 +00:00
|
|
|
- The Mac address of the device.An ! before the value matches anything but this
|
|
|
|
value.
|
2023-04-14 22:58:51 +00:00
|
|
|
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
|
2023-04-14 22:58:51 +00:00
|
|
|
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
|
2023-04-14 22:58:51 +00:00
|
|
|
path:
|
|
|
|
description:
|
|
|
|
- A shell-style glob matching the persistent path, as exposed by the udev property
|
2023-07-15 20:25:59 +00:00
|
|
|
ID_PATH. An ! before the value matches anything but this value.
|
2023-04-14 22:58:51 +00:00
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
permanentmac:
|
|
|
|
description:
|
2023-07-15 20:25:59 +00:00
|
|
|
- The Permanent Mac address advertised by the device. An ! before the value matches
|
|
|
|
anything but this value.
|
2023-04-14 22:58:51 +00:00
|
|
|
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
|
2023-04-14 22:58:51 +00:00
|
|
|
type:
|
|
|
|
description:
|
2023-07-15 20:25:59 +00:00
|
|
|
- A glob matching the device type, as exposed by networkctl list. An ! before
|
|
|
|
the value matches anything but this value.
|
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
virtualization:
|
|
|
|
description:
|
|
|
|
- The virtualization type. An ! before the value matches anything but this value.
|
2023-04-14 22:58:51 +00:00
|
|
|
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
|
2023-04-14 22:58:51 +00:00
|
|
|
short_description: generates an systemd-networkd link
|
|
|
|
"""
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Module()()
|