From a1dba699c905d5a38d0edc6a088e970a855e5cce Mon Sep 17 00:00:00 2001 From: mrvantage <8395675-mrvantage@users.noreply.gitlab.com> Date: Sun, 17 Nov 2024 10:34:22 +0100 Subject: [PATCH 1/2] T6837 / #288 Implemented replace functionality using a boolean arg for the module. Also Improved detection of config lines already present on router, and filter them out in calculated diff --- plugins/cliconf/vyos.py | 36 +++++++++++++++++++++++++++++----- plugins/modules/vyos_config.py | 9 +++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/plugins/cliconf/vyos.py b/plugins/cliconf/vyos.py index 5beffaa1a..ccc53cb13 100644 --- a/plugins/cliconf/vyos.py +++ b/plugins/cliconf/vyos.py @@ -208,7 +208,7 @@ def get_diff( diff_match="line", diff_ignore_lines=None, path=None, - diff_replace=None, + diff_replace=False, ): diff = {} device_operations = self.get_device_operations() @@ -223,8 +223,8 @@ def get_diff( % (diff_match, ", ".join(option_values["diff_match"])), ) - if diff_replace: - raise ValueError("'replace' in diff is not supported") + # if diff_replace: + # raise ValueError("'replace' in diff is not supported") if diff_ignore_lines: raise ValueError("'diff_ignore_lines' in diff is not supported") @@ -265,8 +265,13 @@ def get_diff( if not item.startswith("set") and not item.startswith("delete"): raise ValueError("line must start with either `set` or `delete`") - elif item.startswith("set") and item not in running_commands: - updates.append(line) + elif item.startswith("set"): + match = False + for rline in running_commands: + if match_cmd(item, rline): + match = True + if not match: + updates.append(line) elif item.startswith("delete"): if not running_commands: @@ -278,6 +283,19 @@ def get_diff( updates.append(line) visited.add(line) + if diff_replace: + for line in running.splitlines(): + line = line.replace("'", "\"") + + match = False + for cline in candidate_commands: + if match_cmd(line, cline): + match = True + + if not match: + line = re.sub(r"set", "delete", line) + updates.append(line) + diff["config_diff"] = list(updates) return diff @@ -341,3 +359,11 @@ def set_cli_prompt_context(self): """ if self._connection.connected: self._update_cli_prompt_context(config_context="#", exit_command="exit discard") + +def match_cmd(cmd1, cmd2): + cmd1 = re.sub("['\"]", "", cmd1) + cmd2 = re.sub("['\"]", "", cmd2) + if cmd1 == cmd2: + return True + else: + return False diff --git a/plugins/modules/vyos_config.py b/plugins/modules/vyos_config.py index bf5d4217c..434c50e84 100644 --- a/plugins/modules/vyos_config.py +++ b/plugins/modules/vyos_config.py @@ -123,6 +123,13 @@ in C(filename) within I(backup) directory. type: path type: dict + replace: + description: + - The C(replace) argument will replace the entire config, instead of merging it + with the base config that is already present. This only works in C(match) is in + C(line) mode. For backwards compatibility default is C(false). + type: bool + default: no """ EXAMPLES = """ @@ -298,6 +305,7 @@ def run(module, result): candidate=candidate, running=config, diff_match=module.params["match"], + diff_replace=module.params["replace"] ) except ConnectionError as exc: module.fail_json(msg=to_text(exc, errors="surrogate_then_replace")) @@ -336,6 +344,7 @@ def main(): backup=dict(type="bool", default=False), backup_options=dict(type="dict", options=backup_spec), save=dict(type="bool", default=False), + replace=dict(type="bool", default=False), ) mutually_exclusive = [("lines", "src")] From e3d4fe706f75e265e72b13083ed5733e8eb88d71 Mon Sep 17 00:00:00 2001 From: mrvantage <8395675-mrvantage@users.noreply.gitlab.com> Date: Sun, 17 Nov 2024 12:48:37 +0100 Subject: [PATCH 2/2] T6837 / #288 cleanup commented obsolete code --- plugins/cliconf/vyos.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/plugins/cliconf/vyos.py b/plugins/cliconf/vyos.py index ccc53cb13..22f87fb5c 100644 --- a/plugins/cliconf/vyos.py +++ b/plugins/cliconf/vyos.py @@ -223,9 +223,6 @@ def get_diff( % (diff_match, ", ".join(option_values["diff_match"])), ) - # if diff_replace: - # raise ValueError("'replace' in diff is not supported") - if diff_ignore_lines: raise ValueError("'diff_ignore_lines' in diff is not supported")