From c1c54f1bf4dd84a2ddcd83b01c87357e53621ffe Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Mon, 27 Feb 2017 09:24:05 -0500 Subject: [PATCH] roll up of eos fixes (#21987) * fixes to_command function in eos to not iterate over commands twice * fixes lots of minor issues with _eos_template --- lib/ansible/module_utils/eos.py | 10 +----- .../modules/network/eos/_eos_template.py | 32 ++++++++++++------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/ansible/module_utils/eos.py b/lib/ansible/module_utils/eos.py index 14019edf160..7bf37deca3d 100644 --- a/lib/ansible/module_utils/eos.py +++ b/lib/ansible/module_utils/eos.py @@ -432,15 +432,7 @@ def to_command(module, commands): answer=dict() ), module) - commands = transform(to_list(commands)) - - for index, item in enumerate(commands): - if is_json(item['command']): - item['output'] = 'json' - elif is_text(item['command']): - item['output'] = 'text' - - return commands + return transform(to_list(commands)) def get_config(module, flags=[]): conn = get_connection(module) diff --git a/lib/ansible/modules/network/eos/_eos_template.py b/lib/ansible/modules/network/eos/_eos_template.py index 45fd218ad60..f18f371846a 100644 --- a/lib/ansible/modules/network/eos/_eos_template.py +++ b/lib/ansible/modules/network/eos/_eos_template.py @@ -123,17 +123,28 @@ responses: """ import re +from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.eos import load_config, get_config -from ansible.module_utils.basic import AnsibleModle +from ansible.module_utils.eos import eos_argument_spec +from ansible.module_utils.eos import check_args as eos_check_args from ansible.module_utils.netcfg import NetworkConfig, dumps +def check_args(module, warnings): + eos_check_args(module, warnings) + + transport = module.params['transport'] + provider_transport = (module.params['provider'] or {}).get('transport') + + if module.params['replace'] and 'eapi' in (transport, provider_transport): + module.fail_json(msg='config replace is only supported over cli') + def get_current_config(module): config = module.params.get('config') if not config and not module.params['force']: flags = [] if module.params['include_defaults']: flags.append('all') - config = module.config.get_config(include_defaults=defaults) + config = get_config(module, flags) return config def filter_exit(commands): @@ -173,7 +184,7 @@ def main(): config=dict() ) - argument_spec.update(eos_local.eapi_argument_spec) + argument_spec.update(eos_argument_spec) mutually_exclusive = [('config', 'backup'), ('config', 'force')] @@ -181,7 +192,8 @@ def main(): mutually_exclusive=mutually_exclusive, supports_check_mode=True) - warnings = check_args(module) + warnings = list() + check_args(module, warnings) result = {'changed': False} if warnings: @@ -191,7 +203,7 @@ def main(): candidate = NetworkConfig(contents=src, indent=3) if module.params['backup']: - result['__backup__'] = get_config() + result['__backup__'] = get_config(module) if not module.params['force']: contents = get_current_config(module) @@ -202,18 +214,16 @@ def main(): else: commands = [c.strip() for c in str(candidate).split('\n')] - # FIXME not implemented yet!! - if replace: - if module.params['transport'] == 'cli': - module.fail_json(msg='config replace is only supported over eos_local') - commands = str(candidate).split('\n') + #commands = str(candidate).split('\n') if commands: commands = filter_exit(commands) commit = not module.check_mode - load_config(commands, commit=commit) + replace = module.params['replace'] or False + load_config(module, commands, commit=commit, replace=replace) result['changed'] = True + result['commands'] = commands result['updates'] = commands module.exit_json(**result)