diff --git a/plugins/cliconf/eos.py b/plugins/cliconf/eos.py index e86d30f7c..20badd1ba 100644 --- a/plugins/cliconf/eos.py +++ b/plugins/cliconf/eos.py @@ -210,7 +210,7 @@ def edit_config( resp = {} session = None - diff_onbox = self.get_option("diff_onbox") + # diff_onbox = self.get_option("diff_onbox") if self.supports_sessions(): session = session_name() diff --git a/plugins/module_utils/network/eos/utils/utils.py b/plugins/module_utils/network/eos/utils/utils.py index 260aa1996..f19a87736 100644 --- a/plugins/module_utils/network/eos/utils/utils.py +++ b/plugins/module_utils/network/eos/utils/utils.py @@ -10,6 +10,7 @@ __metaclass__ = type +import difflib def get_interface_number(name): digits = "" @@ -82,3 +83,7 @@ def numerical_sort(string_int_list): as_int_list.append(int(vlan)) as_int_list.sort() return list(set(as_int_list)) + +def unified_diff(content1, content2): + unified_diff = difflib.unified_diff(content1, content2, n=70, lineterm='\n') + return "\n".join(unified_diff) diff --git a/plugins/modules/eos_config.py b/plugins/modules/eos_config.py index 5c809386b..04316e6da 100644 --- a/plugins/modules/eos_config.py +++ b/plugins/modules/eos_config.py @@ -140,10 +140,16 @@ false, the command is issued without the all keyword type: bool default: false - diff_onbox: - description: Specify the status for on-box-diff - type: bool - default: true + off_box_diff: + description: Specify the off-box diff parameters + type: dict + suboptions: + enable: + description: Enable off box diff + type: bool + context_lines: + description: Specify The number of context lines, by default it includes all lines. + type: int save_when: description: - When changes are made to the device running-configuration, the changes are not @@ -322,6 +328,7 @@ NetworkConfig, dumps, ) +from ansible_collections.arista.eos.plugins.module_utils.network.eos.utils.utils import unified_diff from ansible_collections.arista.eos.plugins.module_utils.network.eos.eos import ( get_config, @@ -379,7 +386,9 @@ def main(): parents=dict(type="list", elements="str"), before=dict(type="list", elements="str"), after=dict(type="list", elements="str"), - diff_onbox=dict(type="bool", default=True), + off_box_diff=dict(type="dict", options=dict( + enable=dict(type=bool), context_lines=dict(type=int) + )), match=dict( default="line", choices=["line", "strict", "exact", "none"], @@ -463,7 +472,6 @@ def main(): candidate=candidate, running=running, diff_match=match, - diff_onbox=module.params["diff_onbox"], diff_ignore_lines=diff_ignore_lines, path=path, diff_replace=replace, @@ -471,7 +479,6 @@ def main(): except ConnectionError as exc: module.fail_json(msg=to_text(exc, errors="surrogate_then_replace")) - config_diff = response["config_diff"] if config_diff: commands = config_diff.split("\n") @@ -493,12 +500,14 @@ def main(): replace=replace, commit=commit, ) - result["changed"] = True if module.params["diff_against"] == "session": if "diff" in response: - result["diff"] = {"prepared": response["diff"]} + if module.params.get("off_box_diff"): + result["off_box_diff"] = unified_diff(candidate.split("\n"), running.split("\n")) + else: + result["diff"] = {"prepared": response["diff"]} else: result["changed"] = False @@ -624,7 +633,6 @@ def main(): result["warnings"].append(msg) else: result["warnings"] = msg - module.exit_json(**result)