The second homework (or hands-on assignment) is to create summary reports. The idea is to collect information from the network devices and turn that into an easily readable report that is useful on its own.
This course does not require to solve a set of specific problems, it rather sets a theme to follow and provides the student with ideas. One of the given ideas is to create a network graph based on neighbor information collected from the devices. I like this idea, because it provides me with an opportunity to look into Graphviz which is a suggested tool to visualize (i.e. generate an image file of) the connectivity graph.
This problem has some relation to the
Prescriptive Topology Manager
from Cumulus Networks
(PTM Documentation)
which uses a file in Graphviz format named topology.dot
to describe
the intended network topolgy and uses the Link Layer Discovery Protocol
(LLDP) to compare the switch's neighbors with the given topology.
Since I am using virtual Cisco IOS devices in my lab, I am using the Cisco Discovery Protocol (CDP, also known as CiscoDP on Enterasys (now ExtremeEOS) switches, sometimes known as Industry Standard Discovery Protocol (ISDP) as well) as data source.
I intend to use the
ios_command
module to collect CDP data from the virtual routers. The output is then parsed
by an Ansible filter (one of the
parse_cli
or parse_cli_textfsm
filters) into variables. The
variables are used together with a
jinja2 template
to generate a network topology description in Graphviz format,
i.e. using the
DOT language.
The
dot
program is used to create an image file in
PNG
format.
When I tried to use the ios_command
module, it would fail with the error
message:
unable to open shell. Please see: https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell
.
Root cause was that pip install ansible
did not install all requirements
for the ios_command
module, it missed the two Python packages enum34
and
ipaddress
. For all the gory details see the separate
Interlude 1 document.
The solution approach provides a general structure for the Ansible playbook
topology.yml
. I have used tags
to allow
invoking parts of the playbook that belong together:
get
retrieves CDP neighbor information from network devices and writes the raw CLI output to a fileparse
reads the raw CLI output from file and parses it into a data structuregraph
uses the data structure from theparse
step to generate a DOT language description of the graph (connectivity.gv
) and a PNG image visualizing it (connectivity.png
)debug_*
tags allow debugging of the respective steps
The lab as configured for homework 1 lacks interesting connectivity, thus I needed add some configuration. This is described in the separate Interlude 2 document.
The following sections provide more information about the Ansible playbook to create a visualization of the network connectivity.
The playbook comprises one play with several tasks. The network devices used
are specified at play level (the OOB
group using the OOB interfaces, since
in-band connectivity is not yet established). Additionally, use of the local
connection is specified at play level, because all tasks are executed on
localhost
:
- Files for intermediate steps and end result need to be created on the Ansible host.
- Ansible network modules are executed on the Ansible host, the module then connects to the network device.
A few variables to control where generated files are stored and how they are named are defined at play level as well.
Four tasks (with the tag get
) are used to retrieve CDP neighbor information
from the network devices. The first uses the ios_command
module to issue
show cdp neighbors detail
on each router. This information is written to
a text file in the fouth get
task, after the second and third get
tasks
have deleted old CDP information and (re-)created the directory to store it in.
Two tasks (with tag parse
) are used to parse the CDP data to fill a data
structure that can be used in a Jinja2 template. The first reads the raw CDP
output using the Ansible lookup
plugin file
. The second uses the
parse_cli_textfsm
filter with the
TextFSM
template
cdp_neighbors_detail.textfsm
to create a list with neighbor data.
Four tasks (with tag graph
) are used to generate an image that shows the
network topology. The first two tasks prepare the output directory, i.e.
they delete contents from previous playbook executions and (re-)create the
output directory. The third task uses the Ansible template
module to
generate a DOT language description with the
connectivity.j2
Jinja2 template. The fourth task invokes the dot
program using the Ansible
command
module to generate a PNG image of the connectivity graph.
The Ansible playbook topology.yml
generates
to result files:
- The DOT language graph description
connectivity.gv
- The PNG image
connectivity.png
of the topology
- Ansible Filters
- Ansible Lookup Plugins
- Ansible Modules
- Ansible Templating
- DOT language
- DOT language (Wikipedia)
- dot program
- Graphviz
- Jinja2
- PNG
- Prescriptive Topology Manager
- PTM Documentation
- TextFSM
- TextFSM documentation
- TextFSM examples
- YAML
BNAS2018 GitHub repository | My GitHub user page | My home page