From e2ae191812624fa26550a20a49b61c186a2b3c60 Mon Sep 17 00:00:00 2001 From: Om Nom Date: Wed, 13 Nov 2024 19:09:10 +1000 Subject: [PATCH 1/4] VyOS v1.4+ chronyd conf support --- .../vyos/config/ntp_global/ntp_global.py | 12 ++++++++++ .../network/vyos/rm_templates/ntp_global.py | 24 +++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py b/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py index 106faac5..a00fe48a 100644 --- a/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py +++ b/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py @@ -31,6 +31,10 @@ NtpTemplate, ) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import get_os_version + +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.utils.version import LooseVersion + class Ntp_global(ResourceModule): """ @@ -120,6 +124,14 @@ def generate_commands(self): for k, want in iteritems(wantd): self._compare(want=want, have=haved.pop(k, {})) + if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): + path = "service" + else: + path = "system" + + if self.commands: + self.commands = [cl.replace('path', path) for cl in self.commands] + def _compare(self, want, have): """Leverages the base class `compare()` method and populates the list of commands to be run by comparing diff --git a/plugins/module_utils/network/vyos/rm_templates/ntp_global.py b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py index 33d173e0..9224d3ef 100644 --- a/plugins/module_utils/network/vyos/rm_templates/ntp_global.py +++ b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py @@ -35,11 +35,11 @@ def __init__(self, lines=None, module=None): "name": "allow_clients", "getval": re.compile( r""" - ^set\ssystem\sntp\sallow-clients\saddress (\s(?P\S+))? + ^set\s(?Psystem|service)?\sntp\sallow-clients\saddress (\s(?P\S+))? $""", re.VERBOSE, ), - "setval": "system ntp allow-clients address {{allow_clients}}", + "setval": "path ntp allow-clients address {{allow_clients}}", "result": { "allow_clients": ["{{ipaddress}}"], }, @@ -50,11 +50,11 @@ def __init__(self, lines=None, module=None): "name": "allow_clients_delete", "getval": re.compile( r""" - ^set\ssystem\sntp\sallow-clients + ^set\sservice\sntp\sallow-clients $""", re.VERBOSE, ), - "setval": "system ntp allow-clients", + "setval": "path ntp allow-clients", "result": { }, @@ -66,11 +66,11 @@ def __init__(self, lines=None, module=None): "name": "listen_addresses", "getval": re.compile( r""" - ^set\ssystem\sntp\slisten-address (\s(?P\S+))? + ^set\s(?Psystem|service)?\sntp\slisten-address (\s(?P\S+))? $""", re.VERBOSE, ), - "setval": "system ntp listen-address {{listen_addresses}}", + "setval": "path ntp listen-address {{listen_addresses}}", "result": { "listen_addresses": ["{{ip_address}}"], }, @@ -81,11 +81,11 @@ def __init__(self, lines=None, module=None): "name": "listen_addresses_delete", "getval": re.compile( r""" - ^set\ssystem\sntp\slisten-address + ^set\s(?Psystem|service)?\sntp\slisten-address $""", re.VERBOSE, ), - "setval": "system ntp listen-address", + "setval": "path ntp listen-address", "result": { }, }, @@ -95,11 +95,11 @@ def __init__(self, lines=None, module=None): "name": "server", "getval": re.compile( r""" - ^set\ssystem\sntp\sserver (\s(?P\S+))? + ^set\s(?Psystem|service)?\sntp\sserver (\s(?P\S+))? $""", re.VERBOSE, ), - "setval": "system ntp server {{server}}", + "setval": "path ntp server {{server}}", "result": { "servers": { "{{name}}": { @@ -115,13 +115,13 @@ def __init__(self, lines=None, module=None): "name": "options", "getval": re.compile( r""" - ^set\ssystem\sntp\sserver + ^set\s(?Psystem|service)?\sntp\sserver \s(?P\S+) \s(?Pnoselect|dynamic|pool|preempt|prefer)? $""", re.VERBOSE, ), - "setval": "system ntp server {{server}} {{options}}", + "setval": "path ntp server {{server}} {{options}}", "result": { "servers": { "{{name}}": { From 00f8b8030717f507780fb29356885bbc724286ee Mon Sep 17 00:00:00 2001 From: Om Nom Date: Thu, 14 Nov 2024 20:10:58 +1000 Subject: [PATCH 2/4] template typo fix --- plugins/module_utils/network/vyos/rm_templates/ntp_global.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/network/vyos/rm_templates/ntp_global.py b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py index 9224d3ef..2eb62fd2 100644 --- a/plugins/module_utils/network/vyos/rm_templates/ntp_global.py +++ b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py @@ -50,7 +50,7 @@ def __init__(self, lines=None, module=None): "name": "allow_clients_delete", "getval": re.compile( r""" - ^set\sservice\sntp\sallow-clients + ^set\s(?Psystem|service)?\sntp\sallow-clients $""", re.VERBOSE, ), From 09b90ade752e306ed25734e64f9939a15c949382 Mon Sep 17 00:00:00 2001 From: Om Nom Date: Thu, 14 Nov 2024 20:19:24 +1000 Subject: [PATCH 3/4] Making replace tool more robust by distinctive placeholder --- .../network/vyos/config/ntp_global/ntp_global.py | 2 +- .../network/vyos/rm_templates/ntp_global.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py b/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py index a00fe48a..25092bb6 100644 --- a/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py +++ b/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py @@ -130,7 +130,7 @@ def generate_commands(self): path = "system" if self.commands: - self.commands = [cl.replace('path', path) for cl in self.commands] + self.commands = [cl.replace('%%path%%', path) for cl in self.commands] def _compare(self, want, have): """Leverages the base class `compare()` method and diff --git a/plugins/module_utils/network/vyos/rm_templates/ntp_global.py b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py index 2eb62fd2..855271b2 100644 --- a/plugins/module_utils/network/vyos/rm_templates/ntp_global.py +++ b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py @@ -39,7 +39,7 @@ def __init__(self, lines=None, module=None): $""", re.VERBOSE, ), - "setval": "path ntp allow-clients address {{allow_clients}}", + "setval": "%%path%% ntp allow-clients address {{allow_clients}}", "result": { "allow_clients": ["{{ipaddress}}"], }, @@ -54,7 +54,7 @@ def __init__(self, lines=None, module=None): $""", re.VERBOSE, ), - "setval": "path ntp allow-clients", + "setval": "%%path%% ntp allow-clients", "result": { }, @@ -70,7 +70,7 @@ def __init__(self, lines=None, module=None): $""", re.VERBOSE, ), - "setval": "path ntp listen-address {{listen_addresses}}", + "setval": "%%path%% ntp listen-address {{listen_addresses}}", "result": { "listen_addresses": ["{{ip_address}}"], }, @@ -85,7 +85,7 @@ def __init__(self, lines=None, module=None): $""", re.VERBOSE, ), - "setval": "path ntp listen-address", + "setval": "%%path%% ntp listen-address", "result": { }, }, @@ -99,7 +99,7 @@ def __init__(self, lines=None, module=None): $""", re.VERBOSE, ), - "setval": "path ntp server {{server}}", + "setval": "%%path%% ntp server {{server}}", "result": { "servers": { "{{name}}": { @@ -121,7 +121,7 @@ def __init__(self, lines=None, module=None): $""", re.VERBOSE, ), - "setval": "path ntp server {{server}} {{options}}", + "setval": "%%path%% ntp server {{server}} {{options}}", "result": { "servers": { "{{name}}": { From 6e9f79b1fea3cdee19febd72710a9b971cdd5c05 Mon Sep 17 00:00:00 2001 From: Om Nom Date: Thu, 14 Nov 2024 21:05:19 +1000 Subject: [PATCH 4/4] allow-clients workaround --- .../network/vyos/config/ntp_global/ntp_global.py | 3 +++ .../module_utils/network/vyos/rm_templates/ntp_global.py | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py b/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py index 25092bb6..0af1af58 100644 --- a/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py +++ b/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py @@ -126,11 +126,14 @@ def generate_commands(self): if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): path = "service" + ac = "allow-client" else: path = "system" + ac = "allow-clients" if self.commands: self.commands = [cl.replace('%%path%%', path) for cl in self.commands] + self.commands = [nc.replace('%%ac%%', ac) for nc in self.commands] def _compare(self, want, have): """Leverages the base class `compare()` method and diff --git a/plugins/module_utils/network/vyos/rm_templates/ntp_global.py b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py index 855271b2..1e011dd9 100644 --- a/plugins/module_utils/network/vyos/rm_templates/ntp_global.py +++ b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py @@ -35,11 +35,11 @@ def __init__(self, lines=None, module=None): "name": "allow_clients", "getval": re.compile( r""" - ^set\s(?Psystem|service)?\sntp\sallow-clients\saddress (\s(?P\S+))? + ^set\s(?Psystem|service)?\sntp\s(?Pallow-clients|allow-client)?\saddress (\s(?P\S+))? $""", re.VERBOSE, ), - "setval": "%%path%% ntp allow-clients address {{allow_clients}}", + "setval": "%%path%% ntp %%ac%% address {{allow_clients}}", "result": { "allow_clients": ["{{ipaddress}}"], }, @@ -50,11 +50,11 @@ def __init__(self, lines=None, module=None): "name": "allow_clients_delete", "getval": re.compile( r""" - ^set\s(?Psystem|service)?\sntp\sallow-clients + ^set\s(?Psystem|service)?\sntp\s(?Pallow-clients|allow-client)? $""", re.VERBOSE, ), - "setval": "%%path%% ntp allow-clients", + "setval": "%%path%% ntp %%ac%%", "result": { },