Network Automation with Python: Getting Started

Learn about basic network automation concepts using Python in this tutorial.

Marcia Savage

May 2, 2017

4 Min Read
Network Computing logo

In the first two posts in this series, we covered why network automation is needed and tool selection. Now, let's look at how we can get started using some of those tools. If you have ever written a maintenance operations procedure, you have already written a script that is for human consumption. If we structure the language differently, we can represent the same steps with Python or Ansible and let the tools execute those steps more quickly and consistently than we can by typing in a console. For the sake of brevity, this article assumes you are in a Linux or MacOS type of terminal. Some commands will vary slightly on Windows.

Automation typically represents a series of steps or defines our intent through some type of code. Hence the phrase infrastructure-as-code. When working with code, we want to be able to track changes, have backups, and determine who changed what and when. Git can help do those things, so start by creating a new directory and initializing it as a git repository.

null

IAO-logo.png

 

Most projects have a common location to look for a description of what it does and how to use it.  So create a README.txt with your favorite editor, add a description and save it. Now, tell git to manage this file and save this version. Good commit messages have a short heading (50 characters or less) and are in the imperative, present tense. Think "This commit will... ".

null

no-internet.jpg

 

Then enter a description of your commit

null

AWS-status-dashboard.jpg

 

Save and exit your editor.

Now that you have a repository and something under revision control, it's time to write some code.

Virtualenv simplifies working on different Python projects with different requirements and eliminates the need for administrative privileges to install modules. Setup a new virtual environment with 'virtualenv venv' then tell your terminal shell to use that environment 'source venv/bin/activate', then to ensure you have the latest package manager, 'pip install --upgrade pip'. Next time you come back to this project to use it, just activate the environment with 'source venv/bin/activate' before you begin.

null

ebt-cards.jpg

 

Tell git not to track the venv directory

null

yahoo-mail.jpg

 

Install the Jinja2 module and make a script print out something rendered from a template. Over time your project may require many modules, so make it easy for someone else to setup their environment like yours. When you need a new module, add it to a file called requirements.txt, then tell pip to install the packages in that file.

null

google-downtime.png

 

Python lets you try commands in an interactive shell. We will start there to get fast feedback on what we try. Just type 'python' then enter commands that follow the '>>> ' prompts. When templates are rendered, text such as '{{ some_var }}' will be replaced by the value of 'some_var' in your script.

null

nasdaq.jpg

 

Press CTRL+D to exit the Python shell. Now, in your editor, create a new script based on the lines, above. In the example, the script is named configure.py (.py indicates it is a Python script).

null

timewarner-outage.jpg

 

Run it with 'python configure.py'.  Then commit it.

null

no-mobile-phones.jpg

 

Now it's time to make our script actually talk to a device. Add the Netmiko module, which communicates with many different network operating systems:

null

iWeek_VMware.jpg

 

Next, create version.py, to gather some information from a device and generate a report.

null

datacenter-work.jpg

 

Run it with 'python version.py' and when it works, save the script to git.

null

iWeek_PMP.jpg

 

This was just a glimpse of the power of automation and source-code management. Imagine any status checks you need to run on the network, then multiply that across the number of devices you have. Think of all of those checks running on a moment's notice and taking only a few minutes: You can get near-instant answers and get back to more important things.  Additionally, these concepts can be expanded to manage your configurations. In our next article, we will walk through the same scenario using Ansible. The final article in the series will address some of the cultural aspects of embracing automated workflows. Join us in person at Interop ITX for a hands-on workshop exploring these tools, including learning how to configure devices.

Meet Jere Julian and Scott Lowe in person at their half-day workshop at InteropITX, "Hands-On Practical Network Automation." The workshop will cover how to get started with network automation, and experts will help guide attendees. Register now for Interop ITX, May 15-19 in Las Vegas.

Interop-end-banner.gif

Jere Julian, DevOps for Networking Evangelist and Extensibility Engineer for Arista Networks, has more than 20 years’ experience in networking and automation. Scott Lowe, Engineering Architect at VMware, is a blogger, speaker, and best-selling author with more than 20 years of experience in the IT industry.

 

About the Author(s)

Marcia Savage

Executive Editor, Network Computing

SUBSCRIBE TO OUR NEWSLETTER
Stay informed! Sign up to get expert advice and insight delivered direct to your inbox
More Insights