diff --git a/plugins/cliconf/vyos.py b/plugins/cliconf/vyos.py index c35ff1ec..28fa3886 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,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") @@ -268,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"): @@ -283,6 +285,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 @@ -346,3 +361,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 eeb6bc44..0e2b4399 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 = """ @@ -299,6 +306,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")) @@ -337,6 +345,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")]