Scripting vPC and trunk changes

I currently have a large-ish vPC/UCS environment. To add a VLAN I have to touch quite a few switches. Since there is little consistency I also have to reference my visio of the environment so I get the port channels correct or correlate CDP neighbors and port channel summaries. I finally got sick of doing all that so I wrote a python script that will do it for me. Like I noted, the port channel numbering is not consistent and I’m not proficient with Jinja2 (yet) so it’s a bit tedious referencing every switch. You should however get a good framework if this is something you are looking to accomplish.

# Here's a script that will prompt you for a VLAN number and name.
# It will then add the VLAN to all switches in the UCS domain. From
# there it will add the VLAN to all upstream & downstream trunks.
# This currently covers the devices listed below. No Intel or INS.

from netmiko import ConnectHandler

print "---------------------------------------------------------"
print " This script will prompt you for a VLAN number and name. "
print " It will then login into the UCS environment and create "
print " the VLAN and add the VLAN to the necessary trunks. "
print " Good luck! "
print "---------------------------------------------------------"
print " "

# -- Get the VLAN info from the user
vlan_number = raw_input("Enter the VLAN number: ")
vlan_int = int(vlan_number)
vlan_name = raw_input("Enter the VLAN description: ")

# -- Merge strings to create a list to pass to Netmiko
vlan_number_cli = "vlan " + vlan_number
vlan_name_cli = "name " + vlan_name

# -- FYI & last chance before execution
print " This script is about to add vlan", vlan_int, "to: "
print " svr-dsw1"
print " svr-dsw2"
print " unix-sw1"
print " unix-sw2"
print " unix-sw3"
print " unix-sw4"
print " and all the vPC peer links between them!"
print " "
last_chance = raw_input(" This is your last chance to cancel. Do you want to continue [Y/N] ?")

if last_chance.lower() == 'y':

svrdsw1 = {
'device_type': 'cisco_ios',
'host': 'svr-dsw1',
'username': 'ise_local_admin',
'password': 'SuPeRsEcReT',
'verbose': False, # set to True for troubleshooting
}

device = ConnectHandler(**svrdsw1)

# -- Send the commands to the device
device.config_mode()
device.find_prompt()
vlan_config_cmds = [vlan_number_cli, vlan_name_cli]
device.send_config_set(vlan_config_cmds)
device.exit_config_mode()

# -- Add the new VLAN to the trunks on the switch
vlan_trunk_cli = "switchport trunk allow vlan add " + vlan_number
device.config_mode()
device.find_prompt()
trunk_config_cmds = ['interface range po10, po40, po50, po52', vlan_trunk_cli]
device.send_config_set(trunk_config_cmds)
device.exit_config_mode()
device.disconnect()

print "Complete!"
print ""

print "Connecting to next switch and applying configuration... "
unixsw1 = {
'device_type': 'cisco_ios',
'host': 'unix-sw1',
'username': 'ise_local_admin',
'password': 'SuPeRsEcReT',
'verbose': False, # set to True for troubleshooting
}

device = ConnectHandler(**unixsw1)

# -- Send the commands to the device
device.config_mode()
device.find_prompt()
vlan_config_cmds = [vlan_number_cli, vlan_name_cli]
device.send_config_set(vlan_config_cmds)
device.exit_config_mode()

# -- Add the new VLAN to the trunks on the switch
vlan_trunk_cli = "switchport trunk allow vlan add " + vlan_number
device.config_mode()
device.find_prompt()
trunk_config_cmds = ['interface range po11, po17, po51, po84', vlan_trunk_cli]
device.send_config_set(trunk_config_cmds)
device.exit_config_mode()
device.disconnect()

print "Complete!"
print ""
print "Configuration has been applied to all devices!"
quit()

elif last_chance.lower() == 'n':
print "OK, exiting the program. Better luck next time."
quit()

else:
print "You didn't hit 'Y' so I am assuming you want to quit..."
quit()

In this example we only added the VLAN to two switches, but different port channels on each. If you want to add additional switches, you just need to copy-n-past this section and change the parameters to meet the switches name and port-channels.

print "Connecting to next switch and applying configuration... "
 unixsw3 = {
'device_type': 'cisco_ios',
'host': 'unix-asw3', # This is actual DNS hostname
'username': 'ise_local_admin', 
'password': 'SuPeRsEcReT',
'verbose': False, # set to True for troubleshooting
}

device = ConnectHandler(**unixsw3)

# -- Send the commands to the device
device.config_mode()
device.find_prompt()
vlan_config_cmds = [vlan_number_cli, vlan_name_cli]
device.send_config_set(vlan_config_cmds)
device.exit_config_mode()

# -- Add the new VLAN to the trunks on the switch
vlan_trunk_cli = "switchport trunk allow vlan add " + vlan_number
device.config_mode()
device.find_prompt()
trunk_config_cmds = ['interface range po152, po255, po251', vlan_trunk_cli]
device.send_config_set(trunk_config_cmds)
device.exit_config_mode()
device.disconnect()

print "Complete!"
print ""