profile for Gajendra D Ambi on Stack Exchange, a network of free, community-driven Q&A sites

Friday, December 1, 2017

Automating mapping of vlans to vnic templates on cisco UCS

If you haven't read my article on Automating adding of vlans to UCS then please do. This is a continuation of that. You will also be able to understand this better if you have gone through that. Some of the explanation might be repetitive but please hold on. Repetition helps you to retain it better. So here we go. I am writing this to enable my nearly 90 solutions architects for whom I do automation on cisco, emc and vmware products to understand automation and logic behind it. make them realize that it is easier than they think.
Challenge :
Map 500 vlans to vnicTemplate-A
Map 500 vlans to vnicTemplate-B
So in total 1000 manual tasks to do.
Here is the powertool command for mapping vlans to vnic templates.


1
Get-UcsOrg -Level root  | Get-UcsVnicTemplate -Name $vnicTemplate | Add-UcsVnicInterface -ModifyPresent -DefaultNet "false" -Name $vlan 

Now we need to loop it against all 500*2 templates.
Assuming that we are connected to UCS already. Let us begin.


1
2
3
4
5
6
    Write-Host "
    A CSV file will be opened (open in excel/spreadsheet)
    populate the values,
    save & close the file,
    Hit Enter to proceed
    " -ForegroundColor Blue -BackgroundColor White

The above 6 lines will just tell the user on what to do. Now we will use a csv file to take the input and use that csv file to map vlans to vnics.


1
$csv = "$PSScriptRoot/ucsVlanVnicMap.csv"

Now here $PSScriptRoot means that the path for our csv file will be the current working directory or wherever this script is currently stored. Then we are giving a name to the csv file.


1
2
3
get-process | Select-Object vnicTemplate,Vlan | Export-Csv -Path $csv -Encoding ASCII -NoTypeInformation
    Start-Process $csv
    Read-Host "Hit Enter/Return to proceed"

line 1 --> exports a csv file with vnicTemplate,Vlan as the name of the columns.
line 2 --> it will start the csv file and opens it up for the user.
line 3 --> This is to pause the script till the csv file is populated with all the values and saved. Once done just hit enter.

1
2
3
4
5
    $csv = Import-Csv $csv
     foreach ($line in $csv) {
      $vnicTemplate = $($line.vnicTemplate)
      $Vlan  = $($line.Vlan)
}

line1 --> imports the csv file's data and stores all that data into a variable's name called csv.
line2 --> processes each line in the csv file
line3 --> in the line $line from the column named vnicTemplate it takes the value and stores into a variable called $vnicTemplate
line4 --> in the line $line from the column named Vlan it takes the value and stores into a variable called $Vlan
for the 1st line when we create these variables we want to use these inside the powertool oneliner script to map this vlan with this vnicTemplate.


1
2
3
4
5
6
    $csv = Import-Csv $csv
     foreach ($line in $csv) {
      $vnicTemplate = $($line.vnicTemplate)
      $Vlan  = $($line.Vlan)
      Get-UcsOrg -Level root  | Get-UcsVnicTemplate -Name $vnicTemplate | Add-UcsVnicInterface -ModifyPresent -DefaultNet "false" -Name $vlan 
     }

line 6--> uses the variables created from the line 3,4 and maps the vlan from 4 to vnic template from line 3.
Now I leave the combining all this into what you want. Have fun.

No comments:

Post a Comment