Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions plugins/cliconf/vyos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")

Expand Down Expand Up @@ -265,8 +262,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:
Expand All @@ -278,6 +280,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

Expand Down Expand Up @@ -341,3 +356,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
9 changes: 9 additions & 0 deletions plugins/modules/vyos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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")]
Expand Down
Loading