2017-10-24 03:19:23 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2017, Ansible by Red Hat, Inc
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'network'}
|
|
|
|
|
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: iosxr_netconf
|
|
|
|
version_added: "2.5"
|
|
|
|
author: "Kedar Kekan (@kedarX)"
|
|
|
|
short_description: Configures NetConf sub-system service on Cisco IOS-XR devices
|
|
|
|
description:
|
|
|
|
- This module provides an abstraction that enables and configures
|
|
|
|
the netconf system service running on Cisco IOS-XR Software.
|
|
|
|
This module can be used to easily enable the Netconf API. Netconf provides
|
|
|
|
a programmatic interface for working with configuration and state
|
|
|
|
resources as defined in RFC 6242.
|
|
|
|
extends_documentation_fragment: iosxr
|
|
|
|
options:
|
|
|
|
netconf_port:
|
|
|
|
description:
|
|
|
|
- This argument specifies the port the netconf service should
|
|
|
|
listen on for SSH connections. The default port as defined
|
|
|
|
in RFC 6242 is 830.
|
|
|
|
required: false
|
|
|
|
default: 830
|
|
|
|
aliases: ['listens_on']
|
|
|
|
netconf_vrf:
|
|
|
|
description:
|
|
|
|
- netconf vrf name
|
|
|
|
required: false
|
2017-12-06 17:07:31 +00:00
|
|
|
default: default
|
2018-02-12 17:42:25 +00:00
|
|
|
aliases: ['vrf']
|
2017-10-24 03:19:23 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Specifies the state of the C(iosxr_netconf) resource on
|
|
|
|
the remote device. If the I(state) argument is set to
|
|
|
|
I(present) the netconf service will be configured. If the
|
|
|
|
I(state) argument is set to I(absent) the netconf service
|
|
|
|
will be removed from the configuration.
|
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
notes:
|
|
|
|
- Tested against Cisco IOS XR Software, Version 6.1.2
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
|
|
|
- name: enable netconf service on port 830
|
|
|
|
iosxr_netconf:
|
|
|
|
listens_on: 830
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: disable netconf service
|
|
|
|
iosxr_netconf:
|
|
|
|
state: absent
|
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
commands:
|
|
|
|
description: Returns the command sent to the remote device
|
|
|
|
returned: when changed is True
|
|
|
|
type: str
|
|
|
|
sample: 'ssh server netconf port 830'
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2017-12-06 17:07:31 +00:00
|
|
|
from ansible.module_utils.network.iosxr.iosxr import iosxr_argument_spec
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.iosxr.iosxr import get_config, load_config
|
2017-10-24 03:19:23 +00:00
|
|
|
from ansible.module_utils.six import iteritems
|
|
|
|
|
|
|
|
USE_PERSISTENT_CONNECTION = True
|
|
|
|
|
|
|
|
|
2017-12-07 14:44:57 +00:00
|
|
|
def map_obj_to_commands(updates):
|
2017-10-24 03:19:23 +00:00
|
|
|
want, have = updates
|
|
|
|
commands = list()
|
|
|
|
|
|
|
|
if want['state'] == 'absent':
|
|
|
|
if have['state'] == 'present':
|
|
|
|
commands.append('no netconf-yang agent ssh')
|
|
|
|
|
2017-12-06 17:07:31 +00:00
|
|
|
if 'netconf_port' in have:
|
|
|
|
commands.append('no ssh server netconf port %s' % have['netconf_port'])
|
2017-10-24 03:19:23 +00:00
|
|
|
|
2017-12-06 17:07:31 +00:00
|
|
|
if have['netconf_vrf']:
|
|
|
|
for vrf in have['netconf_vrf']:
|
|
|
|
commands.append('no ssh server netconf vrf %s' % vrf)
|
2017-10-24 03:19:23 +00:00
|
|
|
else:
|
|
|
|
if have['state'] == 'absent':
|
|
|
|
commands.append('netconf-yang agent ssh')
|
|
|
|
|
|
|
|
if want['netconf_port'] is not None and (want['netconf_port'] != have.get('netconf_port')):
|
|
|
|
commands.append(
|
|
|
|
'ssh server netconf port %s' % want['netconf_port']
|
|
|
|
)
|
|
|
|
if want['netconf_vrf'] is not None and (want['netconf_vrf'] not in have['netconf_vrf']):
|
|
|
|
commands.append(
|
|
|
|
'ssh server netconf vrf %s' % want['netconf_vrf']
|
|
|
|
)
|
|
|
|
|
|
|
|
return commands
|
|
|
|
|
|
|
|
|
|
|
|
def parse_vrf(config):
|
|
|
|
match = re.search(r'vrf (\w+)', config)
|
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_port(config):
|
|
|
|
match = re.search(r'port (\d+)', config)
|
|
|
|
if match:
|
|
|
|
return int(match.group(1))
|
|
|
|
|
|
|
|
|
|
|
|
def map_config_to_obj(module):
|
|
|
|
obj = {'state': 'absent'}
|
|
|
|
|
2017-12-06 17:07:31 +00:00
|
|
|
netconf_config = get_config(module, config_filter='netconf-yang agent')
|
2017-10-24 03:19:23 +00:00
|
|
|
|
2017-12-06 17:07:31 +00:00
|
|
|
ssh_config = get_config(module, config_filter='ssh server')
|
2017-10-24 03:19:23 +00:00
|
|
|
ssh_config = [config_line for config_line in (line.strip() for line in ssh_config.splitlines()) if config_line]
|
|
|
|
obj['netconf_vrf'] = []
|
|
|
|
for config in ssh_config:
|
|
|
|
if 'netconf port' in config:
|
|
|
|
obj.update({'netconf_port': parse_port(config)})
|
|
|
|
if 'netconf vrf' in config:
|
|
|
|
obj['netconf_vrf'].append(parse_vrf(config))
|
2017-12-06 17:07:31 +00:00
|
|
|
if 'ssh' in netconf_config and ('netconf_port' in obj or obj['netconf_vrf']):
|
2017-10-24 03:19:23 +00:00
|
|
|
obj.update({'state': 'present'})
|
|
|
|
|
|
|
|
if 'ssh' in netconf_config and 'netconf_port' not in obj:
|
|
|
|
obj.update({'netconf_port': 830})
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
def validate_netconf_port(value, module):
|
|
|
|
if not 1 <= value <= 65535:
|
|
|
|
module.fail_json(msg='netconf_port must be between 1 and 65535')
|
|
|
|
|
|
|
|
|
|
|
|
def map_params_to_obj(module):
|
|
|
|
obj = {
|
|
|
|
'netconf_port': module.params['netconf_port'],
|
|
|
|
'netconf_vrf': module.params['netconf_vrf'],
|
|
|
|
'state': module.params['state']
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value in iteritems(obj):
|
|
|
|
# validate the param value (if validator func exists)
|
|
|
|
validator = globals().get('validate_%s' % key)
|
|
|
|
if callable(validator):
|
|
|
|
validator(value, module)
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""main entry point for module execution
|
|
|
|
"""
|
|
|
|
argument_spec = dict(
|
|
|
|
netconf_port=dict(type='int', default=830, aliases=['listens_on']),
|
2017-12-06 17:07:31 +00:00
|
|
|
netconf_vrf=dict(aliases=['vrf'], default='default'),
|
2017-10-24 03:19:23 +00:00
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
)
|
|
|
|
argument_spec.update(iosxr_argument_spec)
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True)
|
|
|
|
|
|
|
|
warnings = list()
|
|
|
|
|
|
|
|
result = {'changed': False, 'warnings': warnings}
|
|
|
|
|
|
|
|
want = map_params_to_obj(module)
|
|
|
|
have = map_config_to_obj(module)
|
2017-12-07 14:44:57 +00:00
|
|
|
commands = map_obj_to_commands((want, have))
|
2017-10-24 03:19:23 +00:00
|
|
|
result['commands'] = commands
|
|
|
|
|
|
|
|
if commands:
|
2017-12-07 14:44:57 +00:00
|
|
|
commit = not module.check_mode
|
|
|
|
diff = load_config(module, commands, commit=commit)
|
|
|
|
if diff:
|
|
|
|
result['diff'] = dict(prepared=diff)
|
2017-10-24 03:19:23 +00:00
|
|
|
result['changed'] = True
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|