| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: facts |
| 12 |
author: "Egor Zaitsev (@heuels)" |
| 13 |
short_description: Collect facts from remote devices running MikroTik RouterOS |
| 14 |
description: |
| 15 |
- Collects a base set of device facts from a remote device that is running RouterOS. This module prepends all of the base |
| 16 |
network fact keys with C(ansible_net_<fact>). The facts module will always collect a base set of facts from the device |
| 17 |
and can enable or disable collection of additional facts. |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.routeros.attributes |
| 20 |
- community.routeros.attributes.facts |
| 21 |
- community.routeros.attributes.facts_module |
| 22 |
- community.routeros.attributes.idempotent_not_modify_state |
| 23 |
attributes: |
| 24 |
platform: |
| 25 |
support: full |
| 26 |
platforms: RouterOS |
| 27 |
options: |
| 28 |
gather_subset: |
| 29 |
description: |
| 30 |
- When supplied, this argument will restrict the facts collected to a given subset. Possible values for this argument |
| 31 |
include V(all), V(hardware), V(config), V(interfaces), and V(routing). |
| 32 |
- Can specify a list of values to include a larger subset. Values can also be used with an initial V(!) to specify that |
| 33 |
a specific subset should not be collected. |
| 34 |
required: false |
| 35 |
default: |
| 36 |
- '!config' |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
seealso: |
| 40 |
- ref: ansible_collections.community.routeros.docsite.ssh-guide |
| 41 |
description: How to connect to RouterOS devices with SSH. |
| 42 |
""" |
| 43 |
|
| 44 |
EXAMPLES = r""" |
| 45 |
--- |
| 46 |
- name: Collect all facts from the device |
| 47 |
community.routeros.facts: |
| 48 |
gather_subset: all |
| 49 |
|
| 50 |
- name: Collect only the config and default facts |
| 51 |
community.routeros.facts: |
| 52 |
gather_subset: |
| 53 |
- config |
| 54 |
|
| 55 |
- name: Do not collect hardware facts |
| 56 |
community.routeros.facts: |
| 57 |
gather_subset: |
| 58 |
- "!hardware" |
| 59 |
""" |
| 60 |
|
| 61 |
RETURN = r""" |
| 62 |
ansible_facts: |
| 63 |
description: "Dictionary of IP geolocation facts for a host's IP address." |
| 64 |
returned: always |
| 65 |
type: dict |
| 66 |
contains: |
| 67 |
ansible_net_gather_subset: |
| 68 |
description: The list of fact subsets collected from the device. |
| 69 |
returned: always |
| 70 |
type: list |
| 71 |
|
| 72 |
# default |
| 73 |
ansible_net_model: |
| 74 |
description: The model name returned from the device. |
| 75 |
returned: O(gather_subset) contains V(default) |
| 76 |
type: str |
| 77 |
ansible_net_serialnum: |
| 78 |
description: The serial number of the remote device. |
| 79 |
returned: O(gather_subset) contains V(default) |
| 80 |
type: str |
| 81 |
ansible_net_version: |
| 82 |
description: The operating system version running on the remote device. |
| 83 |
returned: O(gather_subset) contains V(default) |
| 84 |
type: str |
| 85 |
ansible_net_hostname: |
| 86 |
description: The configured hostname of the device. |
| 87 |
returned: O(gather_subset) contains V(default) |
| 88 |
type: str |
| 89 |
ansible_net_arch: |
| 90 |
description: The CPU architecture of the device. |
| 91 |
returned: O(gather_subset) contains V(default) |
| 92 |
type: str |
| 93 |
ansible_net_uptime: |
| 94 |
description: The uptime of the device. |
| 95 |
returned: O(gather_subset) contains V(default) |
| 96 |
type: str |
| 97 |
ansible_net_cpu_load: |
| 98 |
description: Current CPU load. |
| 99 |
returned: O(gather_subset) contains V(default) |
| 100 |
type: str |
| 101 |
|
| 102 |
# hardware |
| 103 |
ansible_net_spacefree_mb: |
| 104 |
description: The available disk space on the remote device in MiB. |
| 105 |
returned: O(gather_subset) contains V(hardware) |
| 106 |
type: dict |
| 107 |
ansible_net_spacetotal_mb: |
| 108 |
description: The total disk space on the remote device in MiB. |
| 109 |
returned: O(gather_subset) contains V(hardware) |
| 110 |
type: dict |
| 111 |
ansible_net_memfree_mb: |
| 112 |
description: The available free memory on the remote device in MiB. |
| 113 |
returned: O(gather_subset) contains V(hardware) |
| 114 |
type: int |
| 115 |
ansible_net_memtotal_mb: |
| 116 |
description: The total memory on the remote device in MiB. |
| 117 |
returned: O(gather_subset) contains V(hardware) |
| 118 |
type: int |
| 119 |
|
| 120 |
# config |
| 121 |
ansible_net_config: |
| 122 |
description: The current active config from the device. |
| 123 |
returned: O(gather_subset) contains V(config) |
| 124 |
type: str |
| 125 |
|
| 126 |
ansible_net_config_nonverbose: |
| 127 |
description: |
| 128 |
- The current active config from the device in minimal form. |
| 129 |
- This value is idempotent in the sense that if the facts module is run twice and the device's config was not changed |
| 130 |
between the runs, the value is identical. This is achieved by running C(/export) and stripping the timestamp from |
| 131 |
the comment in the first line. |
| 132 |
returned: O(gather_subset) contains V(config) |
| 133 |
type: str |
| 134 |
version_added: 1.2.0 |
| 135 |
|
| 136 |
# interfaces |
| 137 |
ansible_net_all_ipv4_addresses: |
| 138 |
description: All IPv4 addresses configured on the device. |
| 139 |
returned: O(gather_subset) contains V(interfaces) |
| 140 |
type: list |
| 141 |
ansible_net_all_ipv6_addresses: |
| 142 |
description: All IPv6 addresses configured on the device. |
| 143 |
returned: O(gather_subset) contains V(interfaces) |
| 144 |
type: list |
| 145 |
ansible_net_interfaces: |
| 146 |
description: A hash of all interfaces running on the system. |
| 147 |
returned: O(gather_subset) contains V(interfaces) |
| 148 |
type: dict |
| 149 |
ansible_net_neighbors: |
| 150 |
description: The list of neighbors from the remote device. |
| 151 |
returned: O(gather_subset) contains V(interfaces) |
| 152 |
type: dict |
| 153 |
|
| 154 |
# routing |
| 155 |
ansible_net_bgp_peer: |
| 156 |
description: A dictionary with BGP peer information. |
| 157 |
returned: O(gather_subset) contains V(routing) |
| 158 |
type: dict |
| 159 |
ansible_net_bgp_vpnv4_route: |
| 160 |
description: A dictionary with BGP vpnv4 route information. |
| 161 |
returned: O(gather_subset) contains V(routing) |
| 162 |
type: dict |
| 163 |
ansible_net_bgp_instance: |
| 164 |
description: A dictionary with BGP instance information. |
| 165 |
returned: O(gather_subset) contains V(routing) |
| 166 |
type: dict |
| 167 |
ansible_net_route: |
| 168 |
description: A dictionary for routes in all routing tables. |
| 169 |
returned: O(gather_subset) contains V(routing) |
| 170 |
type: dict |
| 171 |
ansible_net_ospf_instance: |
| 172 |
description: A dictionary with OSPF instances. |
| 173 |
returned: O(gather_subset) contains V(routing) |
| 174 |
type: dict |
| 175 |
ansible_net_ospf_neighbor: |
| 176 |
description: A dictionary with OSPF neighbors. |
| 177 |
returned: O(gather_subset) contains V(routing) |
| 178 |
type: dict |
| 179 |
""" |
| 180 |
import re |
| 181 |
|
| 182 |
from ansible_collections.community.routeros.plugins.module_utils.routeros import run_commands |
| 183 |
from ansible_collections.community.routeros.plugins.module_utils.routeros import routeros_argument_spec |
| 184 |
from ansible.module_utils.basic import AnsibleModule |
| 185 |
|
| 186 |
|
| 187 |
class FactsBase(object): |
| 188 |
|
| 189 |
COMMANDS = list() |
| 190 |
|
| 191 |
def __init__(self, module): |
| 192 |
self.module = module |
| 193 |
self.facts = dict() |
| 194 |
self.responses = None |
| 195 |
|
| 196 |
def populate(self): |
| 197 |
self.responses = run_commands(self.module, commands=self.COMMANDS, check_rc=False) |
| 198 |
|
| 199 |
def run(self, cmd): |
| 200 |
return run_commands(self.module, commands=cmd, check_rc=False) |
| 201 |
|
| 202 |
|
| 203 |
class Default(FactsBase): |
| 204 |
|
| 205 |
COMMANDS = [ |
| 206 |
'/system identity print without-paging', |
| 207 |
'/system resource print without-paging', |
| 208 |
'/system routerboard print without-paging' |
| 209 |
] |
| 210 |
|
| 211 |
def populate(self): |
| 212 |
super(Default, self).populate() |
| 213 |
data = self.responses[0] |
| 214 |
if data: |
| 215 |
self.facts['hostname'] = self.parse_hostname(data) |
| 216 |
data = self.responses[1] |
| 217 |
if data: |
| 218 |
self.facts['version'] = self.parse_version(data) |
| 219 |
self.facts['arch'] = self.parse_arch(data) |
| 220 |
self.facts['uptime'] = self.parse_uptime(data) |
| 221 |
self.facts['cpu_load'] = self.parse_cpu_load(data) |
| 222 |
data = self.responses[2] |
| 223 |
if data: |
| 224 |
self.facts['model'] = self.parse_model(data) |
| 225 |
self.facts['serialnum'] = self.parse_serialnum(data) |
| 226 |
|
| 227 |
def parse_hostname(self, data): |
| 228 |
match = re.search(r'name:\s(.*)\s*$', data, re.M) |
| 229 |
if match: |
| 230 |
return match.group(1) |
| 231 |
|
| 232 |
def parse_version(self, data): |
| 233 |
match = re.search(r'version:\s(.*)\s*$', data, re.M) |
| 234 |
if match: |
| 235 |
return match.group(1) |
| 236 |
|
| 237 |
def parse_model(self, data): |
| 238 |
match = re.search(r'model:\s(.*)\s*$', data, re.M) |
| 239 |
if match: |
| 240 |
return match.group(1) |
| 241 |
|
| 242 |
def parse_arch(self, data): |
| 243 |
match = re.search(r'architecture-name:\s(.*)\s*$', data, re.M) |
| 244 |
if match: |
| 245 |
return match.group(1) |
| 246 |
|
| 247 |
def parse_uptime(self, data): |
| 248 |
match = re.search(r'uptime:\s(.*)\s*$', data, re.M) |
| 249 |
if match: |
| 250 |
return match.group(1) |
| 251 |
|
| 252 |
def parse_cpu_load(self, data): |
| 253 |
match = re.search(r'cpu-load:\s(.*)\s*$', data, re.M) |
| 254 |
if match: |
| 255 |
return match.group(1) |
| 256 |
|
| 257 |
def parse_serialnum(self, data): |
| 258 |
match = re.search(r'serial-number:\s(.*)\s*$', data, re.M) |
| 259 |
if match: |
| 260 |
return match.group(1) |
| 261 |
|
| 262 |
|
| 263 |
class Hardware(FactsBase): |
| 264 |
|
| 265 |
COMMANDS = [ |
| 266 |
'/system resource print without-paging' |
| 267 |
] |
| 268 |
|
| 269 |
def populate(self): |
| 270 |
super(Hardware, self).populate() |
| 271 |
data = self.responses[0] |
| 272 |
if data: |
| 273 |
self.parse_filesystem_info(data) |
| 274 |
self.parse_memory_info(data) |
| 275 |
|
| 276 |
def parse_filesystem_info(self, data): |
| 277 |
match = re.search(r'free-hdd-space:\s(.*)([KMG]iB)', data, re.M) |
| 278 |
if match: |
| 279 |
self.facts['spacefree_mb'] = self.to_megabytes(match) |
| 280 |
match = re.search(r'total-hdd-space:\s(.*)([KMG]iB)', data, re.M) |
| 281 |
if match: |
| 282 |
self.facts['spacetotal_mb'] = self.to_megabytes(match) |
| 283 |
|
| 284 |
def parse_memory_info(self, data): |
| 285 |
match = re.search(r'free-memory:\s(\d+\.?\d*)([KMG]iB)', data, re.M) |
| 286 |
if match: |
| 287 |
self.facts['memfree_mb'] = self.to_megabytes(match) |
| 288 |
match = re.search(r'total-memory:\s(\d+\.?\d*)([KMG]iB)', data, re.M) |
| 289 |
if match: |
| 290 |
self.facts['memtotal_mb'] = self.to_megabytes(match) |
| 291 |
|
| 292 |
def to_megabytes(self, data): |
| 293 |
if data.group(2) == 'KiB': |
| 294 |
return float(data.group(1)) / 1024 |
| 295 |
elif data.group(2) == 'MiB': |
| 296 |
return float(data.group(1)) |
| 297 |
elif data.group(2) == 'GiB': |
| 298 |
return float(data.group(1)) * 1024 |
| 299 |
else: |
| 300 |
return None |
| 301 |
|
| 302 |
|
| 303 |
class Config(FactsBase): |
| 304 |
|
| 305 |
COMMANDS = [ |
| 306 |
'/export verbose', |
| 307 |
'/export', |
| 308 |
] |
| 309 |
|
| 310 |
RM_DATE_RE = re.compile(r'^# [a-z0-9/-][a-z0-9/-]* [0-9:]* by RouterOS') |
| 311 |
|
| 312 |
def populate(self): |
| 313 |
super(Config, self).populate() |
| 314 |
|
| 315 |
data = self.responses[0] |
| 316 |
if data: |
| 317 |
self.facts['config'] = data |
| 318 |
|
| 319 |
data = self.responses[1] |
| 320 |
if data: |
| 321 |
# remove datetime |
| 322 |
data = re.sub(self.RM_DATE_RE, r'# RouterOS', data) |
| 323 |
self.facts['config_nonverbose'] = data |
| 324 |
|
| 325 |
|
| 326 |
class Interfaces(FactsBase): |
| 327 |
|
| 328 |
COMMANDS = [ |
| 329 |
'/interface print detail without-paging', |
| 330 |
'/ip address print detail without-paging', |
| 331 |
'/ipv6 address print detail without-paging', |
| 332 |
'/ip neighbor print detail without-paging' |
| 333 |
] |
| 334 |
|
| 335 |
DETAIL_RE = re.compile(r'([\w\d\-]+)=\"?(\w{3}/\d{2}/\d{4}\s\d{2}:\d{2}:\d{2}|[\w\d\-\.:/]+)') |
| 336 |
WRAPPED_LINE_RE = re.compile(r'^\s+(?!\d)') |
| 337 |
|
| 338 |
def populate(self): |
| 339 |
super(Interfaces, self).populate() |
| 340 |
|
| 341 |
self.facts['interfaces'] = dict() |
| 342 |
self.facts['all_ipv4_addresses'] = list() |
| 343 |
self.facts['all_ipv6_addresses'] = list() |
| 344 |
self.facts['neighbors'] = list() |
| 345 |
|
| 346 |
data = self.responses[0] |
| 347 |
if data: |
| 348 |
interfaces = self.parse_interfaces(data) |
| 349 |
self.populate_interfaces(interfaces) |
| 350 |
|
| 351 |
data = self.responses[1] |
| 352 |
if data: |
| 353 |
data = self.parse_detail(data) |
| 354 |
self.populate_addresses(data, 'ipv4') |
| 355 |
|
| 356 |
data = self.responses[2] |
| 357 |
if data: |
| 358 |
data = self.parse_detail(data) |
| 359 |
self.populate_addresses(data, 'ipv6') |
| 360 |
|
| 361 |
data = self.responses[3] |
| 362 |
if data: |
| 363 |
self.facts['neighbors'] = list(self.parse_detail(data)) |
| 364 |
|
| 365 |
def populate_interfaces(self, data): |
| 366 |
for key, value in data.items(): |
| 367 |
self.facts['interfaces'][key] = value |
| 368 |
|
| 369 |
def populate_addresses(self, data, family): |
| 370 |
for value in data: |
| 371 |
key = value['interface'] |
| 372 |
if family not in self.facts['interfaces'][key]: |
| 373 |
self.facts['interfaces'][key][family] = list() |
| 374 |
addr, subnet = value['address'].split("/") |
| 375 |
ip = dict(address=addr.strip(), subnet=subnet.strip()) |
| 376 |
self.add_ip_address(addr.strip(), family) |
| 377 |
self.facts['interfaces'][key][family].append(ip) |
| 378 |
|
| 379 |
def add_ip_address(self, address, family): |
| 380 |
if family == 'ipv4': |
| 381 |
self.facts['all_ipv4_addresses'].append(address) |
| 382 |
else: |
| 383 |
self.facts['all_ipv6_addresses'].append(address) |
| 384 |
|
| 385 |
def preprocess(self, data): |
| 386 |
preprocessed = list() |
| 387 |
for line in data.split('\n'): |
| 388 |
if len(line) == 0 or line[:5] == 'Flags': |
| 389 |
continue |
| 390 |
elif not preprocessed or not re.match(self.WRAPPED_LINE_RE, line): |
| 391 |
preprocessed.append(line) |
| 392 |
else: |
| 393 |
preprocessed[-1] += line |
| 394 |
return preprocessed |
| 395 |
|
| 396 |
def parse_interfaces(self, data): |
| 397 |
facts = dict() |
| 398 |
data = self.preprocess(data) |
| 399 |
for line in data: |
| 400 |
parsed = dict(re.findall(self.DETAIL_RE, line)) |
| 401 |
if "name" not in parsed: |
| 402 |
continue |
| 403 |
facts[parsed["name"]] = dict(re.findall(self.DETAIL_RE, line)) |
| 404 |
return facts |
| 405 |
|
| 406 |
def parse_detail(self, data): |
| 407 |
data = self.preprocess(data) |
| 408 |
for line in data: |
| 409 |
parsed = dict(re.findall(self.DETAIL_RE, line)) |
| 410 |
if "interface" not in parsed: |
| 411 |
continue |
| 412 |
yield parsed |
| 413 |
|
| 414 |
|
| 415 |
class Routing(FactsBase): |
| 416 |
|
| 417 |
COMMANDS = [ |
| 418 |
'/routing bgp peer print detail without-paging', |
| 419 |
'/routing bgp vpnv4-route print detail without-paging', |
| 420 |
'/routing bgp instance print detail without-paging', |
| 421 |
'/ip route print detail without-paging', |
| 422 |
'/routing ospf instance print detail without-paging', |
| 423 |
'/routing ospf neighbor print detail without-paging' |
| 424 |
] |
| 425 |
|
| 426 |
DETAIL_RE = re.compile(r'([\w\d\-]+)=\"?(\w{3}/\d{2}/\d{4}\s\d{2}:\d{2}:\d{2}|[\w\d\-\.:/]+)') |
| 427 |
WRAPPED_LINE_RE = re.compile(r'^\s+(?!\d)') |
| 428 |
|
| 429 |
def populate(self): |
| 430 |
super(Routing, self).populate() |
| 431 |
self.facts['bgp_peer'] = dict() |
| 432 |
self.facts['bgp_vpnv4_route'] = dict() |
| 433 |
self.facts['bgp_instance'] = dict() |
| 434 |
self.facts['route'] = dict() |
| 435 |
self.facts['ospf_instance'] = dict() |
| 436 |
self.facts['ospf_neighbor'] = dict() |
| 437 |
data = self.responses[0] |
| 438 |
if data: |
| 439 |
peer = self.parse_bgp_peer(data) |
| 440 |
self.populate_bgp_peer(peer) |
| 441 |
data = self.responses[1] |
| 442 |
if data: |
| 443 |
vpnv4 = self.parse_vpnv4_route(data) |
| 444 |
self.populate_vpnv4_route(vpnv4) |
| 445 |
data = self.responses[2] |
| 446 |
if data: |
| 447 |
instance = self.parse_instance(data) |
| 448 |
self.populate_bgp_instance(instance) |
| 449 |
data = self.responses[3] |
| 450 |
if data: |
| 451 |
route = self.parse_route(data) |
| 452 |
self.populate_route(route) |
| 453 |
data = self.responses[4] |
| 454 |
if data: |
| 455 |
instance = self.parse_instance(data) |
| 456 |
self.populate_ospf_instance(instance) |
| 457 |
data = self.responses[5] |
| 458 |
if data: |
| 459 |
instance = self.parse_ospf_neighbor(data) |
| 460 |
self.populate_ospf_neighbor(instance) |
| 461 |
|
| 462 |
def preprocess(self, data): |
| 463 |
preprocessed = list() |
| 464 |
for line in data.split('\n'): |
| 465 |
if len(line) == 0 or line[:5] == 'Flags': |
| 466 |
continue |
| 467 |
elif not preprocessed or not re.match(self.WRAPPED_LINE_RE, line): |
| 468 |
preprocessed.append(line) |
| 469 |
else: |
| 470 |
preprocessed[-1] += line |
| 471 |
return preprocessed |
| 472 |
|
| 473 |
def parse_name(self, data): |
| 474 |
match = re.search(r'name=.(\S+\b)', data, re.M) |
| 475 |
if match: |
| 476 |
return match.group(1) |
| 477 |
|
| 478 |
def parse_interface(self, data): |
| 479 |
match = re.search(r'interface=([\w\d\-]+)', data, re.M) |
| 480 |
if match: |
| 481 |
return match.group(1) |
| 482 |
|
| 483 |
def parse_instance_name(self, data): |
| 484 |
match = re.search(r'instance=([\w\d\-]+)', data, re.M) |
| 485 |
if match: |
| 486 |
return match.group(1) |
| 487 |
|
| 488 |
def parse_routing_mark(self, data): |
| 489 |
match = re.search(r'routing-mark=([\w\d\-]+)', data, re.M) |
| 490 |
if match: |
| 491 |
return match.group(1) |
| 492 |
else: |
| 493 |
match = 'main' |
| 494 |
return match |
| 495 |
|
| 496 |
def parse_bgp_peer(self, data): |
| 497 |
facts = dict() |
| 498 |
data = self.preprocess(data) |
| 499 |
for line in data: |
| 500 |
name = self.parse_name(line) |
| 501 |
facts[name] = dict() |
| 502 |
for (key, value) in re.findall(self.DETAIL_RE, line): |
| 503 |
facts[name][key] = value |
| 504 |
return facts |
| 505 |
|
| 506 |
def parse_instance(self, data): |
| 507 |
facts = dict() |
| 508 |
data = self.preprocess(data) |
| 509 |
for line in data: |
| 510 |
name = self.parse_name(line) |
| 511 |
facts[name] = dict() |
| 512 |
for (key, value) in re.findall(self.DETAIL_RE, line): |
| 513 |
facts[name][key] = value |
| 514 |
return facts |
| 515 |
|
| 516 |
def parse_vpnv4_route(self, data): |
| 517 |
facts = dict() |
| 518 |
data = self.preprocess(data) |
| 519 |
for line in data: |
| 520 |
name = self.parse_interface(line) |
| 521 |
facts[name] = dict() |
| 522 |
for (key, value) in re.findall(self.DETAIL_RE, line): |
| 523 |
facts[name][key] = value |
| 524 |
return facts |
| 525 |
|
| 526 |
def parse_route(self, data): |
| 527 |
facts = dict() |
| 528 |
data = self.preprocess(data) |
| 529 |
for line in data: |
| 530 |
name = self.parse_routing_mark(line) |
| 531 |
facts[name] = dict() |
| 532 |
for (key, value) in re.findall(self.DETAIL_RE, line): |
| 533 |
facts[name][key] = value |
| 534 |
return facts |
| 535 |
|
| 536 |
def parse_ospf_instance(self, data): |
| 537 |
facts = dict() |
| 538 |
data = self.preprocess(data) |
| 539 |
for line in data: |
| 540 |
name = self.parse_name(line) |
| 541 |
facts[name] = dict() |
| 542 |
for (key, value) in re.findall(self.DETAIL_RE, line): |
| 543 |
facts[name][key] = value |
| 544 |
return facts |
| 545 |
|
| 546 |
def parse_ospf_neighbor(self, data): |
| 547 |
facts = dict() |
| 548 |
data = self.preprocess(data) |
| 549 |
for line in data: |
| 550 |
name = self.parse_instance_name(line) |
| 551 |
facts[name] = dict() |
| 552 |
for (key, value) in re.findall(self.DETAIL_RE, line): |
| 553 |
facts[name][key] = value |
| 554 |
return facts |
| 555 |
|
| 556 |
def populate_bgp_peer(self, data): |
| 557 |
for key, value in data.items(): |
| 558 |
self.facts['bgp_peer'][key] = value |
| 559 |
|
| 560 |
def populate_vpnv4_route(self, data): |
| 561 |
for key, value in data.items(): |
| 562 |
self.facts['bgp_vpnv4_route'][key] = value |
| 563 |
|
| 564 |
def populate_bgp_instance(self, data): |
| 565 |
for key, value in data.items(): |
| 566 |
self.facts['bgp_instance'][key] = value |
| 567 |
|
| 568 |
def populate_route(self, data): |
| 569 |
for key, value in data.items(): |
| 570 |
self.facts['route'][key] = value |
| 571 |
|
| 572 |
def populate_ospf_instance(self, data): |
| 573 |
for key, value in data.items(): |
| 574 |
self.facts['ospf_instance'][key] = value |
| 575 |
|
| 576 |
def populate_ospf_neighbor(self, data): |
| 577 |
for key, value in data.items(): |
| 578 |
self.facts['ospf_neighbor'][key] = value |
| 579 |
|
| 580 |
|
| 581 |
FACT_SUBSETS = dict( |
| 582 |
default=Default, |
| 583 |
hardware=Hardware, |
| 584 |
interfaces=Interfaces, |
| 585 |
config=Config, |
| 586 |
routing=Routing, |
| 587 |
) |
| 588 |
|
| 589 |
VALID_SUBSETS = frozenset(FACT_SUBSETS.keys()) |
| 590 |
|
| 591 |
|
| 592 |
def main(): |
| 593 |
"""main entry point for module execution |
| 594 |
""" |
| 595 |
argument_spec = dict( |
| 596 |
gather_subset=dict(default=['!config'], type='list', elements='str') |
| 597 |
) |
| 598 |
|
| 599 |
argument_spec.update(routeros_argument_spec) |
| 600 |
|
| 601 |
module = AnsibleModule(argument_spec=argument_spec, |
| 602 |
supports_check_mode=True) |
| 603 |
|
| 604 |
gather_subset = module.params['gather_subset'] |
| 605 |
|
| 606 |
runable_subsets = set() |
| 607 |
exclude_subsets = set() |
| 608 |
|
| 609 |
for subset in gather_subset: |
| 610 |
if subset == 'all': |
| 611 |
runable_subsets.update(VALID_SUBSETS) |
| 612 |
continue |
| 613 |
|
| 614 |
if subset.startswith('!'): |
| 615 |
subset = subset[1:] |
| 616 |
if subset == 'all': |
| 617 |
exclude_subsets.update(VALID_SUBSETS) |
| 618 |
continue |
| 619 |
exclude = True |
| 620 |
else: |
| 621 |
exclude = False |
| 622 |
|
| 623 |
if subset not in VALID_SUBSETS: |
| 624 |
module.fail_json(msg='Bad subset: %s' % subset) |
| 625 |
|
| 626 |
if exclude: |
| 627 |
exclude_subsets.add(subset) |
| 628 |
else: |
| 629 |
runable_subsets.add(subset) |
| 630 |
|
| 631 |
if not runable_subsets: |
| 632 |
runable_subsets.update(VALID_SUBSETS) |
| 633 |
|
| 634 |
runable_subsets.difference_update(exclude_subsets) |
| 635 |
runable_subsets.add('default') |
| 636 |
|
| 637 |
facts = dict() |
| 638 |
facts['gather_subset'] = list(runable_subsets) |
| 639 |
|
| 640 |
instances = list() |
| 641 |
for key in runable_subsets: |
| 642 |
instances.append(FACT_SUBSETS[key](module)) |
| 643 |
|
| 644 |
for inst in instances: |
| 645 |
inst.populate() |
| 646 |
facts.update(inst.facts) |
| 647 |
|
| 648 |
ansible_facts = dict() |
| 649 |
for key, value in facts.items(): |
| 650 |
key = 'ansible_net_%s' % key |
| 651 |
ansible_facts[key] = value |
| 652 |
|
| 653 |
module.exit_json(ansible_facts=ansible_facts) |
| 654 |
|
| 655 |
|
| 656 |
if __name__ == '__main__': |
| 657 |
main() |