Improved Solution: Create Network Diagram from LLDP Data

A long while ago I published a sample Ansible/NAPALM/Jinja2 solution that would take LLDP information and turn it into a network diagram (I described its details in a short video that’s accessible to anyone attending our network automation course or having an Expert subscription).

The trickiest part of that solution was detection of bidirectional links:

  • If node A has node B as its LLDP neighbor and node B has node A as its LLDP neighbor then there should be only one link in the network diagram, not two of them;
  • If node A has node B as its LLDP neighbor but we have no idea what node B is, then we’re dealing with an external link and should add it to the network diagram (together with node B).

I used a dirty trick: sorting nodes alphabetically and adding links to the diagram only when the source node name comes before the target node name (proving the correctness of this idea is left as an exercise for the reader).

Erik Ruiter improved my solution and decided to track double links explicitly within his Jinja2 template: he’s adding already-processed links to a list and checking the list contents before adding a new link to the graph – a standard programming trick that’s a bit hard to implement in Jinja2, so you might want to check out his template.

1 comments:

  1. Usual practice/recommendation tho is to push logic outside of templates files (erb for ruby/chef automation and J2 for python/ansible) into separate helper modules. That way processing of incoming data such as filtering, enrichment and so on, can be tested and modified outside of the template body and used by several templates.
Add comment
Sidebar