| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2022, Felix Fontein <[email protected]> |
| 4 |
# Copyright (c) 2020, Nikolay Dachev <[email protected]> |
| 5 |
# Copyright (c) 2018, Egor Zaitsev (@heuels) |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import (absolute_import, division, print_function) |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: api_facts |
| 14 |
author: |
| 15 |
- "Egor Zaitsev (@heuels)" |
| 16 |
- "Nikolay Dachev (@NikolayDachev)" |
| 17 |
- "Felix Fontein (@felixfontein)" |
| 18 |
version_added: 2.1.0 |
| 19 |
short_description: Collect facts from remote devices running MikroTik RouterOS using the API |
| 20 |
description: |
| 21 |
- Collects a base set of device facts from a remote device that is running RouterOS. This module prepends all of the base |
| 22 |
network fact keys with C(ansible_net_<fact>). The facts module will always collect a base set of facts from the device |
| 23 |
and can enable or disable collection of additional facts. |
| 24 |
- As opposed to the M(community.routeros.facts) module, it uses the RouterOS API, similar to the M(community.routeros.api) |
| 25 |
module. |
| 26 |
extends_documentation_fragment: |
| 27 |
- community.routeros.api |
| 28 |
- community.routeros.attributes |
| 29 |
- community.routeros.attributes.actiongroup_api |
| 30 |
- community.routeros.attributes.facts |
| 31 |
- community.routeros.attributes.facts_module |
| 32 |
- community.routeros.attributes.idempotent_not_modify_state |
| 33 |
attributes: |
| 34 |
platform: |
| 35 |
support: full |
| 36 |
platforms: RouterOS |
| 37 |
options: |
| 38 |
gather_subset: |
| 39 |
description: |
| 40 |
- When supplied, this argument will restrict the facts collected to a given subset. Possible values for this argument |
| 41 |
include V(all), V(hardware), V(interfaces), and V(routing). |
| 42 |
- Can specify a list of values to include a larger subset. Values can also be used with an initial V(!) to specify that |
| 43 |
a specific subset should not be collected. |
| 44 |
required: false |
| 45 |
default: |
| 46 |
- all |
| 47 |
type: list |
| 48 |
elements: str |
| 49 |
seealso: |
| 50 |
- module: community.routeros.facts |
| 51 |
- module: community.routeros.api |
| 52 |
- module: community.routeros.api_find_and_modify |
| 53 |
- module: community.routeros.api_info |
| 54 |
- module: community.routeros.api_modify |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
--- |
| 59 |
- name: Collect all facts from the device |
| 60 |
community.routeros.api_facts: |
| 61 |
hostname: 192.168.88.1 |
| 62 |
username: admin |
| 63 |
password: password |
| 64 |
gather_subset: all |
| 65 |
|
| 66 |
- name: Do not collect hardware facts |
| 67 |
community.routeros.api_facts: |
| 68 |
hostname: 192.168.88.1 |
| 69 |
username: admin |
| 70 |
password: password |
| 71 |
gather_subset: |
| 72 |
- "!hardware" |
| 73 |
""" |
| 74 |
|
| 75 |
RETURN = r""" |
| 76 |
ansible_facts: |
| 77 |
description: "Dictionary of IP geolocation facts for a host's IP address." |
| 78 |
returned: always |
| 79 |
type: dict |
| 80 |
contains: |
| 81 |
ansible_net_gather_subset: |
| 82 |
description: The list of fact subsets collected from the device. |
| 83 |
returned: always |
| 84 |
type: list |
| 85 |
|
| 86 |
# default |
| 87 |
ansible_net_model: |
| 88 |
description: The model name returned from the device. |
| 89 |
returned: O(gather_subset) contains V(default) |
| 90 |
type: str |
| 91 |
ansible_net_serialnum: |
| 92 |
description: The serial number of the remote device. |
| 93 |
returned: O(gather_subset) contains V(default) |
| 94 |
type: str |
| 95 |
ansible_net_version: |
| 96 |
description: The operating system version running on the remote device. |
| 97 |
returned: O(gather_subset) contains V(default) |
| 98 |
type: str |
| 99 |
ansible_net_hostname: |
| 100 |
description: The configured hostname of the device. |
| 101 |
returned: O(gather_subset) contains V(default) |
| 102 |
type: str |
| 103 |
ansible_net_arch: |
| 104 |
description: The CPU architecture of the device. |
| 105 |
returned: O(gather_subset) contains V(default) |
| 106 |
type: str |
| 107 |
ansible_net_uptime: |
| 108 |
description: The uptime of the device. |
| 109 |
returned: O(gather_subset) contains V(default) |
| 110 |
type: str |
| 111 |
ansible_net_cpu_load: |
| 112 |
description: Current CPU load. |
| 113 |
returned: O(gather_subset) contains V(default) |
| 114 |
type: str |
| 115 |
|
| 116 |
# hardware |
| 117 |
ansible_net_spacefree_mb: |
| 118 |
description: The available disk space on the remote device in MiB. |
| 119 |
returned: O(gather_subset) contains V(hardware) |
| 120 |
type: dict |
| 121 |
ansible_net_spacetotal_mb: |
| 122 |
description: The total disk space on the remote device in MiB. |
| 123 |
returned: O(gather_subset) contains V(hardware) |
| 124 |
type: dict |
| 125 |
ansible_net_memfree_mb: |
| 126 |
description: The available free memory on the remote device in MiB. |
| 127 |
returned: O(gather_subset) contains V(hardware) |
| 128 |
type: int |
| 129 |
ansible_net_memtotal_mb: |
| 130 |
description: The total memory on the remote device in MiB. |
| 131 |
returned: O(gather_subset) contains V(hardware) |
| 132 |
type: int |
| 133 |
|
| 134 |
# interfaces |
| 135 |
ansible_net_all_ipv4_addresses: |
| 136 |
description: All IPv4 addresses configured on the device. |
| 137 |
returned: O(gather_subset) contains V(interfaces) |
| 138 |
type: list |
| 139 |
ansible_net_all_ipv6_addresses: |
| 140 |
description: All IPv6 addresses configured on the device. |
| 141 |
returned: O(gather_subset) contains V(interfaces) |
| 142 |
type: list |
| 143 |
ansible_net_interfaces: |
| 144 |
description: A hash of all interfaces running on the system. |
| 145 |
returned: O(gather_subset) contains V(interfaces) |
| 146 |
type: dict |
| 147 |
ansible_net_neighbors: |
| 148 |
description: The list of neighbors from the remote device. |
| 149 |
returned: O(gather_subset) contains V(interfaces) |
| 150 |
type: dict |
| 151 |
|
| 152 |
# routing |
| 153 |
ansible_net_bgp_peer: |
| 154 |
description: A dictionary with BGP peer information. |
| 155 |
returned: O(gather_subset) contains V(routing) |
| 156 |
type: dict |
| 157 |
ansible_net_bgp_vpnv4_route: |
| 158 |
description: A dictionary with BGP vpnv4 route information. |
| 159 |
returned: O(gather_subset) contains V(routing) |
| 160 |
type: dict |
| 161 |
ansible_net_bgp_instance: |
| 162 |
description: A dictionary with BGP instance information. |
| 163 |
returned: O(gather_subset) contains V(routing) |
| 164 |
type: dict |
| 165 |
ansible_net_route: |
| 166 |
description: A dictionary for routes in all routing tables. |
| 167 |
returned: O(gather_subset) contains V(routing) |
| 168 |
type: dict |
| 169 |
ansible_net_ospf_instance: |
| 170 |
description: A dictionary with OSPF instances. |
| 171 |
returned: O(gather_subset) contains V(routing) |
| 172 |
type: dict |
| 173 |
ansible_net_ospf_neighbor: |
| 174 |
description: A dictionary with OSPF neighbors. |
| 175 |
returned: O(gather_subset) contains V(routing) |
| 176 |
type: dict |
| 177 |
""" |
| 178 |
|
| 179 |
from ansible.module_utils.basic import AnsibleModule |
| 180 |
from ansible.module_utils.common.text.converters import to_native |
| 181 |
|
| 182 |
from ansible_collections.community.routeros.plugins.module_utils.api import ( |
| 183 |
api_argument_spec, |
| 184 |
check_has_library, |
| 185 |
create_api, |
| 186 |
) |
| 187 |
|
| 188 |
try: |
| 189 |
from librouteros.exceptions import LibRouterosError |
| 190 |
except Exception: |
| 191 |
# Handled in api module_utils |
| 192 |
pass |
| 193 |
|
| 194 |
|
| 195 |
class FactsBase(object): |
| 196 |
|
| 197 |
COMMANDS = [] |
| 198 |
|
| 199 |
def __init__(self, module, api): |
| 200 |
self.module = module |
| 201 |
self.api = api |
| 202 |
self.facts = {} |
| 203 |
self.responses = None |
| 204 |
|
| 205 |
def populate(self): |
| 206 |
self.responses = [] |
| 207 |
for path in self.COMMANDS: |
| 208 |
self.responses.append(self.query_path(path)) |
| 209 |
|
| 210 |
def query_path(self, path): |
| 211 |
api_path = self.api.path() |
| 212 |
for part in path: |
| 213 |
api_path = api_path.join(part) |
| 214 |
try: |
| 215 |
return list(api_path) |
| 216 |
except LibRouterosError as e: |
| 217 |
self.module.warn('Error while querying path {path}: {error}'.format( |
| 218 |
path=' '.join(path), |
| 219 |
error=to_native(e), |
| 220 |
)) |
| 221 |
return [] |
| 222 |
|
| 223 |
|
| 224 |
class Default(FactsBase): |
| 225 |
|
| 226 |
COMMANDS = [ |
| 227 |
['system', 'identity'], |
| 228 |
['system', 'resource'], |
| 229 |
['system', 'routerboard'], |
| 230 |
] |
| 231 |
|
| 232 |
def populate(self): |
| 233 |
super(Default, self).populate() |
| 234 |
data = self.responses[0] |
| 235 |
if data: |
| 236 |
self.facts['hostname'] = data[0].get('name') |
| 237 |
data = self.responses[1] |
| 238 |
if data: |
| 239 |
self.facts['version'] = data[0].get('version') |
| 240 |
self.facts['arch'] = data[0].get('architecture-name') |
| 241 |
self.facts['uptime'] = data[0].get('uptime') |
| 242 |
self.facts['cpu_load'] = data[0].get('cpu-load') |
| 243 |
data = self.responses[2] |
| 244 |
if data: |
| 245 |
self.facts['model'] = data[0].get('model') |
| 246 |
self.facts['serialnum'] = data[0].get('serial-number') |
| 247 |
|
| 248 |
|
| 249 |
class Hardware(FactsBase): |
| 250 |
|
| 251 |
COMMANDS = [ |
| 252 |
['system', 'resource'], |
| 253 |
] |
| 254 |
|
| 255 |
def populate(self): |
| 256 |
super(Hardware, self).populate() |
| 257 |
data = self.responses[0] |
| 258 |
if data: |
| 259 |
self.parse_filesystem_info(data[0]) |
| 260 |
self.parse_memory_info(data[0]) |
| 261 |
|
| 262 |
def parse_filesystem_info(self, data): |
| 263 |
self.facts['spacefree_mb'] = self.to_megabytes(data.get('free-hdd-space')) |
| 264 |
self.facts['spacetotal_mb'] = self.to_megabytes(data.get('total-hdd-space')) |
| 265 |
|
| 266 |
def parse_memory_info(self, data): |
| 267 |
self.facts['memfree_mb'] = self.to_megabytes(data.get('free-memory')) |
| 268 |
self.facts['memtotal_mb'] = self.to_megabytes(data.get('total-memory')) |
| 269 |
|
| 270 |
def to_megabytes(self, value): |
| 271 |
if value is None: |
| 272 |
return None |
| 273 |
return float(value) / 1024 / 1024 |
| 274 |
|
| 275 |
|
| 276 |
class Interfaces(FactsBase): |
| 277 |
|
| 278 |
COMMANDS = [ |
| 279 |
['interface'], |
| 280 |
['ip', 'address'], |
| 281 |
['ipv6', 'address'], |
| 282 |
['ip', 'neighbor'], |
| 283 |
] |
| 284 |
|
| 285 |
def populate(self): |
| 286 |
super(Interfaces, self).populate() |
| 287 |
|
| 288 |
self.facts['interfaces'] = {} |
| 289 |
self.facts['all_ipv4_addresses'] = [] |
| 290 |
self.facts['all_ipv6_addresses'] = [] |
| 291 |
self.facts['neighbors'] = [] |
| 292 |
|
| 293 |
data = self.responses[0] |
| 294 |
if data: |
| 295 |
interfaces = self.parse_interfaces(data) |
| 296 |
self.populate_interfaces(interfaces) |
| 297 |
|
| 298 |
data = self.responses[1] |
| 299 |
if data: |
| 300 |
data = self.parse_detail(data) |
| 301 |
self.populate_addresses(data, 'ipv4') |
| 302 |
|
| 303 |
data = self.responses[2] |
| 304 |
if data: |
| 305 |
data = self.parse_detail(data) |
| 306 |
self.populate_addresses(data, 'ipv6') |
| 307 |
|
| 308 |
data = self.responses[3] |
| 309 |
if data: |
| 310 |
self.facts['neighbors'] = list(self.parse_detail(data)) |
| 311 |
|
| 312 |
def populate_interfaces(self, data): |
| 313 |
for key, value in data.items(): |
| 314 |
self.facts['interfaces'][key] = value |
| 315 |
|
| 316 |
def populate_addresses(self, data, family): |
| 317 |
for value in data: |
| 318 |
key = value['interface'] |
| 319 |
iface = self.facts['interfaces'].setdefault(key, ( |
| 320 |
{"type": "ansible:unknown"} if key.startswith('*') else |
| 321 |
{"type": "ansible:mismatch"})) |
| 322 |
iface_addrs = iface.setdefault(family, []) |
| 323 |
addr, subnet = value['address'].split('/') |
| 324 |
subnet = subnet.strip() |
| 325 |
# Try to convert subnet to an integer |
| 326 |
try: |
| 327 |
subnet = int(subnet) |
| 328 |
except Exception: |
| 329 |
pass |
| 330 |
ip = dict(address=addr.strip(), subnet=subnet) |
| 331 |
self.add_ip_address(addr.strip(), family) |
| 332 |
iface_addrs.append(ip) |
| 333 |
|
| 334 |
def add_ip_address(self, address, family): |
| 335 |
if family == 'ipv4': |
| 336 |
self.facts['all_ipv4_addresses'].append(address) |
| 337 |
else: |
| 338 |
self.facts['all_ipv6_addresses'].append(address) |
| 339 |
|
| 340 |
def parse_interfaces(self, data): |
| 341 |
facts = {} |
| 342 |
for entry in data: |
| 343 |
if 'name' not in entry: |
| 344 |
continue |
| 345 |
entry.pop('.id', None) |
| 346 |
facts[entry['name']] = entry |
| 347 |
return facts |
| 348 |
|
| 349 |
def parse_detail(self, data): |
| 350 |
for entry in data: |
| 351 |
if 'interface' not in entry: |
| 352 |
continue |
| 353 |
entry.pop('.id', None) |
| 354 |
yield entry |
| 355 |
|
| 356 |
|
| 357 |
class Routing(FactsBase): |
| 358 |
|
| 359 |
COMMANDS = [ |
| 360 |
['routing', 'bgp', 'peer'], |
| 361 |
['routing', 'bgp', 'vpnv4-route'], |
| 362 |
['routing', 'bgp', 'instance'], |
| 363 |
['ip', 'route'], |
| 364 |
['routing', 'ospf', 'instance'], |
| 365 |
['routing', 'ospf', 'neighbor'], |
| 366 |
] |
| 367 |
|
| 368 |
def populate(self): |
| 369 |
super(Routing, self).populate() |
| 370 |
self.facts['bgp_peer'] = {} |
| 371 |
self.facts['bgp_vpnv4_route'] = {} |
| 372 |
self.facts['bgp_instance'] = {} |
| 373 |
self.facts['route'] = {} |
| 374 |
self.facts['ospf_instance'] = {} |
| 375 |
self.facts['ospf_neighbor'] = {} |
| 376 |
data = self.responses[0] |
| 377 |
if data: |
| 378 |
peer = self.parse(data, 'name') |
| 379 |
self.populate_result('bgp_peer', peer) |
| 380 |
data = self.responses[1] |
| 381 |
if data: |
| 382 |
vpnv4 = self.parse(data, 'interface') |
| 383 |
self.populate_result('bgp_vpnv4_route', vpnv4) |
| 384 |
data = self.responses[2] |
| 385 |
if data: |
| 386 |
instance = self.parse(data, 'name') |
| 387 |
self.populate_result('bgp_instance', instance) |
| 388 |
data = self.responses[3] |
| 389 |
if data: |
| 390 |
route = self.parse(data, 'routing-mark', fallback='main') |
| 391 |
self.populate_result('route', route) |
| 392 |
data = self.responses[4] |
| 393 |
if data: |
| 394 |
instance = self.parse(data, 'name') |
| 395 |
self.populate_result('ospf_instance', instance) |
| 396 |
data = self.responses[5] |
| 397 |
if data: |
| 398 |
instance = self.parse(data, 'instance') |
| 399 |
self.populate_result('ospf_neighbor', instance) |
| 400 |
|
| 401 |
def parse(self, data, key, fallback=None): |
| 402 |
facts = {} |
| 403 |
for line in data: |
| 404 |
name = line.get(key) or fallback |
| 405 |
line.pop('.id', None) |
| 406 |
facts[name] = line |
| 407 |
return facts |
| 408 |
|
| 409 |
def populate_result(self, name, data): |
| 410 |
for key, value in data.items(): |
| 411 |
self.facts[name][key] = value |
| 412 |
|
| 413 |
|
| 414 |
FACT_SUBSETS = dict( |
| 415 |
default=Default, |
| 416 |
hardware=Hardware, |
| 417 |
interfaces=Interfaces, |
| 418 |
routing=Routing, |
| 419 |
) |
| 420 |
|
| 421 |
VALID_SUBSETS = frozenset(FACT_SUBSETS.keys()) |
| 422 |
|
| 423 |
|
| 424 |
def main(): |
| 425 |
argument_spec = dict( |
| 426 |
gather_subset=dict( |
| 427 |
default=['all'], |
| 428 |
type='list', |
| 429 |
elements='str', |
| 430 |
) |
| 431 |
) |
| 432 |
argument_spec.update(api_argument_spec()) |
| 433 |
|
| 434 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 435 |
check_has_library(module) |
| 436 |
api = create_api(module) |
| 437 |
|
| 438 |
gather_subset = module.params['gather_subset'] |
| 439 |
|
| 440 |
runable_subsets = set() |
| 441 |
exclude_subsets = set() |
| 442 |
|
| 443 |
for subset in gather_subset: |
| 444 |
if subset == 'all': |
| 445 |
runable_subsets.update(VALID_SUBSETS) |
| 446 |
continue |
| 447 |
|
| 448 |
if subset.startswith('!'): |
| 449 |
subset = subset[1:] |
| 450 |
if subset == 'all': |
| 451 |
exclude_subsets.update(VALID_SUBSETS) |
| 452 |
continue |
| 453 |
exclude = True |
| 454 |
else: |
| 455 |
exclude = False |
| 456 |
|
| 457 |
if subset not in VALID_SUBSETS: |
| 458 |
module.fail_json(msg='Bad subset: %s' % subset) |
| 459 |
|
| 460 |
if exclude: |
| 461 |
exclude_subsets.add(subset) |
| 462 |
else: |
| 463 |
runable_subsets.add(subset) |
| 464 |
|
| 465 |
if not runable_subsets: |
| 466 |
runable_subsets.update(VALID_SUBSETS) |
| 467 |
|
| 468 |
runable_subsets.difference_update(exclude_subsets) |
| 469 |
runable_subsets.add('default') |
| 470 |
|
| 471 |
facts = {} |
| 472 |
facts['gather_subset'] = sorted(runable_subsets) |
| 473 |
|
| 474 |
instances = [] |
| 475 |
for key in runable_subsets: |
| 476 |
instances.append(FACT_SUBSETS[key](module, api)) |
| 477 |
|
| 478 |
for inst in instances: |
| 479 |
inst.populate() |
| 480 |
facts.update(inst.facts) |
| 481 |
|
| 482 |
ansible_facts = {} |
| 483 |
for key, value in facts.items(): |
| 484 |
key = 'ansible_net_%s' % key |
| 485 |
ansible_facts[key] = value |
| 486 |
|
| 487 |
module.exit_json(ansible_facts=ansible_facts) |
| 488 |
|
| 489 |
|
| 490 |
if __name__ == '__main__': |
| 491 |
main() |