| 1 |
# |
| 2 |
# (c) 2017 Red Hat Inc. |
| 3 |
# (c) 2017 Kedar Kekan ([email protected]) |
| 4 |
# |
| 5 |
# This file is part of Ansible |
| 6 |
# |
| 7 |
# Ansible is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Ansible is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
from __future__ import absolute_import, division, print_function |
| 21 |
|
| 22 |
|
| 23 |
__metaclass__ = type |
| 24 |
|
| 25 |
DOCUMENTATION = """ |
| 26 |
author: Ansible Networking Team (@ansible-network) |
| 27 |
name: iosxr |
| 28 |
short_description: Use iosxr netconf plugin to run netconf commands on Cisco IOSXR |
| 29 |
platform |
| 30 |
description: |
| 31 |
- This iosxr plugin provides low level abstraction apis for sending and receiving |
| 32 |
netconf commands from Cisco iosxr network devices. |
| 33 |
version_added: 1.0.0 |
| 34 |
options: |
| 35 |
ncclient_device_handler: |
| 36 |
type: str |
| 37 |
default: iosxr |
| 38 |
description: |
| 39 |
- Specifies the ncclient device handler name for Cisco iosxr network os. To identify |
| 40 |
the ncclient device handler name refer ncclient library documentation. |
| 41 |
""" |
| 42 |
|
| 43 |
import collections |
| 44 |
import json |
| 45 |
import re |
| 46 |
|
| 47 |
from ansible.errors import AnsibleConnectionFailure |
| 48 |
from ansible.module_utils._text import to_native, to_text |
| 49 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.netconf import ( |
| 50 |
remove_namespaces, |
| 51 |
) |
| 52 |
from ansible_collections.ansible.netcommon.plugins.plugin_utils.netconf_base import ( |
| 53 |
NetconfBase, |
| 54 |
ensure_ncclient, |
| 55 |
) |
| 56 |
|
| 57 |
from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.iosxr import ( |
| 58 |
build_xml, |
| 59 |
etree_find, |
| 60 |
) |
| 61 |
|
| 62 |
|
| 63 |
try: |
| 64 |
from ncclient import manager |
| 65 |
from ncclient.operations import RPCError |
| 66 |
from ncclient.transport.errors import SSHUnknownHostError |
| 67 |
from ncclient.xml_ import to_xml |
| 68 |
|
| 69 |
HAS_NCCLIENT = True |
| 70 |
except ( |
| 71 |
ImportError, |
| 72 |
AttributeError, |
| 73 |
): # paramiko and gssapi are incompatible and raise AttributeError not ImportError |
| 74 |
HAS_NCCLIENT = False |
| 75 |
|
| 76 |
|
| 77 |
class Netconf(NetconfBase): |
| 78 |
def get_device_info(self): |
| 79 |
device_info = {} |
| 80 |
device_info["network_os"] = "iosxr" |
| 81 |
install_meta = collections.OrderedDict() |
| 82 |
install_meta.update( |
| 83 |
[ |
| 84 |
("prepare", {"xpath": "install/prepare", "tag": True}), |
| 85 |
( |
| 86 |
"prepared-boot-image", |
| 87 |
{ |
| 88 |
"xpath": "install/prepare/prepared-boot-image", |
| 89 |
"tag": True, |
| 90 |
}, |
| 91 |
), |
| 92 |
("version", {"xpath": "install/version", "tag": True}), |
| 93 |
("label", {"xpath": "install/version/label", "tag": True}), |
| 94 |
( |
| 95 |
"hardware-info", |
| 96 |
{"xpath": "install/version/hardware-info", "tag": True}, |
| 97 |
), |
| 98 |
("package", {"xpath": "install/version/package", "tag": True}), |
| 99 |
], |
| 100 |
) |
| 101 |
install_filter = build_xml( |
| 102 |
"install", |
| 103 |
install_meta, |
| 104 |
opcode="filter", |
| 105 |
namespace="install", |
| 106 |
) |
| 107 |
try: |
| 108 |
reply = self.get(install_filter) |
| 109 |
resp = remove_namespaces( |
| 110 |
re.sub(r'<\?xml version="1.0" encoding="UTF-8"\?>', "", reply), |
| 111 |
) |
| 112 |
ele_package_name = etree_find(resp.strip(), "name") |
| 113 |
if ele_package_name is not None: |
| 114 |
device_info["network_os_package"] = ele_package_name.text |
| 115 |
ele_label = etree_find(resp.strip(), "label") |
| 116 |
if ele_label is not None: |
| 117 |
device_info["network_os_version"] = ele_label.text |
| 118 |
|
| 119 |
model_search_strs = [ |
| 120 |
r"^[Cc]isco (.+) \(\) processor", |
| 121 |
r"^[Cc]isco (.+) \(revision", |
| 122 |
r"^[Cc]isco (\S+ \S+).+bytes of .*memory", |
| 123 |
] |
| 124 |
ele_hardware_info = etree_find(resp.strip(), "hardware-info") |
| 125 |
if ele_hardware_info is not None: |
| 126 |
for item in model_search_strs: |
| 127 |
match = re.search(item, ele_hardware_info.text, re.M) |
| 128 |
if match: |
| 129 |
device_info["network_os_model"] = match.group(1) |
| 130 |
break |
| 131 |
except Exception as exc: |
| 132 |
error_msg = to_text(exc, errors="surrogate_or_strict").strip() |
| 133 |
if "bad-namespace" in error_msg: |
| 134 |
device_info = self.get_device_info_old_version() |
| 135 |
else: |
| 136 |
self._connection.queue_message( |
| 137 |
"vvvv", |
| 138 |
"Fail to retrieve device info %s" % exc, |
| 139 |
) |
| 140 |
try: |
| 141 |
hostname_filter = build_xml( |
| 142 |
"host-names", |
| 143 |
opcode="filter", |
| 144 |
namespace="host-names", |
| 145 |
) |
| 146 |
reply = self.get(hostname_filter) |
| 147 |
resp = remove_namespaces( |
| 148 |
re.sub(r'<\?xml version="1.0" encoding="UTF-8"\?>', "", reply), |
| 149 |
) |
| 150 |
hostname_ele = etree_find(resp.strip(), "host-name") |
| 151 |
device_info["network_os_hostname"] = ( |
| 152 |
hostname_ele.text if hostname_ele is not None else None |
| 153 |
) |
| 154 |
except Exception as exc: |
| 155 |
self._connection.queue_message( |
| 156 |
"vvvv", |
| 157 |
"Fail to retrieve device info %s" % exc, |
| 158 |
) |
| 159 |
return device_info |
| 160 |
|
| 161 |
def get_capabilities(self): |
| 162 |
result = dict() |
| 163 |
result["rpc"] = self.get_base_rpc() |
| 164 |
result["network_api"] = "netconf" |
| 165 |
result["device_info"] = self.get_device_info() |
| 166 |
result["server_capabilities"] = list(self.m.server_capabilities) |
| 167 |
result["client_capabilities"] = list(self.m.client_capabilities) |
| 168 |
result["session_id"] = self.m.session_id |
| 169 |
result["device_operations"] = self.get_device_operations( |
| 170 |
result["server_capabilities"], |
| 171 |
) |
| 172 |
return json.dumps(result) |
| 173 |
|
| 174 |
@staticmethod |
| 175 |
@ensure_ncclient |
| 176 |
def guess_network_os(obj): |
| 177 |
""" |
| 178 |
Guess the remote network os name |
| 179 |
:param obj: Netconf connection class object |
| 180 |
:return: Network OS name |
| 181 |
""" |
| 182 |
try: |
| 183 |
m = manager.connect( |
| 184 |
host=obj._play_context.remote_addr, |
| 185 |
port=obj._play_context.port or 830, |
| 186 |
username=obj._play_context.remote_user, |
| 187 |
password=obj._play_context.password, |
| 188 |
key_filename=obj.key_filename, |
| 189 |
hostkey_verify=obj.get_option("host_key_checking"), |
| 190 |
look_for_keys=obj.get_option("look_for_keys"), |
| 191 |
allow_agent=obj._play_context.allow_agent, |
| 192 |
timeout=obj.get_option("persistent_connect_timeout"), |
| 193 |
# We need to pass in the path to the ssh_config file when guessing |
| 194 |
# the network_os so that a jumphost is correctly used if defined |
| 195 |
ssh_config=obj._ssh_config, |
| 196 |
) |
| 197 |
except SSHUnknownHostError as exc: |
| 198 |
raise AnsibleConnectionFailure(to_native(exc)) |
| 199 |
|
| 200 |
guessed_os = None |
| 201 |
for c in m.server_capabilities: |
| 202 |
if re.search("IOS-XR", c): |
| 203 |
guessed_os = "iosxr" |
| 204 |
break |
| 205 |
|
| 206 |
m.close_session() |
| 207 |
return guessed_os |
| 208 |
|
| 209 |
# TODO: change .xml to .data_xml, when ncclient supports data_xml on all platforms |
| 210 |
def get(self, filter=None, with_defaults=None, remove_ns=False): |
| 211 |
if isinstance(filter, list): |
| 212 |
filter = tuple(filter) |
| 213 |
try: |
| 214 |
resp = self.m.get(filter=filter, with_defaults=with_defaults) |
| 215 |
if remove_ns: |
| 216 |
response = remove_namespaces(resp) |
| 217 |
else: |
| 218 |
response = resp.data_xml if hasattr(resp, "data_xml") else resp.xml |
| 219 |
return response |
| 220 |
except RPCError as exc: |
| 221 |
raise Exception(to_xml(exc.xml)) |
| 222 |
|
| 223 |
def get_config(self, source=None, filter=None, remove_ns=False): |
| 224 |
if isinstance(filter, list): |
| 225 |
filter = tuple(filter) |
| 226 |
try: |
| 227 |
resp = self.m.get_config(source=source, filter=filter) |
| 228 |
if remove_ns: |
| 229 |
response = remove_namespaces(resp) |
| 230 |
else: |
| 231 |
response = resp.data_xml if hasattr(resp, "data_xml") else resp.xml |
| 232 |
return response |
| 233 |
except RPCError as exc: |
| 234 |
raise Exception(to_xml(exc.xml)) |
| 235 |
|
| 236 |
def edit_config( |
| 237 |
self, |
| 238 |
config=None, |
| 239 |
format="xml", |
| 240 |
target="candidate", |
| 241 |
default_operation=None, |
| 242 |
test_option=None, |
| 243 |
error_option=None, |
| 244 |
remove_ns=False, |
| 245 |
): |
| 246 |
if config is None: |
| 247 |
raise ValueError("config value must be provided") |
| 248 |
try: |
| 249 |
resp = self.m.edit_config( |
| 250 |
config, |
| 251 |
format=format, |
| 252 |
target=target, |
| 253 |
default_operation=default_operation, |
| 254 |
test_option=test_option, |
| 255 |
error_option=error_option, |
| 256 |
) |
| 257 |
if remove_ns: |
| 258 |
response = remove_namespaces(resp) |
| 259 |
else: |
| 260 |
response = resp.data_xml if hasattr(resp, "data_xml") else resp.xml |
| 261 |
return response |
| 262 |
except RPCError as exc: |
| 263 |
raise Exception(to_xml(exc.xml)) |
| 264 |
|
| 265 |
def commit( |
| 266 |
self, |
| 267 |
confirmed=False, |
| 268 |
timeout=None, |
| 269 |
persist=None, |
| 270 |
remove_ns=False, |
| 271 |
): |
| 272 |
timeout = to_text(timeout, errors="surrogate_or_strict") |
| 273 |
try: |
| 274 |
resp = self.m.commit( |
| 275 |
confirmed=confirmed, |
| 276 |
timeout=timeout, |
| 277 |
persist=persist, |
| 278 |
) |
| 279 |
if remove_ns: |
| 280 |
response = remove_namespaces(resp) |
| 281 |
else: |
| 282 |
response = resp.data_xml if hasattr(resp, "data_xml") else resp.xml |
| 283 |
return response |
| 284 |
except RPCError as exc: |
| 285 |
raise Exception(to_xml(exc.xml)) |
| 286 |
|
| 287 |
def validate(self, source="candidate", remove_ns=False): |
| 288 |
try: |
| 289 |
resp = self.m.validate(source=source) |
| 290 |
if remove_ns: |
| 291 |
response = remove_namespaces(resp) |
| 292 |
else: |
| 293 |
response = resp.data_xml if hasattr(resp, "data_xml") else resp.xml |
| 294 |
return response |
| 295 |
except RPCError as exc: |
| 296 |
raise Exception(to_xml(exc.xml)) |
| 297 |
|
| 298 |
def discard_changes(self, remove_ns=False): |
| 299 |
try: |
| 300 |
resp = self.m.discard_changes() |
| 301 |
if remove_ns: |
| 302 |
response = remove_namespaces(resp) |
| 303 |
else: |
| 304 |
response = resp.data_xml if hasattr(resp, "data_xml") else resp.xml |
| 305 |
return response |
| 306 |
except RPCError as exc: |
| 307 |
raise Exception(to_xml(exc.xml)) |
| 308 |
|
| 309 |
def get_device_info_old_version(self): |
| 310 |
device_info = {} |
| 311 |
device_info["network_os"] = "iosxr" |
| 312 |
install_meta = collections.OrderedDict() |
| 313 |
install_meta.update( |
| 314 |
[ |
| 315 |
( |
| 316 |
"boot-variables", |
| 317 |
{"xpath": "install/boot-variables", "tag": True}, |
| 318 |
), |
| 319 |
( |
| 320 |
"boot-variable", |
| 321 |
{ |
| 322 |
"xpath": "install/boot-variables/boot-variable", |
| 323 |
"tag": True, |
| 324 |
"lead": True, |
| 325 |
}, |
| 326 |
), |
| 327 |
("software", {"xpath": "install/software", "tag": True}), |
| 328 |
( |
| 329 |
"alias-devices", |
| 330 |
{"xpath": "install/software/alias-devices", "tag": True}, |
| 331 |
), |
| 332 |
( |
| 333 |
"alias-device", |
| 334 |
{ |
| 335 |
"xpath": "install/software/alias-devices/alias-device", |
| 336 |
"tag": True, |
| 337 |
}, |
| 338 |
), |
| 339 |
( |
| 340 |
"m:device-name", |
| 341 |
{ |
| 342 |
"xpath": "install/software/alias-devices/alias-device/device-name", |
| 343 |
"value": "disk0:", |
| 344 |
}, |
| 345 |
), |
| 346 |
], |
| 347 |
) |
| 348 |
|
| 349 |
install_filter = build_xml( |
| 350 |
"install", |
| 351 |
install_meta, |
| 352 |
opcode="filter", |
| 353 |
namespace="install_old", |
| 354 |
) |
| 355 |
try: |
| 356 |
reply = self.get(install_filter) |
| 357 |
resp = remove_namespaces( |
| 358 |
re.sub(r'<\?xml version="1.0" encoding="UTF-8"\?>', "", reply), |
| 359 |
) |
| 360 |
ele_boot_variable = etree_find(resp, "boot-variable/boot-variable") |
| 361 |
if ele_boot_variable is not None: |
| 362 |
device_info["network_os_image"] = re.split( |
| 363 |
"[:|,]", |
| 364 |
ele_boot_variable.text, |
| 365 |
)[1] |
| 366 |
ele_package_name = etree_find(reply, "package-name") |
| 367 |
if ele_package_name is not None: |
| 368 |
device_info["network_os_package"] = ele_package_name.text |
| 369 |
device_info["network_os_version"] = re.split( |
| 370 |
"-", |
| 371 |
ele_package_name.text, |
| 372 |
)[-1] |
| 373 |
except Exception as exc: |
| 374 |
self._connection.queue_message( |
| 375 |
"vvvv", |
| 376 |
"Fail to retrieve device info %s" % exc, |
| 377 |
) |
| 378 |
return device_info |