1
0
Fork 0

added map_param to ease the generation simple parameters for systemd.

it reduces the boilerplate for simple key-value parameters
Dieser Commit ist enthalten in:
Sebastian Tobie 2023-04-23 09:23:15 +02:00
Ursprung f2f3580337
Commit f7880850dc
1 geänderte Dateien mit 23 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -230,22 +230,31 @@ class SystemdUnitModule(AnsibleModule):
def header(self) -> str: def header(self) -> str:
header = "[Unit]\n" header = "[Unit]\n"
if self.get("description", False): header += "\n".join(
header += "Description={}\n".format(self.get("description")) self.map_param(
if self.get("documentation", False): description="Description",
header += "Documentation={}\n".format(" ".join(self.get("documentation"))) documentation="Documentation",
if self.get("requires", False): requires="Requires",
header += "Requires={}\n".format(" ".join(self.get("requires"))) wants="Wants",
if self.get("wants", False): partof="PartOf",
header += "Wants={}\n".format(" ".join(self.get("wants"))) before="Before",
if self.get("partof", False): after="After",
header += "PartOf={}\n".format(" ".join(self.get("partof"))) )
if self.get("before", False): )
header += "Before={}\n".format(" ".join(self.get("before")))
if self.get("after", False):
header += "After={}\n".format(" ".join(self.get("after")))
return header return header
def map_param(self, **parammap: str):
"""maps an dict with keys for an section with given params. The key of the dictionary is the parameter and the value is the key in the unitfile. If an parameter has multiple values it adds multiple entries"""
output: list[str] = []
for param, key in parammap:
if self.params[param] is not None:
params = self.params[param]
if isinstance(params, list):
output.extend(("{}={}".format(key, p) for p in params))
else:
output.append("{}={}".format(key, self.params[param]))
return output
def unitfile_gen(self): def unitfile_gen(self):
path = self.tmpdir / "newunit" path = self.tmpdir / "newunit"
with open(path, "w") as unit: with open(path, "w") as unit: