Currently in Migration - Jason Edelman's Old Blog
  • Home
  • About
  • Contact

Leveraging Cisco NX-API with Ansible to Make Your Life Easier

8/21/2014

3 Comments

 
I had a conversation recently with someone who has more of a sysadmin background.  We started talking about the intersection of DevOps and networking and while his environment wasn’t large, there was one pain point he talked about – he doesn't have access to the network switches to ensure they are configured properly for “his” servers and to ensure packets aren't being dropped, etc. when there are issues with the application, server, or network.  And by the way, he really doesn't want access to the data center switches, because after all, many fear logging into network devices that are in production.  

Could DevOps and network automation help here?
In fact, the answer is yes.  The goal is to get the right data into the right hands as quick as possible.  An automation platform can be used to query the switch to get the exact data the admin needs.  For those that have help desks supporting large campus networks, the same philosophy can be used there as well.   Help desk, junior admins, and cross-functional team members can now get what they need in just a few seconds.

In order to test this out, I’ve built a test bed using a Cisco Nexus 9000 switch using its NX-API and the Ansible automation framework.

While the remainder of this post walks through how this is accomplished, the main takeaway should be, there may be more opportunities for automation than we think. Keep your eyes open!
Picture
Source: http://salettaleadership.com/?p=211
Back to it...

Here is a screen shot of the Ansible playbook that I’ll be testing.
Picture
As you can see, this is a pretty small playbook that can be manipulated by anyone.  This playbook has two tasks.  The first tasks gets the required data from the Nexus 9000 switch.  It does this by using the module called intf_stats.  The ip of the switch and associated interface we want data on is sent to the module (note the indentation under intf_stats). The data being returned from the device is stored in a new variable called results. 

Note: interface supports two options: all or a specific interface, e.g. Ethernet1/1. Sample outputs for each will be shown below.

The second task uses the data returned and formats it using a pre-built template called interface_stats.j2 so that the admin can easily interpret the data.

Now that we’ve taken a quick look at the playbook, let’s run it!
Picture
There isn’t anything glamorous about running this Ansible playbook, but we can see everything is okay and the report (template) is created.  Let’s take a look at the report.

Picture
We can see now after running this playbook, the admin has access to all sorts of data without having to engage with a senior network engineer or clicking around through a GUI. Note: this clearly just a sub-set available from a switch.  Any data you want can be inserted here too.

This particular report showed the data for all interfaces although you can only see 3 interfaces in the above screen shot.  The next two screen shots show the playbook and report when just one interface is specified.

Picture
Picture

Jinja2 Template

I’m sure some of you will want to see this – the screen shot below shows the j2 template that I used to create the report. 
Picture
If you are interested in checking out all of the files and code I used, feel free to check them out here.

Let me know if you have any questions or any other thoughts on topics like this!

Thanks,
Jason

Twitter: @jedelman8

3 Comments
Sreenivas Makam
9/1/2014 02:19:49 am

hi Jason
I recently went through your blogs on Devops and Network automation and it is very nicely written with lot of good hands-on resources to try out these tools.

I had 1 question related to Ansible and the connection approach used.

As I understood, "connection:local" is used to execute the script locally and in "connection:remote", python script is executed on remote machine. Using "connection:local", I can see following examples for accessing the device:
Using python sdk library that device provides
calling apis like NXAPI mentioned in this blog
using library like OnePK

Under what scenarios, would "connection:remote" be useful where python script is executed directly in remote machine? I can think of the following, but not very sure:
Speed of access
apis provided externally dont expose all features
Use direct linux calls in remote machine

Do you agree with the reasons above? Are there more reasons other than whats mentioned above for using remote connection approach?

Thanks
Sreenivas Makam

PS:
Referring to your blogs and few other references, I wrote a small ansible module that probes a bunch of UCS devices in my network and prints out useful information about the devices(like vlans configured, mode etc). This was a simple program, but I can see the power of the automation capability that ansible and network automation together brings in.

Reply
Jason Edelman link
9/3/2014 03:07:27 am

Hi Sreenivas,

I think I agree with you, assuming I understand correctly :). For any APIs like Cisco APIs you mentioned, you would use "connection: local" and then you are relying on a module you build for the device connection.

For anything native Linux that also supports Python, you don't have to specify the connection parameter. It would then be assumed ssh is going to be used. This wouldn't work for some network devices b/c Ansible would ssh into the device, then drop in the required Python code, execute it, and remove the code and disconnect. And of course the majority of devices don't support bash/python.

Another example is Arista. I do believe they support ssh with Ansible which would offload the device connection work from you if you were building custom modules.

As far as explicitly stating "connection: remote" I've never done that, so I can't comment on what that actually does.

BTW, great work on the UCS modules. Did you use the UCS Python SDK for that?


Reply
Sreenivas Makam link
9/4/2014 01:11:45 am

hi Jason
Thanks for your response.

Yes, I used UCS Python SDK. Will play more with Ansible and Network automation in the coming weeks.

Regards
Sreenivas

Reply



Leave a Reply.

    Author

    Jason Edelman, Founder of Network to Code, focused on training and services for emerging network technologies. CCIE 15394.  VCDX-NV 167.


    Enter your email address:

    Delivered by FeedBurner


    Top Posts

    The Future of Networking and the Network Engineer

    OpenFlow, vPath, and SDN

    Network Virtualization vs. SDN

    Nexus 7000 FAQ

    Possibilities of OpenFlow/SDN Applications 

    Loved, Hated, but Never Ignored #OpenFlow #SDN

    Software Defined Networking: Cisco Domination to Market Education

    OpenFlow, SDN, and Meraki

    CAPWAP and OpenFlow - thinking outside the box

    Introduction to OpenFlow...for Network Engineers


    Categories

    All
    1cloudroad
    2011
    2960
    40gbe
    7000
    Arista
    Aruba
    Big Switch
    Brocade
    Capwap
    Christmas
    Cisco
    Controller
    Data Center
    Dell Force10
    Embrane
    Extreme
    Fex
    Hadoop
    Hp
    Ibm
    Isr G2
    Juniper
    Limited Lifetime Warranty
    Meraki
    Multicast
    N7k
    Nexus
    Nicira
    Ons
    Opendaylight
    Openflow
    Openstack
    Presidio
    Qsfp
    Quick Facts
    Routeflow
    Sdn
    Sdn Ecosystem
    Security
    Ucs


    Archives

    May 2015
    April 2015
    February 2015
    January 2015
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    June 2014
    May 2014
    March 2014
    February 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013
    May 2013
    April 2013
    March 2013
    February 2013
    January 2013
    December 2012
    November 2012
    October 2012
    June 2012
    May 2012
    April 2012
    March 2012
    February 2012
    January 2012
    December 2011
    November 2011


    RSS Feed


    View my profile on LinkedIn
Photo used under Creative Commons from NASA Goddard Photo and Video