12-22-2022 01:21 AM
Hi Everyone,
I am trying to use PYATS to pull some LLDP neighbor information from some switches.
The format I get out of it is nested JSON, but I want to have the information in a easy to iterate over list format.
As you can see above the LLDP information is neste. The best for me would be to have the data in a list contaning the interface neighbors and id.
How do I convert this format to something easy to iterate over like the CDP output from PYATS?
Solved! Go to Solution.
12-23-2022 12:12 AM
Hi
You may use this function:
def genieLldp_2_listOfDicts(lldpdict):
result = list()
for localint, remote in lldpdict.get("interfaces").items():
for remoteint, remotedetails in remote.get("port_id").items():
for neighbor in remotedetails.get("neighbors").keys():
result.append({
"local_int": localint,
"remote_int": remoteint,
"neighbor": neighbor.strip()
})
return result
Complete example:
import json
def genieLldp_2_listOfDicts(lldpdict):
result = list()
for localint, remote in lldpdict.get("interfaces").items():
for remoteint, remotedetails in remote.get("port_id").items():
for neighbor in remotedetails.get("neighbors").keys():
result.append({
"local_int": localint,
"remote_int": remoteint,
"neighbor": neighbor.strip()
})
return result
lldp = {
"interfaces": {
"GigabitEthernet2": {
"port_id": {
"Gi0/3": {
"neighbors": {
"IOSvL2.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
},
"GigabitEthernet3": {
"port_id": {
"Gi0/2": {
"neighbors": {
"IOSv.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
},
"GigabitEthernet4": {
"port_id": {
"Gi0/1": {
"neighbors": {
"IOSv2.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
}
},
"total_entries": 3
}
print(json.dumps(genieLldp_2_listOfDicts(lldp), indent=1))
Result:
[
{
"local_int": "GigabitEthernet2",
"remote_int": "Gi0/3",
"neighbor": "IOSvL2.lab"
},
{
"local_int": "GigabitEthernet3",
"remote_int": "Gi0/2",
"neighbor": "IOSv.lab"
},
{
"local_int": "GigabitEthernet4",
"remote_int": "Gi0/1",
"neighbor": "IOSv2.lab"
}
]
HTH
Marcel
12-23-2022 12:12 AM
Hi
You may use this function:
def genieLldp_2_listOfDicts(lldpdict):
result = list()
for localint, remote in lldpdict.get("interfaces").items():
for remoteint, remotedetails in remote.get("port_id").items():
for neighbor in remotedetails.get("neighbors").keys():
result.append({
"local_int": localint,
"remote_int": remoteint,
"neighbor": neighbor.strip()
})
return result
Complete example:
import json
def genieLldp_2_listOfDicts(lldpdict):
result = list()
for localint, remote in lldpdict.get("interfaces").items():
for remoteint, remotedetails in remote.get("port_id").items():
for neighbor in remotedetails.get("neighbors").keys():
result.append({
"local_int": localint,
"remote_int": remoteint,
"neighbor": neighbor.strip()
})
return result
lldp = {
"interfaces": {
"GigabitEthernet2": {
"port_id": {
"Gi0/3": {
"neighbors": {
"IOSvL2.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
},
"GigabitEthernet3": {
"port_id": {
"Gi0/2": {
"neighbors": {
"IOSv.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
},
"GigabitEthernet4": {
"port_id": {
"Gi0/1": {
"neighbors": {
"IOSv2.lab ": {
"capabilities": [
"R"
],
"hold_time": 120
}
}
}
}
}
},
"total_entries": 3
}
print(json.dumps(genieLldp_2_listOfDicts(lldp), indent=1))
Result:
[
{
"local_int": "GigabitEthernet2",
"remote_int": "Gi0/3",
"neighbor": "IOSvL2.lab"
},
{
"local_int": "GigabitEthernet3",
"remote_int": "Gi0/2",
"neighbor": "IOSv.lab"
},
{
"local_int": "GigabitEthernet4",
"remote_int": "Gi0/1",
"neighbor": "IOSv2.lab"
}
]
HTH
Marcel
06-27-2024 03:13 AM
use ansible gather_facts: yes
as here :
https://raw.githubusercontent.com/NWMichl/network-automation/master/fix_ifdesc.yml
Fix Your Interface Description with Ansible and CDP/LLDP – NWMichl Blog
--- - name: Set Interface description based on CDP/LLDP discovery hosts: all gather_facts: yes connection: network_cli tasks: - name: Set IOS Interface Description ios_config: lines: - description Connected to {{ item.value[0].host }} via its {{ item.value[0].port }} parents: interface {{ item.key }} save_when: changed with_dict: "{{ansible_facts.net_neighbors }}" when: ansible_network_os == 'ios' - name: Set NXOS Interface Description nxos_config: lines: - description Connected to {{ item.value[0].host }} via its {{ item.value[0].port }} parents: interface {{ item.key }} save_when: changed with_dict: "{{ansible_facts.net_neighbors }}" when: ansible_network_os == 'nxos'
and you will have dictionary : ansible_facts.net_neighbors
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide