Commits vergleichen

...

2 Commits

Autor SHA1 Nachricht Datum
d82a902043 fixed an issue with choices in lists 2025-03-17 21:11:22 +01:00
328e58c439 improved the filemoving of AnsibleModules and SystemdUnitModule
I wrapped the file stuff in an function that is available in AnsibleModule.
2025-03-17 21:10:59 +01:00
2 geänderte Dateien mit 31 neuen und 44 gelöschten Zeilen

Datei anzeigen

@ -124,9 +124,8 @@ class Types(metaclass=meta):
f"helptext of option {name} is unset." f"helptext of option {name} is unset."
" Ansible requires suboptions to have an documentation" " Ansible requires suboptions to have an documentation"
) )
elif elements["type"] == "list": elif "choices" in elements:
if "choices" in elements: option["choices"] = elements["choices"]
option["choices"] = elements["choices"]
if default is not None: if default is not None:
option["default"] = default option["default"] = default
if help is not None and isinstance(help, str): if help is not None and isinstance(help, str):

Datei anzeigen

@ -1,9 +1,9 @@
import pathlib import pathlib
from copy import deepcopy from copy import deepcopy
from typing import (Any, Callable, ClassVar, Dict, NoReturn, Optional, Type, from typing import Any, Callable, ClassVar, Dict, NoReturn, Optional, Type, TypedDict, TypeVar, Union, overload, Generic
TypedDict, TypeVar, Union, overload)
import ansible.module_utils.basic as basic import ansible.module_utils.basic as basic
import os
import shutil
from .generic import AnsibleParameter, Types, systemdbool from .generic import AnsibleParameter, Types, systemdbool
@ -18,11 +18,11 @@ __all__ = (
T = TypeVar("T") T = TypeVar("T")
class TypedDiff(TypedDict): class TypedDiff(Generic[T], TypedDict, total=False):
before: str before: T
after: str after: T
before_header: Optional[str] before_header: str
after_header: Optional[str] after_header: str
def docify(input: Union[dict, AnsibleParameter]) -> dict: def docify(input: Union[dict, AnsibleParameter]) -> dict:
@ -78,6 +78,7 @@ class AnsibleModule(object):
specs["argument_spec"].update(modspec["argument_spec"]) specs["argument_spec"].update(modspec["argument_spec"])
del modspec["argument_spec"] del modspec["argument_spec"]
specs.update(modspec) specs.update(modspec)
self.modspec = specs
self.module = basic.AnsibleModule(**specs) self.module = basic.AnsibleModule(**specs)
self.tmpdir = pathlib.Path(self.module.tmpdir) self.tmpdir = pathlib.Path(self.module.tmpdir)
@ -114,7 +115,7 @@ class AnsibleModule(object):
if diff is not None and not any((before is not None, after is not None)): if diff is not None and not any((before is not None, after is not None)):
pass pass
elif all((before is not None, after is not None, diff is None)): elif all((before is not None, after is not None, diff is None)):
diff = dict( diff = TypedDiff(
before=before, before=before,
after=after, after=after,
) )
@ -206,6 +207,24 @@ class AnsibleModule(object):
"""Wrapper for AnsibleModule.exit_json""" """Wrapper for AnsibleModule.exit_json"""
self.module.exit_json(**self.result) self.module.exit_json(**self.result)
def move_file(self, path: pathlib.Path, dest: pathlib.Path, backup: bool = False, unsafe_writes: bool = False):
"""Moves an Temporary file to an destination it uses the args from add_file_common_args when used
Args:
path: The Path that the file currently is
dest: the location the file should be
backup: should an backup be made before the file is moved
"""
if backup:
shutil.copy2(dest, dest.with_suffix(dest.suffix + ".bak"), follow_symlinks=False)
self.module.atomic_move(path, dest, unsafe_writes=unsafe_writes, keep_dest_attrs=True)
if "add_file_common_args" in self.modspec and self.modspec["add_file_common_args"]:
file_args = self.module.load_file_common_arguments(self.params, path=dest)
diff = TypedDiff()
self.changed |= self.module.set_fs_attributes_if_different(file_args, diff=diff)
self.diff(diff)
class SystemdUnitModule(AnsibleModule): class SystemdUnitModule(AnsibleModule):
#: path of the unitfile managed by this module #: path of the unitfile managed by this module
@ -286,34 +305,6 @@ class SystemdUnitModule(AnsibleModule):
path = self.tmpdir / "newunit" path = self.tmpdir / "newunit"
with open(path, "w") as unit: with open(path, "w") as unit:
unit.write(self.unit()) unit.write(self.unit())
self.module.set_owner_if_different(path.as_posix(), "root", False)
self.module.set_group_if_different(path.as_posix(), "root", False)
self.module.set_mode_if_different(path.as_posix(), "0644", False)
if self.unitfile.exists():
diff = TypedDiff() # type:ignore[reportCallIssue]
self.changed = self.changed | self.module.set_owner_if_different(
self.unitfile.as_posix(),
"root",
self.result["changed"],
diff,
)
self.diff(diff)
diff = TypedDiff() # type:ignore[reportCallIssue]
self.changed = self.changed | self.module.set_group_if_different(
self.unitfile.as_posix(),
"root",
self.result["changed"],
diff,
)
self.diff(diff)
diff = TypedDiff() # type:ignore[reportCallIssue]
self.changed = self.changed | self.module.set_mode_if_different(
self.unitfile.as_posix(),
"0644",
self.result["changed"],
diff,
)
self.diff(diff)
def check(self): # pragma: nocover def check(self): # pragma: nocover
self.set("unitfile", self.unitfile.as_posix()) self.set("unitfile", self.unitfile.as_posix())
@ -334,10 +325,7 @@ class SystemdUnitModule(AnsibleModule):
self.check() self.check()
if not self.changed: if not self.changed:
return return
self.module.atomic_move( self.move_file(self.tmpdir / "newunit", self.unitfile)
src=(self.tmpdir / "newunit").as_posix(),
dest=self.unitfile.as_posix(),
)
if hasattr(self, "post") and self.post is not None: if hasattr(self, "post") and self.post is not None:
self.post() self.post()