| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# (c) 2017, Ansible by Red Hat, Inc |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = """ |
| 14 |
module: iosxr_netconf |
| 15 |
author: Kedar Kekan (@kedarX) |
| 16 |
short_description: Configures NetConf sub-system service on Cisco IOS-XR devices |
| 17 |
description: |
| 18 |
- This module provides an abstraction that enables and configures the netconf system |
| 19 |
service running on Cisco IOS-XR Software. This module can be used to easily enable |
| 20 |
the Netconf API. Netconf provides a programmatic interface for working with configuration |
| 21 |
and state resources as defined in RFC 6242. |
| 22 |
version_added: 1.0.0 |
| 23 |
extends_documentation_fragment: |
| 24 |
- cisco.iosxr.iosxr |
| 25 |
options: |
| 26 |
netconf_port: |
| 27 |
description: |
| 28 |
- This argument specifies the port the netconf service should listen on for SSH |
| 29 |
connections. The default port as defined in RFC 6242 is 830. |
| 30 |
required: false |
| 31 |
type: int |
| 32 |
default: 830 |
| 33 |
aliases: |
| 34 |
- listens_on |
| 35 |
netconf_vrf: |
| 36 |
description: |
| 37 |
- netconf vrf name |
| 38 |
required: false |
| 39 |
type: str |
| 40 |
default: default |
| 41 |
aliases: |
| 42 |
- vrf |
| 43 |
state: |
| 44 |
description: |
| 45 |
- Specifies the state of the C(iosxr_netconf) resource on the remote device. If |
| 46 |
the I(state) argument is set to I(present) the netconf service will be configured. If |
| 47 |
the I(state) argument is set to I(absent) the netconf service will be removed |
| 48 |
from the configuration. |
| 49 |
type: str |
| 50 |
required: false |
| 51 |
default: present |
| 52 |
choices: |
| 53 |
- present |
| 54 |
- absent |
| 55 |
notes: |
| 56 |
- This module works with connection C(network_cli). See L(the IOS-XR Platform Options,../network/user_guide/platform_iosxr.html). |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = """ |
| 60 |
- name: enable netconf service on port 830 |
| 61 |
cisco.iosxr.iosxr_netconf: |
| 62 |
listens_on: 830 |
| 63 |
state: present |
| 64 |
|
| 65 |
- name: disable netconf service |
| 66 |
cisco.iosxr.iosxr_netconf: |
| 67 |
state: absent |
| 68 |
""" |
| 69 |
|
| 70 |
RETURN = """ |
| 71 |
commands: |
| 72 |
description: Returns the command sent to the remote device |
| 73 |
returned: when changed is True |
| 74 |
type: str |
| 75 |
sample: 'ssh server netconf port 830' |
| 76 |
""" |
| 77 |
import re |
| 78 |
|
| 79 |
from ansible.module_utils.basic import AnsibleModule |
| 80 |
|
| 81 |
from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.iosxr import ( |
| 82 |
get_config, |
| 83 |
load_config, |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
USE_PERSISTENT_CONNECTION = True |
| 88 |
|
| 89 |
|
| 90 |
def map_obj_to_commands(updates): |
| 91 |
want, have = updates |
| 92 |
commands = list() |
| 93 |
|
| 94 |
if want["state"] == "absent": |
| 95 |
if have["state"] == "present": |
| 96 |
commands.append("no netconf-yang agent ssh") |
| 97 |
|
| 98 |
if "netconf_port" in have: |
| 99 |
commands.append("no ssh server netconf port %s" % have["netconf_port"]) |
| 100 |
|
| 101 |
if have["netconf_vrf"]: |
| 102 |
for vrf in have["netconf_vrf"]: |
| 103 |
commands.append("no ssh server netconf vrf %s" % vrf) |
| 104 |
else: |
| 105 |
if have["state"] == "absent": |
| 106 |
commands.append("netconf-yang agent ssh") |
| 107 |
|
| 108 |
if want["netconf_port"] is not None and (want["netconf_port"] != have.get("netconf_port")): |
| 109 |
commands.append("ssh server netconf port %s" % want["netconf_port"]) |
| 110 |
if want["netconf_vrf"] is not None and (want["netconf_vrf"] not in have["netconf_vrf"]): |
| 111 |
commands.append("ssh server netconf vrf %s" % want["netconf_vrf"]) |
| 112 |
|
| 113 |
return commands |
| 114 |
|
| 115 |
|
| 116 |
def parse_vrf(config): |
| 117 |
match = re.search(r"vrf (\w+)", config) |
| 118 |
if match: |
| 119 |
return match.group(1) |
| 120 |
|
| 121 |
|
| 122 |
def parse_port(config): |
| 123 |
match = re.search(r"port (\d+)", config) |
| 124 |
if match: |
| 125 |
return int(match.group(1)) |
| 126 |
|
| 127 |
|
| 128 |
def map_config_to_obj(module): |
| 129 |
obj = {"state": "absent"} |
| 130 |
|
| 131 |
netconf_config = get_config(module, config_filter="netconf-yang agent") |
| 132 |
|
| 133 |
ssh_config = get_config(module, config_filter="ssh server") |
| 134 |
ssh_config = [ |
| 135 |
config_line |
| 136 |
for config_line in (line.strip() for line in ssh_config.splitlines()) |
| 137 |
if config_line |
| 138 |
] |
| 139 |
obj["netconf_vrf"] = [] |
| 140 |
for config in ssh_config: |
| 141 |
if "netconf port" in config: |
| 142 |
obj.update({"netconf_port": parse_port(config)}) |
| 143 |
if "netconf vrf" in config: |
| 144 |
obj["netconf_vrf"].append(parse_vrf(config)) |
| 145 |
if "ssh" in netconf_config and ("netconf_port" in obj or obj["netconf_vrf"]): |
| 146 |
obj.update({"state": "present"}) |
| 147 |
|
| 148 |
if "ssh" in netconf_config and "netconf_port" not in obj: |
| 149 |
obj.update({"netconf_port": 830}) |
| 150 |
|
| 151 |
return obj |
| 152 |
|
| 153 |
|
| 154 |
def validate_netconf_port(value, module): |
| 155 |
if not 1 <= value <= 65535: |
| 156 |
module.fail_json(msg="netconf_port must be between 1 and 65535") |
| 157 |
|
| 158 |
|
| 159 |
def map_params_to_obj(module): |
| 160 |
obj = { |
| 161 |
"netconf_port": module.params["netconf_port"], |
| 162 |
"netconf_vrf": module.params["netconf_vrf"], |
| 163 |
"state": module.params["state"], |
| 164 |
} |
| 165 |
|
| 166 |
for key, value in obj.items(): |
| 167 |
# validate the param value (if validator func exists) |
| 168 |
validator = globals().get("validate_%s" % key) |
| 169 |
if callable(validator): |
| 170 |
validator(value, module) |
| 171 |
|
| 172 |
return obj |
| 173 |
|
| 174 |
|
| 175 |
def main(): |
| 176 |
"""main entry point for module execution""" |
| 177 |
argument_spec = dict( |
| 178 |
netconf_port=dict(type="int", default=830, aliases=["listens_on"]), |
| 179 |
netconf_vrf=dict(aliases=["vrf"], default="default"), |
| 180 |
state=dict(default="present", choices=["present", "absent"]), |
| 181 |
) |
| 182 |
|
| 183 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 184 |
|
| 185 |
warnings = list() |
| 186 |
|
| 187 |
result = {"changed": False, "warnings": warnings} |
| 188 |
|
| 189 |
want = map_params_to_obj(module) |
| 190 |
have = map_config_to_obj(module) |
| 191 |
commands = map_obj_to_commands((want, have)) |
| 192 |
result["commands"] = commands |
| 193 |
|
| 194 |
if commands: |
| 195 |
commit = not module.check_mode |
| 196 |
diff = load_config(module, commands, commit=commit) |
| 197 |
if diff: |
| 198 |
result["diff"] = dict(prepared=diff) |
| 199 |
result["changed"] = True |
| 200 |
|
| 201 |
module.exit_json(**result) |
| 202 |
|
| 203 |
|
| 204 |
if __name__ == "__main__": |
| 205 |
main() |