TextFSM to the Rescue
TextFSM is a state machine (basically like a templating engine) that is purpose built to simplify working with regular expressions and getting structured data out of traditional network devices. Nifty.
For the more formal introduction taken directly from the TextFSM website:
"TextFSM, originally developed to allow programmatic access to information given by the output of CLI driven devices, such as network routers and switches, it can however be used for any such textual output."
How does it work?
After seeing this, I wanted to do three things:
- Convert the individual lists to a list of dictionaries such that each existing row is it’s own dictionary and the keys are equal to the column headers as currently shown.
- Make it possible to use this approach by using real-time state data and not just local text stored in offline files
- Integrate with Ansible to eliminate the user from having to deal with the list to dict conversion, etc. and this way, it’ll just seem like structured data is being returned from every device!
What was the result?
Another Example - Gathering neighbor data from a switch
Running an Ansible playbook using netget:
Almost forgot - here is the Playbook:
Since I am not regex guru, I’ve come to use the following site quite a bit to test and troubleshoot regular expressions: http://www.regexr.com/. There are plenty of others, but this one has been doing the trick for me.
Side note: I haven’t posted the code yet for the netget module mainly because it doesn’t have all the functionality I’d like it to have yet. And I started using Paramiko as the library, but think it probably makes more sense to use the library, called netmiko, that Kirk Byers has been building since it’s multi vendor, etc., which also depends on Paramiko anyway.
Given that the industry has kind of standardized on a common CLI, this method should even work across multiple vendors, which is pretty damn cool. TextFSM is definitely not limited to network gear though, so feel free to use it on whatever device you want - even those native Linux operating systems :)
What do you think? What other functionality would you like to see in something like this?