mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
Refactor iosxr_facts to use network_cli plugin (#20857)
This commit is contained in:
parent
bfffd1952f
commit
375846d764
10 changed files with 270 additions and 50 deletions
|
@ -23,7 +23,7 @@ DOCUMENTATION = """
|
|||
---
|
||||
module: iosxr_facts
|
||||
version_added: "2.2"
|
||||
author: "Peter Sprygada (@privateip)"
|
||||
author: "Ricardo Carrillo Cruz (@rcarrillocruz)"
|
||||
short_description: Collect facts from remote devices running IOS-XR
|
||||
description:
|
||||
- Collects a base set of device facts from a remote device that
|
||||
|
@ -121,43 +121,31 @@ ansible_net_neighbors:
|
|||
"""
|
||||
import re
|
||||
|
||||
import ansible.module_utils.iosxr
|
||||
from ansible.module_utils.netcli import CommandRunner, AddCommandError
|
||||
from ansible.module_utils.network import NetworkModule
|
||||
from ansible.module_utils.iosxr import run_commands
|
||||
from ansible.module_utils.local import LocalAnsibleModule
|
||||
from ansible.module_utils.six import iteritems
|
||||
from ansible.module_utils.six.moves import zip
|
||||
|
||||
|
||||
def add_command(runner, command):
|
||||
try:
|
||||
runner.add_command(command)
|
||||
except AddCommandError:
|
||||
# AddCommandError is raised for any issue adding a command to
|
||||
# the runner. Silently ignore the exception in this case
|
||||
pass
|
||||
|
||||
class FactsBase(object):
|
||||
|
||||
def __init__(self, runner):
|
||||
self.runner = runner
|
||||
def __init__(self):
|
||||
self.facts = dict()
|
||||
|
||||
self.commands()
|
||||
|
||||
def commands(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class Default(FactsBase):
|
||||
|
||||
def commands(self):
|
||||
add_command(self.runner, 'show version brief')
|
||||
return(['show version brief'])
|
||||
|
||||
def populate(self):
|
||||
data = self.runner.get_command('show version brief')
|
||||
|
||||
self.facts['version'] = self.parse_version(data)
|
||||
self.facts['image'] = self.parse_image(data)
|
||||
self.facts['hostname'] = self.parse_hostname(data)
|
||||
def populate(self, results):
|
||||
self.facts['version'] = self.parse_version(results['show version brief'])
|
||||
self.facts['image'] = self.parse_image(results['show version brief'])
|
||||
self.facts['hostname'] = self.parse_hostname(results['show version brief'])
|
||||
|
||||
def parse_version(self, data):
|
||||
match = re.search(r'Version (\S+)$', data, re.M)
|
||||
|
@ -178,15 +166,14 @@ class Default(FactsBase):
|
|||
class Hardware(FactsBase):
|
||||
|
||||
def commands(self):
|
||||
add_command(self.runner, 'dir /all | include Directory')
|
||||
add_command(self.runner, 'show memory summary')
|
||||
return(['dir /all | include Directory', 'show memory summary'])
|
||||
|
||||
def populate(self):
|
||||
data = self.runner.get_command('dir /all | include Directory')
|
||||
self.facts['filesystems'] = self.parse_filesystems(data)
|
||||
def populate(self, results):
|
||||
self.facts['filesystems'] = self.parse_filesystems(
|
||||
results['dir /all | include Directory'])
|
||||
|
||||
data = self.runner.get_command('show memory summary')
|
||||
match = re.search(r'Physical Memory (\d+)M total \((\d+)', data)
|
||||
match = re.search(r'Physical Memory (\d+)M total \((\d+)',
|
||||
results['show memory summary'])
|
||||
if match:
|
||||
self.facts['memtotal_mb'] = int(match[0])
|
||||
self.facts['memfree_mb'] = int(match[1])
|
||||
|
@ -198,35 +185,32 @@ class Hardware(FactsBase):
|
|||
class Config(FactsBase):
|
||||
|
||||
def commands(self):
|
||||
add_command(self.runner, 'show running-config')
|
||||
return(['show running-config'])
|
||||
|
||||
def populate(self):
|
||||
self.facts['config'] = self.runner.get_command('show running-config')
|
||||
def populate(self, results):
|
||||
self.facts['config'] = results['show running-config']
|
||||
|
||||
|
||||
class Interfaces(FactsBase):
|
||||
|
||||
def commands(self):
|
||||
add_command(self.runner, 'show interfaces')
|
||||
add_command(self.runner, 'show ipv6 interface')
|
||||
add_command(self.runner, 'show lldp')
|
||||
add_command(self.runner, 'show lldp neighbors detail')
|
||||
return(['show interfaces', 'show ipv6 interface',
|
||||
'show lldp', 'show lldp neighbors detail'])
|
||||
|
||||
def populate(self):
|
||||
def populate(self, results):
|
||||
self.facts['all_ipv4_addresses'] = list()
|
||||
self.facts['all_ipv6_addresses'] = list()
|
||||
|
||||
data = self.runner.get_command('show interfaces')
|
||||
interfaces = self.parse_interfaces(data)
|
||||
interfaces = self.parse_interfaces(results['show interfaces'])
|
||||
self.facts['interfaces'] = self.populate_interfaces(interfaces)
|
||||
|
||||
data = self.runner.get_command('show ipv6 interface')
|
||||
data = results['show ipv6 interface']
|
||||
if len(data) > 0:
|
||||
data = self.parse_interfaces(data)
|
||||
self.populate_ipv6_interfaces(data)
|
||||
|
||||
if 'LLDP is not enabled' not in self.runner.get_command('show lldp'):
|
||||
neighbors = self.runner.get_command('show lldp neighbors detail')
|
||||
if 'LLDP is not enabled' not in results['show lldp']:
|
||||
neighbors = results['show lldp neighbors detail']
|
||||
self.facts['neighbors'] = self.parse_neighbors(neighbors)
|
||||
|
||||
def populate_interfaces(self, interfaces):
|
||||
|
@ -369,12 +353,13 @@ FACT_SUBSETS = dict(
|
|||
|
||||
VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())
|
||||
|
||||
|
||||
def main():
|
||||
spec = dict(
|
||||
gather_subset=dict(default=['!config'], type='list')
|
||||
)
|
||||
|
||||
module = NetworkModule(argument_spec=spec, supports_check_mode=True)
|
||||
module = LocalAnsibleModule(argument_spec=spec, supports_check_mode=True)
|
||||
|
||||
gather_subset = module.params['gather_subset']
|
||||
|
||||
|
@ -412,20 +397,19 @@ def main():
|
|||
facts = dict()
|
||||
facts['gather_subset'] = list(runable_subsets)
|
||||
|
||||
runner = CommandRunner(module)
|
||||
|
||||
instances = list()
|
||||
for key in runable_subsets:
|
||||
instances.append(FACT_SUBSETS[key](runner))
|
||||
|
||||
runner.run()
|
||||
instances.append(FACT_SUBSETS[key]())
|
||||
|
||||
try:
|
||||
for inst in instances:
|
||||
inst.populate()
|
||||
commands = inst.commands()
|
||||
responses = run_commands(module, commands)
|
||||
results = dict(zip(commands, responses))
|
||||
inst.populate(results)
|
||||
facts.update(inst.facts)
|
||||
except Exception:
|
||||
module.exit_json(out=module.from_json(runner.items))
|
||||
module.exit_json(out=module.from_json(results))
|
||||
|
||||
ansible_facts = dict()
|
||||
for key, value in iteritems(facts):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue