nxos cliconf plugin refactor (#43203)

* nxos cliconf plugin refactor

Fixes #39056

*  Refactor nxos cliconf plugin as per new api definition
*  Minor changes in ios, eos, vyos cliconf plugin
*  Change nxos httpapi plugin edit_config method to be in sync with
   nxos cliconf edit_config

* Fix CI failure

* Fix unit test failure and review comment
This commit is contained in:
Ganesh Nalawade 2018-07-27 11:05:40 +05:30 committed by GitHub
parent e215f842ba
commit af3f510316
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 426 additions and 245 deletions

View file

@ -382,7 +382,8 @@ def main():
candidate = get_candidate(module)
running = get_running_config(module, contents, flags=flags)
response = connection.get_diff(candidate=candidate, running=running, match=match, diff_ignore_lines=diff_ignore_lines, path=path, replace=replace)
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
diff_replace=replace)
config_diff = response['config_diff']
if config_diff:

View file

@ -420,7 +420,8 @@ def main():
candidate = get_candidate_config(module)
running = get_running_config(module, contents, flags=flags)
response = connection.get_diff(candidate=candidate, running=running, match=match, diff_ignore_lines=diff_ignore_lines, path=path, replace=replace)
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
diff_replace=replace)
config_diff = response['config_diff']
banner_diff = response['banner_diff']

View file

@ -100,7 +100,7 @@ options:
the modified lines are pushed to the device in configuration
mode. If the replace argument is set to I(block) then the entire
command block is pushed to the device in configuration mode if any
line is not correct. I(replace config) is supported only on Nexus 9K device.
line is not correct. replace I(config) is supported only on Nexus 9K device.
default: line
choices: ['line', 'block', 'config']
force:
@ -281,7 +281,7 @@ backup_path:
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.connection import ConnectionError
from ansible.module_utils.network.common.config import NetworkConfig, dumps
from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands
from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands, get_connection
from ansible.module_utils.network.nxos.nxos import get_capabilities
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec
from ansible.module_utils.network.nxos.nxos import check_args as nxos_check_args
@ -296,19 +296,21 @@ def get_running_config(module, config=None):
else:
flags = ['all']
contents = get_config(module, flags=flags)
return NetworkConfig(indent=2, contents=contents)
return contents
def get_candidate(module):
candidate = NetworkConfig(indent=2)
candidate = ''
if module.params['src']:
if module.params['replace'] != 'config':
candidate.load(module.params['src'])
candidate = module.params['src']
if module.params['replace'] == 'config':
candidate.load('config replace {0}'.format(module.params['replace_src']))
candidate = 'config replace {0}'.format(module.params['replace_src'])
elif module.params['lines']:
candidate_obj = NetworkConfig(indent=2)
parents = module.params['parents'] or list()
candidate.add(module.params['lines'], parents=parents)
candidate_obj.add(module.params['lines'], parents=parents)
candidate = dumps(candidate_obj, 'raw')
return candidate
@ -404,7 +406,12 @@ def main():
if '9K' not in os_platform:
module.fail_json(msg='replace: config is supported only on Nexus 9K series switches')
if module.params['replace_src']:
diff_ignore_lines = module.params['diff_ignore_lines']
path = module.params['parents']
connection = get_connection(module)
contents = None
replace_src = module.params['replace_src']
if replace_src:
if module.params['replace'] != 'config':
module.fail_json(msg='replace: config is required with replace_src')
@ -414,48 +421,51 @@ def main():
if module.params['backup']:
result['__backup__'] = contents
if any((module.params['src'], module.params['lines'], module.params['replace_src'])):
if any((module.params['src'], module.params['lines'], replace_src)):
match = module.params['match']
replace = module.params['replace']
commit = not module.check_mode
candidate = get_candidate(module)
if match != 'none' and replace != 'config':
config = get_running_config(module, config)
path = module.params['parents']
configobjs = candidate.difference(config, match=match, replace=replace, path=path)
else:
configobjs = candidate.items
if configobjs:
commands = dumps(configobjs, 'commands').split('\n')
if module.params['before']:
commands[:0] = module.params['before']
if module.params['after']:
commands.extend(module.params['after'])
result['commands'] = commands
result['updates'] = commands
if not module.check_mode:
load_config(module, commands)
running = get_running_config(module, contents)
if replace_src:
commands = candidate.split('\n')
result['commands'] = result['updates'] = commands
if commit:
load_config(module, commands, replace=replace_src)
result['changed'] = True
else:
response = connection.get_diff(candidate=candidate, running=running, diff_match=match, diff_ignore_lines=diff_ignore_lines, path=path,
diff_replace=replace)
config_diff = response['config_diff']
if config_diff:
commands = config_diff.split('\n')
if module.params['before']:
commands[:0] = module.params['before']
if module.params['after']:
commands.extend(module.params['after'])
result['commands'] = commands
result['updates'] = commands
if commit:
load_config(module, commands, replace=replace_src)
result['changed'] = True
running_config = module.params['running_config']
startup_config = None
diff_ignore_lines = module.params['diff_ignore_lines']
if module.params['save_when'] == 'always' or module.params['save']:
save_config(module, result)
elif module.params['save_when'] == 'modified':
output = execute_show_commands(module, ['show running-config', 'show startup-config'])
running_config = NetworkConfig(indent=1, contents=output[0], ignore_lines=diff_ignore_lines)
startup_config = NetworkConfig(indent=1, contents=output[1], ignore_lines=diff_ignore_lines)
running_config = NetworkConfig(indent=2, contents=output[0], ignore_lines=diff_ignore_lines)
startup_config = NetworkConfig(indent=2, contents=output[1], ignore_lines=diff_ignore_lines)
if running_config.sha1 != startup_config.sha1:
save_config(module, result)
@ -470,7 +480,7 @@ def main():
contents = running_config
# recreate the object in order to process diff_ignore_lines
running_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines)
running_config = NetworkConfig(indent=2, contents=contents, ignore_lines=diff_ignore_lines)
if module.params['diff_against'] == 'running':
if module.check_mode:
@ -484,14 +494,13 @@ def main():
output = execute_show_commands(module, 'show startup-config')
contents = output[0]
else:
contents = output[0]
contents = startup_config.config_text
elif module.params['diff_against'] == 'intended':
contents = module.params['intended_config']
if contents is not None:
base_config = NetworkConfig(indent=1, contents=contents, ignore_lines=diff_ignore_lines)
base_config = NetworkConfig(indent=2, contents=contents, ignore_lines=diff_ignore_lines)
if running_config.sha1 != base_config.sha1:
if module.params['diff_against'] == 'intended':

View file

@ -208,7 +208,7 @@ def run(module, result):
# create loadable config that includes only the configuration updates
connection = get_connection(module)
response = connection.get_diff(candidate=candidate, running=config, match=module.params['match'])
response = connection.get_diff(candidate=candidate, running=config, diff_match=module.params['match'])
commands = response.get('config_diff')
sanitize_config(commands, result)