Getty Images/iStockphoto

Tip

Best practices for network automation with Python

Python libraries, version control, documentation and object-oriented programming provide helpful best practices for network engineers tackling network automation with Python.

Enterprise networks are a crucial component of the digital economy and must constantly adapt to evolving needs. Most network changes are still done manually, however, leading to an increase in Opex spent on troubleshooting and modification. This results in suboptimal network performance, frequent outages and damaged business reputations.

Network engineers can use automation and optimization techniques to rely less on manual input and ensure their networks function properly with fewer bugs. Python is one of the most preferred languages for implementing programming and automation at scale. Network teams can use it to write simple scripts that automate tasks like network detection, device configuration and troubleshooting.

But poorly designed automation projects can cause sleepless nights for network engineers. To avoid these challenges, teams can optimize the network and associated projects by identifying end goals, understanding network weaknesses, choosing the right tools and monitoring the network continuously.

In this article, we discuss best practices and helpful strategies that network engineers can follow while using Python in network automation projects.

1. Python libraries and framework selection

Network engineers who use Python commonly hear the phrase "Python has batteries included," due to how the language comes with practically everything users need to tackle various tasks.

For example, Python has hundreds of available libraries. Some libraries and frameworks fit well for particular network automation projects, while others do not.

Well-known Python libraries include the following:

  • Ansible. This library provides an open source tool for configuration management.
  • Network Automation and Programmability Abstraction Layer with Multivendor support (NAPALM). This library helps users manage network devices in multivendor environments.
  • Netmiko. This library provides a unified interface to interact with network devices over Secure Socket Shell.
  • Nornir. This library is used to automate a range of network tasks.

When selecting a Python library, consider the following factors:

  • Choose a library or framework for a specific need.
  • Choose a library based on the scalability of your network.
  • Choose a suitable library or framework based on the experience of your team.

2. Object-oriented programming

The world of programming is full of concepts and abstraction that might seem unrelated to networking at first. But concepts like object-oriented programming (OOP) can be beneficial for network automation strategies. OOP is a programming paradigm that uses objects and classes to organize and structure code. This structure helps simplify code complexity and reduce errors.

Just as building architects must describe what their work will look like once it's finished, network engineers and developers should know what their scripts and software will look like before building. OOP enables this strategy by breaking down applications into different blocks, each of which is considered an object that represents a real-life entity.

OOP is important in network automation, as it enables network engineers to write modular and reusable code. Instead of creating new parameters for each script, they can adapt the code classes for different network devices and configurations.

Python works as both an OOP language and a functional programming language. The Python snippet in Figure 1 shows OOP in action, as it defines and describes two router and switch classes.

Screenshot of OOP in Python
Figure 1. Python supports object-oriented programming.

3. Error handling and exception handling

Error handling in Python helps network engineers deal with unexpected events that occur during program execution. These events are called exceptions, which can result from different factors, such as trying to access a file that isn't available anymore and invalid input. Exception handling is the process of responding to those unexpected events that occur.

Error handling and exception handling are both crucial for network automation in test and production environments. They can also help prevent network outages and data loss in scripts.

The snippet in Figure 2 is an example of error handling and exception handling, using Netmiko to connect to a Cisco IOS device. The script imports ConnectHandler to establish a connection to the device and uses the NetmikoTimeoutException command in case the connection times out.

Screenshot of error handling and exception handling in Python
Figure 2. Implement error and exception handling in Python scripts.

Error handling and exception handling involve more than writing scripts. Other practical steps include the following:

  • Use comments to explain your code.
  • Modularize your code using functions.
  • Use the try-except statement to catch and handle exceptions.
  • Use the logging module to log errors and exceptions.

4. Documentation

Documentation serves as a guide for network engineers and developers, as it provides information about different network components, scripts and projects. Without proper documentation, it can be challenging to work with complex networks.

Here are some steps network engineers can follow when documenting Python network automation projects:

  • Write clear comments for your code.
  • Use Python docstrings to explain complex code.
  • Share your documentation with others.
  • Update your code for any applied changes.

5. Testing and validation

Automated networks aren't perfect and can go down at any time. But some DevOps principles can help network engineers maintain more reliable network infrastructure. For example, testing and validation with continuous integration/continuous delivery (CI/CD) can help network engineers improve their work.

A well-known Python framework for testing and validation is PyATS, which is an open source framework developed by Cisco. Network engineers can use the tool to validate the state of the network, check configurations and test scripts.

Here are some best practices network engineers can follow to maintain stable network infrastructures with testing and validation:

  • Use a testing framework.
  • Use a CI/CD pipeline.
  • Write unit tests.
  • Write integration tests.

6. Version control and code management

Other DevOps principles and tools can help network engineers tackle automation projects -- often called NetDevOps. These tools include Git, GitLab, Bitbucket, CI/CD pipelines and version control systems (VCSes), among others.

Enterprises frequently have employees that work in different locations. Version control enables collaboration between network engineers and developers as they work on scripts together. For example, teams can use Git, a common VCS, to push code to a central repository, such as GitHub.

Code repositories and descriptive commit messages help track code changes, improve quality and support team feedback. They also help document the code.

To make the most of version control in Python automation projects, use the following best practices:

  • Create meaningful names. Choose descriptive names for your classes, variables and methods.
  • Choose a consistent coding style. Consistency makes the code easier to read and maintain. The Python style guide, Python Enhancement Proposal 8, can help.
  • Use modules and packages to organize code. Modules make it easier to read, maintain and reuse code.
  • Use libraries to simplify your work. A plethora of Python libraries are available, and you can choose whichever one suits your needs.

Next Steps

Phases to build a network automation architecture

Dig Deeper on Network management and monitoring

Unified Communications
Mobile Computing
Data Center
ITChannel
Close