Update facts (#101)

* Update facts.py

* Update facts.py

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Update plugins/modules/facts.py

Co-authored-by: Egor Zaitsev <heuels@gmail.com>

* Create 101_update_facts.yml

* Update plugins/modules/facts.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update changelogs/fragments/101_update_facts.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* add test default function

* add test data

* Update routing_ospf_neighbor_print_detail_without-paging

* add test function "Routing"

* added information of version

* Removed extra spaces

* add one more version, "ansible_net_cpu_load"

* remove many blank lines

* remove 'too many blank lines'

* renamed arhitec to arch

* Update facts.py

* Update test_facts.py

* Update test_facts.py

* one more

* remove one route

* Update test_facts.py

* remove one neighbor

Co-authored-by: Egor Zaitsev <heuels@gmail.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
adeptvin1 2020-08-21 21:04:09 +10:00 committed by Felix Fontein
parent cc273e7ec1
commit f1ae008c4d
9 changed files with 519 additions and 2 deletions

View file

@ -68,6 +68,21 @@ ansible_net_hostname:
description: The configured hostname of the device
returned: always
type: str
ansible_net_arch:
description: The CPU architecture of the device
returned: always
type: str
version_added: 1.2.0
ansible_net_uptime:
description: The uptime of the device
returned: always
type: str
version_added: 1.2.0
ansible_net_cpu_load:
description: Current CPU load
returned: always
type: str
version_added: 1.2.0
# hardware
ansible_net_spacefree_mb:
@ -110,6 +125,38 @@ ansible_net_neighbors:
description: The list of neighbors from the remote device
returned: when interfaces is configured
type: dict
# routing
ansible_net_bgp_peer:
description: The dict bgp peer
returned: peer information
type: dict
version_added: 1.2.0
ansible_net_bgp_vpnv4_route:
description: The dict bgp vpnv4 route
returned: vpnv4 route information
type: dict
version_added: 1.2.0
ansible_net_bgp_instance:
description: The dict bgp instance
returned: bgp instance information
type: dict
version_added: 1.2.0
ansible_net_route:
description: The dict routes in all routing table
returned: routes information in all routing table
type: dict
version_added: 1.2.0
ansible_net_ospf_instance:
description: The dict ospf instance
returned: ospf instance information
type: dict
version_added: 1.2.0
ansible_net_ospf_neighbor:
description: The dict ospf neighbor
returned: ospf neighbor information
type: dict
version_added: 1.2.0
"""
import re
@ -148,11 +195,12 @@ class Default(FactsBase):
data = self.responses[0]
if data:
self.facts['hostname'] = self.parse_hostname(data)
data = self.responses[1]
if data:
self.facts['version'] = self.parse_version(data)
self.facts['arch'] = self.parse_arch(data)
self.facts['uptime'] = self.parse_uptime(data)
self.facts['cpu_load'] = self.parse_cpu_load(data)
data = self.responses[2]
if data:
self.facts['model'] = self.parse_model(data)
@ -173,6 +221,21 @@ class Default(FactsBase):
if match:
return match.group(1)
def parse_arch(self, data):
match = re.search(r'architecture-name:\s(.*)\s*$', data, re.M)
if match:
return match.group(1)
def parse_uptime(self, data):
match = re.search(r'uptime:\s(.*)\s*$', data, re.M)
if match:
return match.group(1)
def parse_cpu_load(self, data):
match = re.search(r'cpu-load:\s(.*)\s*$', data, re.M)
if match:
return match.group(1)
def parse_serialnum(self, data):
match = re.search(r'serial-number:\s(.*)\s*$', data, re.M)
if match:
@ -319,11 +382,178 @@ class Interfaces(FactsBase):
yield parsed
class Routing(FactsBase):
COMMANDS = [
'/routing bgp peer print detail without-paging',
'/routing bgp vpnv4-route print detail without-paging',
'/routing bgp instance print detail without-paging',
'/ip route print detail without-paging',
'/routing ospf instance print detail without-paging',
'/routing ospf neighbor print detail without-paging'
]
DETAIL_RE = re.compile(r'([\w\d\-]+)=\"?(\w{3}/\d{2}/\d{4}\s\d{2}:\d{2}:\d{2}|[\w\d\-\.:/]+)')
WRAPPED_LINE_RE = re.compile(r'^\s+(?!\d)')
def populate(self):
super(Routing, self).populate()
self.facts['bgp_peer'] = dict()
self.facts['bgp_vpnv4_route'] = dict()
self.facts['bgp_instance'] = dict()
self.facts['route'] = dict()
self.facts['ospf_instance'] = dict()
self.facts['ospf_neighbor'] = dict()
data = self.responses[0]
if data:
peer = self.parse_bgp_peer(data)
self.populate_bgp_peer(peer)
data = self.responses[1]
if data:
vpnv4 = self.parse_vpnv4_route(data)
self.populate_vpnv4_route(vpnv4)
data = self.responses[2]
if data:
instance = self.parse_instance(data)
self.populate_bgp_instance(instance)
data = self.responses[3]
if data:
route = self.parse_route(data)
self.populate_route(route)
data = self.responses[4]
if data:
instance = self.parse_instance(data)
self.populate_ospf_instance(instance)
data = self.responses[5]
if data:
instance = self.parse_ospf_neighbor(data)
self.populate_ospf_neighbor(instance)
def preprocess(self, data):
preprocessed = list()
for line in data.split('\n'):
if len(line) == 0 or line[:5] == 'Flags':
continue
elif not re.match(self.WRAPPED_LINE_RE, line):
preprocessed.append(line)
else:
preprocessed[-1] += line
return preprocessed
def parse_name(self, data):
match = re.search(r'name=.(\S+\b)', data, re.M)
if match:
return match.group(1)
def parse_interface(self, data):
match = re.search(r'interface=([\w\d\-]+)', data, re.M)
if match:
return match.group(1)
def parse_instance_name(self, data):
match = re.search(r'instance=([\w\d\-]+)', data, re.M)
if match:
return match.group(1)
def parse_routing_mark(self, data):
match = re.search(r'routing-mark=([\w\d\-]+)', data, re.M)
if match:
return match.group(1)
else:
match = 'main'
return match
def parse_bgp_peer(self, data):
facts = dict()
data = self.preprocess(data)
for line in data:
name = self.parse_name(line)
facts[name] = dict()
for (key, value) in re.findall(self.DETAIL_RE, line):
facts[name][key] = value
return facts
def parse_instance(self, data):
facts = dict()
data = self.preprocess(data)
for line in data:
name = self.parse_name(line)
facts[name] = dict()
for (key, value) in re.findall(self.DETAIL_RE, line):
facts[name][key] = value
return facts
def parse_vpnv4_route(self, data):
facts = dict()
data = self.preprocess(data)
for line in data:
name = self.parse_interface(line)
facts[name] = dict()
for (key, value) in re.findall(self.DETAIL_RE, line):
facts[name][key] = value
return facts
def parse_route(self, data):
facts = dict()
data = self.preprocess(data)
for line in data:
name = self.parse_routing_mark(line)
facts[name] = dict()
for (key, value) in re.findall(self.DETAIL_RE, line):
facts[name][key] = value
return facts
def parse_ospf_instance(self, data):
facts = dict()
data = self.preprocess(data)
for line in data:
name = self.parse_name(line)
facts[name] = dict()
for (key, value) in re.findall(self.DETAIL_RE, line):
facts[name][key] = value
return facts
def parse_ospf_neighbor(self, data):
facts = dict()
data = self.preprocess(data)
for line in data:
name = self.parse_instance_name(line)
facts[name] = dict()
for (key, value) in re.findall(self.DETAIL_RE, line):
facts[name][key] = value
return facts
def populate_bgp_peer(self, data):
for key, value in iteritems(data):
self.facts['bgp_peer'][key] = value
def populate_vpnv4_route(self, data):
for key, value in iteritems(data):
self.facts['bgp_vpnv4_route'][key] = value
def populate_bgp_instance(self, data):
for key, value in iteritems(data):
self.facts['bgp_instance'][key] = value
def populate_route(self, data):
for key, value in iteritems(data):
self.facts['route'][key] = value
def populate_ospf_instance(self, data):
for key, value in iteritems(data):
self.facts['ospf_instance'][key] = value
def populate_ospf_neighbor(self, data):
for key, value in iteritems(data):
self.facts['ospf_neighbor'][key] = value
FACT_SUBSETS = dict(
default=Default,
hardware=Hardware,
interfaces=Interfaces,
config=Config,
routing=Routing,
)
VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())