mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
adds support for using connection=netconf (#33400)
* adds support for using connection=netconf This change updates the module to provide support for using connection=netconf instead of connection=local. If connection=netconf is used, then the various connection arguments will be silently ignored. * adds netconf plugin default This adds a default implementation for netconf plugins if the network_os is not specified. The default plugin will implement only the standard netconf rpcs * fix up pep8 issues
This commit is contained in:
parent
bd09e67438
commit
b4fa68555d
4 changed files with 151 additions and 60 deletions
|
@ -26,6 +26,7 @@ description:
|
|||
notes:
|
||||
- This module supports devices with and without the candidate and
|
||||
confirmed-commit capabilities. It always use the safer feature.
|
||||
- This module supports the use of connection=netconf
|
||||
version_added: "2.2"
|
||||
options:
|
||||
host:
|
||||
|
@ -101,6 +102,13 @@ requirements:
|
|||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: use lookup filter to provide xml configuration
|
||||
netconf_config:
|
||||
xml: "{{ lookup('file', './config.xml') }}"
|
||||
host: 10.0.0.1
|
||||
username: admin
|
||||
password: admin
|
||||
|
||||
- name: set ntp server in the device
|
||||
netconf_config:
|
||||
host: 10.0.0.1
|
||||
|
@ -150,6 +158,8 @@ server_capabilities:
|
|||
import traceback
|
||||
import xml.dom.minidom
|
||||
|
||||
from xml.etree.ElementTree import fromstring, tostring
|
||||
|
||||
try:
|
||||
import ncclient.manager
|
||||
HAS_NCCLIENT = True
|
||||
|
@ -158,23 +168,31 @@ except ImportError:
|
|||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.connection import Connection, ConnectionError
|
||||
|
||||
|
||||
def netconf_edit_config(m, xml, commit, retkwargs, datastore):
|
||||
def netconf_edit_config(m, xml, commit, retkwargs, datastore, capabilities, local_connection):
|
||||
m.lock(target=datastore)
|
||||
try:
|
||||
if datastore == "candidate":
|
||||
m.discard_changes()
|
||||
|
||||
config_before = m.get_config(source=datastore)
|
||||
m.edit_config(target=datastore, config=xml)
|
||||
config_after = m.get_config(source=datastore)
|
||||
changed = config_before.data_xml != config_after.data_xml
|
||||
|
||||
if local_connection:
|
||||
changed = config_before.data_xml != config_after.data_xml
|
||||
else:
|
||||
changed = config_before != config_after
|
||||
|
||||
if changed and commit and datastore == "candidate":
|
||||
if ":confirmed-commit" in m.server_capabilities:
|
||||
if ":confirmed-commit" in capabilities:
|
||||
m.commit(confirmed=True)
|
||||
m.commit()
|
||||
else:
|
||||
m.commit()
|
||||
|
||||
return changed
|
||||
finally:
|
||||
m.unlock(target=datastore)
|
||||
|
@ -188,22 +206,28 @@ def main():
|
|||
|
||||
module = AnsibleModule(
|
||||
argument_spec=dict(
|
||||
host=dict(type='str', required=True),
|
||||
port=dict(type='int', default=830),
|
||||
hostkey_verify=dict(type='bool', default=True),
|
||||
allow_agent=dict(type='bool', default=True),
|
||||
look_for_keys=dict(type='bool', default=True),
|
||||
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=False),
|
||||
src=dict(type='path', required=False),
|
||||
|
||||
datastore=dict(choices=['auto', 'candidate', 'running'], default='auto'),
|
||||
save=dict(type='bool', default=False),
|
||||
|
||||
# connection arguments
|
||||
host=dict(type='str'),
|
||||
port=dict(type='int', default=830),
|
||||
|
||||
username=dict(type='str', no_log=True),
|
||||
password=dict(type='str', no_log=True),
|
||||
|
||||
hostkey_verify=dict(type='bool', default=True),
|
||||
look_for_keys=dict(type='bool', default=True),
|
||||
|
||||
allow_agent=dict(type='bool', default=True),
|
||||
),
|
||||
mutually_exclusive=[('xml', 'src')]
|
||||
)
|
||||
|
||||
if not HAS_NCCLIENT:
|
||||
if not module._socket_path and not HAS_NCCLIENT:
|
||||
module.fail_json(msg='could not import the python library '
|
||||
'ncclient required by this module')
|
||||
|
||||
|
@ -214,68 +238,82 @@ def main():
|
|||
else:
|
||||
module.fail_json(msg='Option src or xml must be provided')
|
||||
|
||||
try:
|
||||
xml.dom.minidom.parseString(config_xml)
|
||||
local_connection = module._socket_path is None
|
||||
|
||||
except Exception as e:
|
||||
module.fail_json(msg='error parsing XML: %s' % to_native(e), exception=traceback.format_exc())
|
||||
if not local_connection:
|
||||
m = Connection(module._socket_path)
|
||||
capabilities = module.from_json(m.get_capabilities())
|
||||
server_capabilities = capabilities.get('server_capabilities')
|
||||
|
||||
nckwargs = dict(
|
||||
host=module.params['host'],
|
||||
port=module.params['port'],
|
||||
hostkey_verify=module.params['hostkey_verify'],
|
||||
allow_agent=module.params['allow_agent'],
|
||||
look_for_keys=module.params['look_for_keys'],
|
||||
username=module.params['username'],
|
||||
password=module.params['password'],
|
||||
)
|
||||
|
||||
try:
|
||||
m = ncclient.manager.connect(**nckwargs)
|
||||
except ncclient.transport.errors.AuthenticationError:
|
||||
module.fail_json(
|
||||
msg='authentication failed while connecting to device'
|
||||
else:
|
||||
nckwargs = dict(
|
||||
host=module.params['host'],
|
||||
port=module.params['port'],
|
||||
hostkey_verify=module.params['hostkey_verify'],
|
||||
allow_agent=module.params['allow_agent'],
|
||||
look_for_keys=module.params['look_for_keys'],
|
||||
username=module.params['username'],
|
||||
password=module.params['password'],
|
||||
)
|
||||
except Exception as e:
|
||||
module.fail_json(msg='error connecting to the device: %s' % to_native(e), exception=traceback.format_exc())
|
||||
|
||||
try:
|
||||
m = ncclient.manager.connect(**nckwargs)
|
||||
server_capabilities = list(m.server_capabilities)
|
||||
except ncclient.transport.errors.AuthenticationError:
|
||||
module.fail_json(
|
||||
msg='authentication failed while connecting to device'
|
||||
)
|
||||
except Exception as e:
|
||||
module.fail_json(msg='error connecting to the device: %s' % to_native(e), exception=traceback.format_exc())
|
||||
|
||||
try:
|
||||
xml.dom.minidom.parseString(config_xml)
|
||||
except Exception as e:
|
||||
module.fail_json(msg='error parsing XML: %s' % to_native(e), exception=traceback.format_exc())
|
||||
|
||||
retkwargs = dict()
|
||||
retkwargs['server_capabilities'] = list(m.server_capabilities)
|
||||
retkwargs['server_capabilities'] = server_capabilities
|
||||
|
||||
server_capabilities = '\n'.join(server_capabilities)
|
||||
|
||||
if module.params['datastore'] == 'candidate':
|
||||
if ':candidate' in m.server_capabilities:
|
||||
if ':candidate' in server_capabilities:
|
||||
datastore = 'candidate'
|
||||
else:
|
||||
m.close_session()
|
||||
if local_connection:
|
||||
m.close_session()
|
||||
module.fail_json(
|
||||
msg=':candidate is not supported by this netconf server'
|
||||
)
|
||||
elif module.params['datastore'] == 'running':
|
||||
if ':writable-running' in m.server_capabilities:
|
||||
if ':writable-running' in server_capabilities:
|
||||
datastore = 'running'
|
||||
else:
|
||||
m.close_session()
|
||||
if local_connection:
|
||||
m.close_session()
|
||||
module.fail_json(
|
||||
msg=':writable-running is not supported by this netconf server'
|
||||
)
|
||||
elif module.params['datastore'] == 'auto':
|
||||
if ':candidate' in m.server_capabilities:
|
||||
if ':candidate' in server_capabilities:
|
||||
datastore = 'candidate'
|
||||
elif ':writable-running' in m.server_capabilities:
|
||||
elif ':writable-running' in server_capabilities:
|
||||
datastore = 'running'
|
||||
else:
|
||||
m.close_session()
|
||||
if local_connection:
|
||||
m.close_session()
|
||||
module.fail_json(
|
||||
msg='neither :candidate nor :writable-running are supported by this netconf server'
|
||||
)
|
||||
else:
|
||||
m.close_session()
|
||||
if local_connection:
|
||||
m.close_session()
|
||||
module.fail_json(
|
||||
msg=module.params['datastore'] + ' datastore is not supported by this ansible module'
|
||||
)
|
||||
|
||||
if module.params['save']:
|
||||
if ':startup' not in m.server_capabilities:
|
||||
if ':startup' not in server_capabilities:
|
||||
module.fail_json(
|
||||
msg='cannot copy <running/> to <startup/>, while :startup is not supported'
|
||||
)
|
||||
|
@ -287,13 +325,16 @@ def main():
|
|||
commit=True,
|
||||
retkwargs=retkwargs,
|
||||
datastore=datastore,
|
||||
capabilities=server_capabilities,
|
||||
local_connection=local_connection
|
||||
)
|
||||
if changed and module.params['save']:
|
||||
m.copy_config(source="running", target="startup")
|
||||
except Exception as e:
|
||||
module.fail_json(msg='error editing configuration: %s' % to_native(e), exception=traceback.format_exc())
|
||||
finally:
|
||||
m.close_session()
|
||||
if local_connection:
|
||||
m.close_session()
|
||||
|
||||
module.exit_json(changed=changed, **retkwargs)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue