From 76f4e640814e40af44465fba000f3990963d53ec Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Fri, 21 Apr 2023 20:49:07 +0200 Subject: [PATCH] generalized the systemdbool function --- plugins/module_utils/generic.py | 9 +++++++++ plugins/modules/network.py | 16 ++++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/plugins/module_utils/generic.py b/plugins/module_utils/generic.py index 737a11d..8242d97 100644 --- a/plugins/module_utils/generic.py +++ b/plugins/module_utils/generic.py @@ -6,6 +6,7 @@ __all__ = ( "SYSTEMD_SERVICE_CONFIG", "SYSTEMD_NETWORK_CONFIG", "SYSTEMD_CONFIG_ROOT", + "systemdbool", ) @@ -126,3 +127,11 @@ class Types(metaclass=_Type): argument.__name__ = name argument.__doc__ = argument.__doc__.format(name) return argument + + +def systemdbool(b: Union[bool, str]) -> str: + if b is True: + return "yes" + elif b is False: + return "no" + return b diff --git a/plugins/modules/network.py b/plugins/modules/network.py index 0be83bb..d8f37a4 100644 --- a/plugins/modules/network.py +++ b/plugins/modules/network.py @@ -4,20 +4,12 @@ from typing import List, Union try: from ansible_collections.sebastian.systemd.plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types - from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule + from ansible_collections.sebastian.systemd.plugins.module_utils.module import SystemdUnitModule, systemdbool except ImportError: - from plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types + from plugins.module_utils.generic import SYSTEMD_NETWORK_CONFIG, Types, systemdbool from plugins.module_utils.module import SystemdUnitModule -def boolconvert(b: Union[bool, str]) -> str: - if b is True: - return "yes" - elif b is False: - return "no" - return b - - class Module(SystemdUnitModule): """Sets up the systemd network unit""" @@ -87,8 +79,8 @@ class Module(SystemdUnitModule): domain = self.get("domain") self.set("domainlog", str(domain)) options.append("Domains={}".format(" ".join(domain))) - options.append("DNSOverTLS={}".format(boolconvert(self.get("dot", "opportunistic")))) - options.append("DNSSEC={}".format(boolconvert(self.get("dnssec", "allow-downgrade")))) + options.append("DNSOverTLS={}".format(systemdbool(self.get("dot", "opportunistic")))) + options.append("DNSSEC={}".format(systemdbool(self.get("dnssec", "allow-downgrade")))) except KeyError: pass output += "\n".join(options)