mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-09 18:04:02 -07:00
Fix iosxr cli_config module diff issue (#44898)
* Fix iosxr cli_config module diff issue * Modify iosxr plugin to support configuration diff capability (get_diff()) within Ansible to be in sync with iosxr_config module. * Fix unit test case failure
This commit is contained in:
parent
c3e526bf2c
commit
1a684df109
4 changed files with 220 additions and 144 deletions
|
@ -26,7 +26,9 @@ import json
|
|||
from ansible.errors import AnsibleConnectionFailure
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.module_utils.connection import ConnectionError
|
||||
from ansible.module_utils.network.common.config import NetworkConfig, dumps
|
||||
from ansible.module_utils.network.common.utils import to_list
|
||||
from ansible.module_utils.network.iosxr.iosxr import sanitize_config, mask_config_blocks_from_diff
|
||||
from ansible.plugins.cliconf import CliconfBase
|
||||
|
||||
|
||||
|
@ -114,15 +116,37 @@ class Cliconf(CliconfBase):
|
|||
resp['response'] = results
|
||||
return resp
|
||||
|
||||
def get_diff(self, admin=False):
|
||||
self.configure(admin=admin)
|
||||
def get_diff(self, candidate=None, running=None, diff_match='line', diff_ignore_lines=None, path=None, diff_replace='line'):
|
||||
diff = {}
|
||||
device_operations = self.get_device_operations()
|
||||
option_values = self.get_option_values()
|
||||
|
||||
diff = {'config_diff': None}
|
||||
response = self.send_command('show commit changes diff')
|
||||
for item in response.splitlines():
|
||||
if item and item[0] in ['<', '+', '-']:
|
||||
diff['config_diff'] = response
|
||||
break
|
||||
if candidate is None and device_operations['supports_generate_diff']:
|
||||
raise ValueError("candidate configuration is required to generate diff")
|
||||
|
||||
if diff_match not in option_values['diff_match']:
|
||||
raise ValueError("'match' value %s in invalid, valid values are %s" % (diff_match, ', '.join(option_values['diff_match'])))
|
||||
|
||||
if diff_replace not in option_values['diff_replace']:
|
||||
raise ValueError("'replace' value %s in invalid, valid values are %s" % (diff_replace, ', '.join(option_values['diff_replace'])))
|
||||
|
||||
# prepare candidate configuration
|
||||
sanitized_candidate = sanitize_config(candidate)
|
||||
candidate_obj = NetworkConfig(indent=1, ignore_lines=diff_ignore_lines)
|
||||
candidate_obj.load(sanitized_candidate)
|
||||
|
||||
if running and diff_match != 'none':
|
||||
# running configuration
|
||||
running = mask_config_blocks_from_diff(running, candidate, "ansible")
|
||||
running = sanitize_config(running)
|
||||
|
||||
running_obj = NetworkConfig(indent=1, contents=running, ignore_lines=diff_ignore_lines)
|
||||
configdiffobjs = candidate_obj.difference(running_obj, path=path, match=diff_match, replace=diff_replace)
|
||||
|
||||
else:
|
||||
configdiffobjs = candidate_obj.items
|
||||
|
||||
diff['config_diff'] = dumps(configdiffobjs, 'commands') if configdiffobjs else ''
|
||||
return diff
|
||||
|
||||
def get(self, command=None, prompt=None, answer=None, sendonly=False, newline=True, output=None):
|
||||
|
@ -186,16 +210,16 @@ class Cliconf(CliconfBase):
|
|||
|
||||
def get_device_operations(self):
|
||||
return {
|
||||
'supports_diff_replace': False,
|
||||
'supports_diff_replace': True,
|
||||
'supports_commit': True,
|
||||
'supports_rollback': False,
|
||||
'supports_defaults': False,
|
||||
'supports_onbox_diff': True,
|
||||
'supports_onbox_diff': False,
|
||||
'supports_commit_comment': True,
|
||||
'supports_multiline_delimiter': False,
|
||||
'supports_diff_match': False,
|
||||
'supports_diff_ignore_lines': False,
|
||||
'supports_generate_diff': False,
|
||||
'supports_diff_match': True,
|
||||
'supports_diff_ignore_lines': True,
|
||||
'supports_generate_diff': True,
|
||||
'supports_replace': True,
|
||||
'supports_admin': True,
|
||||
'supports_commit_label': True
|
||||
|
@ -204,8 +228,8 @@ class Cliconf(CliconfBase):
|
|||
def get_option_values(self):
|
||||
return {
|
||||
'format': ['text'],
|
||||
'diff_match': [],
|
||||
'diff_replace': [],
|
||||
'diff_match': ['line', 'strict', 'exact', 'none'],
|
||||
'diff_replace': ['line', 'block', 'config'],
|
||||
'output': []
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue