I’ve had the great fortune to take a Python and Ansible Programming for Network Engineers class from Kirk Byers (@kirkbyers) of PyNet over the last 9 weeks. I’m not a pro at either yet, but I see a future in my organization where we can take advantage of simple scripting, and perhaps more. An ideal case for our team would be to use Ansible to generate and push configurations based on templates to closet/IDF switches. This would save a lot of time and ensure all configs meet the given standard. The Ansible template can be crafted in such a way that we can audit existing switches and even push updates to them based on a template.
We started off with the basics of the git version control system and Github, the online hub for many individuals’ and corporate repositories. These are essential to proper step-by-step code creation so we can write some code, test it, then commit the changes so we know we have something good to fall back on if our next change messes things up. We then went on to structured data formats such as JSON and YAML, which are common ways to represent things you want to send to (or get from) a router, switch, or flat configuration file. Since many of us network nerds use Cisco, we were introduced to the CiscoConfParse Python library as a way to translate a standard Cisco IOS configuration file a structured data format. This makes it much easier to script operations for Cisco configurations.
Throughout the 9-week course, Kirk added on more and more useful libraries with great examples in the videos and assigned exercises as well as his Github repo. The repo (short for repository) has all his solutions to the exercises, but as with any learning experience you learn the most by writing your own code and only “cheating” (wink, wink) periodically. Some of these libraries along with example use cases included:
- telnetlib (allows telnet connections to a target network device)
- pysnmp (execute SNMP GET requests for monitoring)
- snmp_helper (Kirk’s handy-dandy SNMP function that manipulates output from pysnmp to make it more usable)
- time (check via SNMP for any config changes that happened since the last change was made)
- getpass (interactive way to ask user to input password while masking it on the screen and assigning it to a variable in the code)
More to come in future posts. I definitely recommend signing up for this course if you’re a network engineer and want to get started with Python and Ansible.