Ansible – Infrastructure as Code Part 3 (Let’s do something interesting!)

We’ve collected our state data in Part 2.

Now let’s do something interesting with our data. I’ve switched out the role I will be using. So this will be a long blog post on how to set up and finally make something pretty with your data!

 

The Setup

What you’ll need –

The reason I’m using a different module for this, is that it will automatically grab a snapshot and format it into JSON. It has other features, like compare that I won’t get into. But for now, this will be good enough to start manipulating data.

The Play Book

---
- hosts: localhost
connection: local
gather_facts: no

- name: Interface Snapshot
hosts: switches
gather_facts: no
connection: network_cli
roles:
- ansible-pyats

tasks:
- name: Gather Snapshot
include_role:
name: ansible-pyats
tasks_from: snapshot_command

vars:
command: show interfaces
file: "snapshots/{{ inventory_hostname }}_interface_snapshot.json"

This play book is assigned to the switches in the inventory. It calls the role that we downloaded from GitHub with the task snapshot_command. I’m telling it to snapshot the show interfaces command. 

Step 1 – Grab the state of the interfaces using your playbook and output to JSON.

Step 2 – Import the data into Jupyter Notebook and convert it into useable information using Pandas.

Now you may be asking why Pandas and why Jupyter? I have a blog post here on why it’s easier to work with. On the note about Pandas, it’s a great tool for quickly putting information into a structure that easier to analyze. Need to do math based on Date time? Need to do some quick counting on cell values? Or maybe convert row data to columns? Very quick an easy to do in Pandas. So let’s get started.

Now this may seem a little denser than normal. But this is a direct export from Jupyter. This is using Python and the two modules to make the magic happen. If you want to try it out, you can copy the code below into Jupyter and use this JSON document. 

import pandas as pd #import pandas for data manipulation
import plotly.express as px #For a quick pretty graph at the end

df
= pd.read_json('/Users/**/Automation/ansible/snapshots/SW1_interface_snapshot.json') #Import the JSON document df.loc['arp_timeout':'bandwidth' , 'FastEthernet0/1':'FastEthernet0/13'] #grab the first 5 columns and top 3 rows
  FastEthernet0/1 FastEthernet0/10 FastEthernet0/11 FastEthernet0/12 FastEthernet0/13
arp_timeout 04:00:00 04:00:00 04:00:00 04:00:00 04:00:00
arp_type arpa arpa arpa arpa arpa
bandwidth 100000 10000 10000 10000 10000
df2 = df.loc[ ['line_protocol', 'last_input'] , : ] #export the desired values
df2.loc['line_protocol':'last_input', 'FastEthernet0/1':'FastEthernet0/13']
  FastEthernet0/1 FastEthernet0/10 FastEthernet0/11 FastEthernet0/12 FastEthernet0/13
line_protocol up down down down down
last_input 00:00:01 never never never never
df2 = df2.transpose() #flip the columns and rows; by default the interfaces are the columns
df2.head(3)
  line_protocol last_input
FastEthernet0/1 up 00:00:01
FastEthernet0/10 down never
FastEthernet0/11 down never
df2.loc[(df2.last_input == "never"), 'last_input']='23:59:59' #convert never to time value
df2.head(3)
  line_protocol last_input
FastEthernet0/1 up 00:00:01
FastEthernet0/10 down 23:59:59
FastEthernet0/11 down 23:59:59
df2["last_input"]= pd.to_datetime(df2["last_input"]) #convert time values to datetime; panda adds todays date
df2.head(3)
  line_protocol last_input
FastEthernet0/1 up 2020-06-18 00:00:01
FastEthernet0/10 down 2020-06-18 23:59:59
FastEthernet0/11 down 2020-06-18 23:59:59
df_value_counts = df2['line_protocol'].value_counts() #grab value counts and put into new dataframe
df_value_counts = df_value_counts.reset_index() #reset the index
df_value_counts.columns = ['State', 'Count'] #set column values
df_value_counts #display values
  State Count
0 down 26
1 up 6
fig = px.bar(df_value_counts,              # dataframe
       x="State",         # x will be the 'State' column of the dataframe
       y="Count",   # y will be the 'Count' column of the dataframe
       color="State", # color gets assigned to the State axis
       title=f"Interface State",
       labels={"State": "Up/Down","Count": "Count"}, # the axis names
       color_discrete_sequence=["red", "green"], # the colors used
       height=500,
       width=800) 
fig.show()
Interface State Graph

 

Jupyter Notebook

Make sure to install Jupyter using your favorite method. I installed jupyter in my venv and run it from there.

Let’s start off by launching Jupyter!Jupyter notebook launch low res

It’s as easy as that to get started.

 

Here’s the value that jupyter notebook can add to your work flow. 

Retains State –

Jupyter hellow world low res

When running each section shown. It evaluates it independently. Making an error and re-running the section doesn’t require me to re-run the whole script. This is great when you have to evaluate many config files and make a mistake. No reason to re-run the whole script. 

You can even run your connect portion of the script and work on other sections while you wait for the pull to finish.

Export and Share – 

You can chose many formats from python to another jupyter notebook. Share you work easily with others.

It’ll even retain variables, such as when pulling a show run from netmiko.

It Uses iPython – 

That’s cool I guess. I’m not 100% sure on the benefits of iPython. But you do get some magic commands that don’t come with regular python – https://ipython.readthedocs.io/en/stable/interactive/magics.html.

It Easily Works – 

I haven’t run into a situation yet where calling a module hasn’t worked. Even something like getpass.getpass() works.

Debugging – 

No need to print in the middle of the script. Want to see what a variable contains? Just call it. I have found that printing in a loop doesn’t work. Now I may be doing something wrong, but I can get it to work in regular python. 

Undo – 

When hitting ctrl-z, it will only undo what you did in that section!!!

 

 

Did I miss anything? Let me know! I probably haven’t explored jupiter’s full potential yet!

Scroll to Top