cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
4997
Views
16
Helpful
3
Replies

Python or ansible script to shutdown interfaces that are unused AND in a specific VLAN

Alston518
Level 1
Level 1

With ansible, I was able to put together a playbook that shutdown any port in the "down" state or "notconnect" state. The only issue was, I could't also filter it by a certain vlan (meaning I didn't know how to). Is there a way with netmiko to shutdown a port in the notconnet or down state, and also in a specific vlan?

 

Here is a link to my playbook I currently use for ansible......

https://github.com/Alston518/Ansible-IOS-/blob/main/Shutdown%20Unused%20Port%20IOS

 

 

This works to see the port states and issue a shutdown commdand anything not being used (not connect state or down state). It looks at the state and basically if it isnt "up", it issues a shutdown command.

What can I add to that to make it only issue it to unused ports in a specific vlan? When I run the iosfacts, I get a ton of interface info, but nothing about what vlan it is on.

3 Replies 3

You would need to pull the configuration back from the device and parse the details for the interfaces which are on the VLAN. Based on a simple if statement you could shut the ports based on their status, VLAN etc..

 

Have a look at pyATS example here https://pubhub.devnetcloud.com/media/pyats/docs/aetest/examples.html

 

Hope this helps.

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Thanks I will check that out. I am new to automation so trying to understand it all.

 

Will CiscoConfParse or TextFSM accomplish this too? I have never used either of those but was looking on reddit and someone suggested those. From my understanding, I can use TextFSM to find the interfaces I am looking for. Will it just put it in a normal text file, similar to a host file for ansible or netmiko?

 

Like if I want to use textfsm and netmiko to accomplish this, what will the host file look like? Is it a list of IPs and interfaces?

Hi @Alston518 

 

What I have found is that is it not just one tool but which tool is best for what you are trying to do or for your environment.   You can certainly do what you want with Netmiko.  In truth, when I start having to do more complicated logic I generally move away from Ansible and go a python based module (nornir, pyATS, etc.)

 

The repo below can give you some ideas on what you can do with Netmiko.   It has a simple script that takes in a layer 3 device and will build you a list of CDP neighbors. This is your "inventory" file.  You can then take that list and feed it to another script that gets and parses show commands.  Basically what you were already thinking..a list of IPs or FQDNs.  I like to save these things in JSON files (or YAML) so they are easy to read but also easy to pass the information into a subsequent script.  

https://github.com/cldeluna/client_discovery


This might also give you some ideas:

https://gratuitous-arp.net/configuration-creation-with-nornir/

 

I think pyATS would work very well for you too.  Makes the parsing a bit easier.   I wind up working alot with Netmiko because I often have to run scripts from a restricted Windows system where I can't install things like WSL for pyATS but I can install portable Python.  If you don't have those kinds of restrictions, do check out pyATS.

https://gratuitous-arp.net/getting-started-with-pyats-and-genie/

 

There are many "right" ways to solve your problem and I'm glad you are looking at the whole field!