Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cdp neighbors to ansible_net_neighbors facts #457

Merged
merged 11 commits into from
Dec 14, 2023
3 changes: 3 additions & 0 deletions changelogs/fragments/fix_add_cdp_neighbors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- iosxr_facts - Add cdp neighbors in ansible_net_neighbors dictionary (https://github.com/ansible-collections/cisco.iosxr/pull/457).
2 changes: 1 addition & 1 deletion docs/cisco.iosxr.iosxr_facts_module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ Common return values are documented `here <https://docs.ansible.com/ansible/late
</td>
<td>when interfaces is configured</td>
<td>
<div>The list of LLDP neighbors from the remote device</div>
<div>The list of LLDP and CDP neighbors from the remote device. If both, CDP and LLDP neighbor data is present on one port, CDP is preferred.</div>
<br/>
</td>
</tr>
Expand Down
45 changes: 45 additions & 0 deletions plugins/module_utils/network/iosxr/facts/legacy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class Interfaces(FactsBase):
"show ipv6 interface",
"show lldp",
"show lldp neighbors detail",
"show cdp",
"show cdp neighbors detail",
]

def populate(self):
Expand All @@ -142,6 +144,10 @@ def populate(self):
neighbors = self.responses[3]
self.facts["neighbors"] = self.parse_neighbors(neighbors)

if "CDP is not enabled" not in self.responses[4]:
neighbors = self.responses[5]
self.facts["neighbors"] = self.parse_cdp_neighbors(neighbors)

def populate_interfaces(self, interfaces):
facts = dict()
for key, value in iteritems(interfaces):
Expand Down Expand Up @@ -200,6 +206,25 @@ def parse_neighbors(self, neighbors):
facts[intf].append(fact)
return facts

def parse_cdp_neighbors(self, neighbors):
facts = dict()
for entry in neighbors.split("-------------------------"):
if entry == "":
continue
intf_port = self.parse_cdp_intf_port(entry)
if intf_port is None:
return facts
intf, port = intf_port
if intf not in facts:
facts[intf] = list()
fact = dict()
fact["host"] = self.parse_cdp_host(entry)
fact["platform"] = self.parse_cdp_platform(entry)
fact["port"] = port
fact["ip"] = self.parse_cdp_ip(entry)
facts[intf].append(fact)
return facts

def parse_interfaces(self, data):
parsed = dict()
key = ""
Expand Down Expand Up @@ -281,3 +306,23 @@ def parse_lldp_port(self, data):
match = re.search(r"Port id: (.+)$", data, re.M)
if match:
return match.group(1)

def parse_cdp_intf_port(self, data):
match = re.search(r"^Interface: (.+), Port ID \(outgoing port\): (.+)$", data, re.M)
if match:
return match.group(1), match.group(2)

def parse_cdp_host(self, data):
match = re.search(r"^Device ID: (.+)$", data, re.M)
if match:
return match.group(1)

def parse_cdp_platform(self, data):
match = re.search(r"^Platform: (.+),", data, re.M)
if match:
return match.group(1)

def parse_cdp_ip(self, data):
match = re.search(r"^ IP address: (.+)$", data, re.M)
if match:
return match.group(1)
3 changes: 2 additions & 1 deletion plugins/modules/iosxr_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@
returned: when interfaces is configured
type: dict
ansible_net_neighbors:
description: The list of LLDP neighbors from the remote device
description: The list of LLDP and CDP neighbors from the remote device. If both,
CDP and LLDP neighbor data is present on one port, CDP is preferred.
returned: when interfaces is configured
type: dict

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/modules/network/iosxr/fixtures/show_cdp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Global CDP information:
Sending CDP packets every 60 seconds
Sending a holdtime value of 30 seconds
Sending CDPv2 advertisements is not enabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-------------------------
Device ID: device2.cisco.com
Entry address(es):
IP address: 171.68.162.134
Platform: cisco 4500, Capabilities: Router
Interface: Ethernet0/1, Port ID (outgoing port): Ethernet0
Holdtime : 156 sec

Version :
Cisco Internetwork Operating System Software
IOS (tm) 4500 Software (C4500-J-M), Version 11.1(10.4), MAINTENANCE INTERIM SOFTWARE
Copyright (c) 1986-1997 by Cisco Systems, Inc.
Compiled Mon 07-Apr-97 19:51 by dschwart


Total cdp entries displayed : 1
16 changes: 16 additions & 0 deletions tests/unit/modules/network/iosxr/test_iosxr_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,19 @@ def test_iosxr_facts_cpu_utilization(self):
ansible_facts["ansible_net_cpu_utilization"],
cpu_utilization_data,
)

def test_iosxr_facts_neighbors(self):
set_module_args(dict(gather_subset="interfaces"))
result = self.execute_module()
ansible_facts = result["ansible_facts"]["ansible_net_neighbors"]
expected_neighbors = {
"Ethernet0/1": [
{
"host": "device2.cisco.com",
"platform": "cisco 4500",
"port": "Ethernet0",
"ip": "171.68.162.134",
},
],
}
self.assertCountEqual(ansible_facts.keys(), expected_neighbors.keys())
Loading