From 0a22dbd9d83c133f01aeddb998a83bbaf7841a84 Mon Sep 17 00:00:00 2001 From: wiso Date: Tue, 9 May 2017 22:04:59 +0200 Subject: [PATCH] Update netconf_config.py (#24323) * Update netconf_config.py Implementation proposal for feature request #24220 * Update netconf_config.py fixed pep8 added code proposal for #24221 * Update netconf_config.py correct more pep8 related things add some cosmetic changes --- .../modules/network/netconf/netconf_config.py | 72 ++++++++++++++----- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/lib/ansible/modules/network/netconf/netconf_config.py b/lib/ansible/modules/network/netconf/netconf_config.py index c56453cab46..ac10600acce 100644 --- a/lib/ansible/modules/network/netconf/netconf_config.py +++ b/lib/ansible/modules/network/netconf/netconf_config.py @@ -75,6 +75,13 @@ options: default: auto required: false version_added: "2.4" + save: + description: + - The C(save) argument instructs the module to save the running- + config to the startup-config if changed. + required: false + default: false + version_added: "2.4" username: description: - the username to authenticate with @@ -86,7 +93,16 @@ options: xml: description: - the XML content to send to the device - required: true + required: false + src: + description: + - Specifies the source path to the xml file that contains the configuration + or configuration template to load. The path to the source file can + either be the full path on the Ansible control host or a relative + path from the playbook or role root directory. This argument is mutually + exclusive with I(xml). + required: false + version_added: "2.4" requirements: @@ -185,26 +201,35 @@ def main(): hostkey_verify=dict(type='bool', default=True), allow_agent=dict(type='bool', default=True), look_for_keys=dict(type='bool', default=True), - datastore=dict(type='str', default='auto'), + datastore=dict(choices=['auto', 'candidate', 'running'], default='auto'), + save=dict(type='bool', default=False), username=dict(type='str', required=True, no_log=True), password=dict(type='str', required=True, no_log=True), - xml=dict(type='str', required=True), - ) + xml=dict(type='str', required=False), + src=dict(type='path', required=False), + ), + mutually_exclusive=[('xml', 'src')] ) if not HAS_NCCLIENT: module.fail_json(msg='could not import the python library ' 'ncclient required by this module') + if (module.params['src']): + config_xml = str(module.params['src']) + elif module.params['xml']: + config_xml = str(module.params['xml']) + else: + module.fail_json(msg='Option src or xml must be provided') + try: - xml.dom.minidom.parseString(module.params['xml']) + xml.dom.minidom.parseString(config_xml) + except: e = get_exception() module.fail_json( - msg='error parsing XML: ' + - str(e) + msg='error parsing XML: ' + str(e) ) - return nckwargs = dict( host=module.params['host'], @@ -215,7 +240,6 @@ def main(): username=module.params['username'], password=module.params['password'], ) - retkwargs = dict() try: m = ncclient.manager.connect(**nckwargs) @@ -226,11 +250,12 @@ def main(): except: e = get_exception() module.fail_json( - msg='error connecting to the device: ' + - str(e) + msg='error connecting to the device: ' + str(e) ) - return + + retkwargs = dict() retkwargs['server_capabilities'] = list(m.server_capabilities) + if module.params['datastore'] == 'candidate': if ':candidate' in m.server_capabilities: datastore = 'candidate' @@ -255,28 +280,39 @@ def main(): else: m.close_session() module.fail_json( - msg='neither :candidate nor :writable-running are supported by this netconf server') + msg='neither :candidate nor :writable-running are supported by this netconf server' + ) else: m.close_session() module.fail_json( - msg=module.params['datastore'] + - ' datastore is not supported by this ansible module') + msg=module.params['datastore'] + ' datastore is not supported by this ansible module' + ) + + if module.params['save']: + if ':startup' not in m.server_capabilities: + module.fail_json( + msg='cannot copy to , while :startup is not supported' + ) try: changed = netconf_edit_config( m=m, - xml=module.params['xml'], + xml=config_xml, commit=True, retkwargs=retkwargs, datastore=datastore, ) + if changed and module.params['save']: + m.copy_config(source="running", target="startup") except: e = get_exception() - module.fail_json(msg='error editing configuration: ' + str(e)) + module.fail_json( + msg='error editing configuration: ' + str(e) + ) finally: m.close_session() - module.exit_json(changed=changed, **retkwargs) + module.exit_json(changed=changed, **retkwargs) # import module snippets from ansible.module_utils.basic import *