Importing vSphere Networks into Aria Automation – Part 2 – Update Network IP Info
If you are standing up a new Aria Automation environment and have a large number of vSphere Networks (aka Port Groups), it could take a very long time to enter this information manually. I have written a few scripts that can make Importing vSphere networks into Aria Automation fairly easy!
This blog article is the 2nd in a 4-part series that discusses how you can update Network IP Information for vSphere networks discovered by Aria Automation. While Aria Automation will discover vSphere Port Groups for any vCenters added into a Cloud Account, it does not automatically fill out the various details about the IP Configuration.
Blog Series Overview – Importing vSphere Networks into Aria Automation
- Export Networks with Missing IP Info – This will export a file that can be used to determine which networks need updated. This will be the basis for the other 3 sections as an input file to target updates. This file will need updated with the pertinent IP information.
- Update Network IP Info – This script will use the file created in step 1 to update the IP Information for vSphere networks discovered by Aria Automation
- Update IP Ranges – This script will use the file created in step 1 and define IP Ranges for the subnets used by the discovered vSphere Networks
- Update Network Profiles – The final step is to take the results from the prior 3 steps and create Network Profiles that can be used by Aria Automation for workload deployment
Update Network IP Info
Aria Automation will discover vSphere Port Groups that exist in any Cloud Accounts that have been configured. The next step is to fill out the details for those various Networks:
- Default Gateway
- CIDR
- DNS Servers
- Search Domains
- Domain
This can be completed in definitions for the Networks in Cloud Assembly ( Infrastructure -> Resources -> Networks -> Networks. It can certainly be accomplished manually; but if the environment has a large number of Port Groups this can take time and be prone to error.
The prior step (Export Networks with Missing IP Info), identified the Networks that were missing the necessary details by exporting a file with those network names.
The final task of the prior step was to create an input file using those Network Names that will drive the rest of the process. This file should have the following column names (exactly as shown) and be saved as a CSV. This is a manual process and should be validated before proceeding.
- PortGroup
- Gateway
- SubnetMask
- NetworkAddress
- CIDR
- StartAddr
- LastAddr
With that file created and validated, the process can proceed with the next script which will use that information to update these network definitions in Aria Automation – Update-vRA-Networks-IP-Info.ps1.
The Script
Requirements
- Powershell – written and tested using 7.3.8
- PowervRA – This script was written and tested with Power vRA 6.0. Make sure to use PowervRA 4.x and above. Earlier versions were compatible with vRealize Automation 7.x. The APIs in Aria Automation 8.x are completely different
Usage
You will need to edit the script and enter a few details about your environment:
- $vRAServer – FQDN of the Aria Automation Server
- $vRAUser – name of the user used for authentication to Aria Automation
- $ImportFile – path the the CSV input file
- $RunLog – path the log file
- $DNS1 – Primary DNS Server
- $DNS2 – Secondary DNS Server
- $Domain – Network Domain
- $DNSSearch – DNS Search Domain
When the script is executed, it will attempt to update the Network definitions in Aria Automation for each line in the input file.
- It is assumed that all Networks with the same name use the same Network IP information. Every network with the same name will be updated with the same information – Even if they are in different Cloud Accounts
- All Data for networks from input file is overwritten
- Networks existing in Aria Automation but not in input file are not touched
Output
The script creates a verbose log file that can be used to understand what occurred during the run.
When you return to the Aria Automation GUI, you will find that the network info for these Networks have been updated.
Before
After
Getting the Code
TipsTest environments are your friend. Make sure your Aria Automation environment is backed up and take snapshots to save yourself time and agony
If you are new to using APIs with PowerShell, check out my article on Using Aria Automation APIs with PowerShell.
You can download the script from GitHub.
<# Update-vRA-Networks-CIDR.ps1 This script will update network information in vRA 8.x. It reads an input file containing a list of networks and the corresponding IP information. It will search vRA networks and update any matching networks that vRA has discovered. Disclaimer: This script was obtained from https://github.com/cybersylum * You are free to use or modify this code for your own purposes. * No warranty or support for this code is provided or implied. * Use this at your own risk. * Testing is highly recommended. #> ## ## Define Environment Variables ## $ImportFile = "/users/arronk/Downloads/vRA Network Import/network-ip-info.csv" #First row must by header which is used by script - it should be PortGroup,Gateway,SubnetMask,NetworkAddress,CIDR,2ndIP,End # hard-coded values that will be used for all Networks $DNS1 = "192.168.1.14" $DNS2 = "192.168.4.14" $DNSSearch = "yourdomain.com" $Domain = "yourdomain.com" $vRAUser = "[email protected]" $vRAServer = "vra8.domain.com" $DateStamp=Get-Date -format "yyyyMMdd" $TimeStamp=Get-Date -format "hhmmss" $RunLog = "Update-vRA-Networks-IP-Info-$DateStamp-$TimeStamp.log" $RateLimit=30 $RatePause=2 #QueryLimit is used to control the max rows returned by invoke-restmethod (which has a default of 100) $QueryLimit=9999 ## ## Function declarations ## function Write-Log { param ( $LogFile, $LogMessage ) # complex strings may require () around message paramter # Write-Log $RunLog ("Read " + $NetworkData.count + " records from $ImportFile. 1st Row is expected to be Column Names as defined in script.") $LogMessage | out-file -FilePath $LogFile -Append } function Get-Network-IP-Info { param ( $VLANname, $NetworkList ) $Value="NA" foreach ($net in $NetworkList) { if ($Net.PortGroup -eq $VLANname) { $Value = $net break } } return $Value } function Update-vRA-Network { param ( $NetworkID, $CIDR, $IPGateway ) #build JSON payload - seems to have syntax requirements to be at line position 1 $json = @" { "domain": "$Domain", "defaultGateway": "$IPGateway", "dnsServerAddresses": [ "$DNS1", "$DNS2" ], "dnsSearchDomains": [ "$DNSSearch" ], "cidr": "$CIDR" } "@ Try { $Results=Invoke-vRARestMethod -Method PATCH -URI "/iaas/api/fabric-networks-vsphere/$NetworkID" -Body $json Write-Log $RunLog $Results } catch { Write-Log $RunLog $(" Unable to update network - " + $NetworkID) Write-Log $RunLog $Error Write-Log $RunLog $Error[0].Exception.GetType().FullName } } ## ## Main Script ## $error.clear() # Load input file write-host "Reading input file for Portgroup IP Information" if (-not(Test-Path -Path $ImportFile -PathType Leaf)) { write-host "Input file '$ImportFile' not found..." exit } else { $NetworkData = import-csv $ImportFile Write-Log $RunLog ("Read " + $NetworkData.count + " records from $ImportFile. 1st Row is expected to be Column Names as defined in script.") } write-host "Connecting to Aria Automation - $vRAServer as $vRAUser" $vRA=connect-vraserver -server $vRAServer -Username "$vRAUser" -IgnoreCertRequirements if ($vRA -eq $null) { write-host "Unable to connect to vRA Server '$vRAServer'..." Write-Log $RunLog ("Unable to connect to vRA Server '$vRAServer'...") exit } #Grab the bearer token for use with invoke-restmethod $APItoken= $vRA.token | ConvertTo-SecureString -AsPlainText -Force # Get vRA-defined Networks (Resources -> Networks) and build lookup table Write-Host "Searching vRA for discovered networks" Write-Log $RunLog "Searching vRA for discovered networks" $Body = @{ '$top' = $QueryLimit } $APIparams = @{ Method = "GET" Uri = "https://$vRAServer/iaas/api/fabric-networks-vsphere" Authentication = "Bearer" Token = $APItoken Body = $Body } try{ $Networks = (Invoke-RestMethod @APIparams -SkipCertificateCheck).content } catch { Write-Log $RunLog $(" Unable to get networks from vRA") Write-Log $RunLog $Error Write-Log $RunLog $Error[0].Exception.GetType().FullName } write-host "Updating IP Info on each discovered network" Write-Log $RunLog "Updating IP Info on each discovered network" $Counter=0 foreach ($Network in $Networks) { $ThisNetworkInfo = Get-Network-IP-Info $Network.name $NetworkData If ($ThisNetworkInfo -eq "NA") { Write-Log $RunLog ("No Network IP information found in input file for " + $Network.name + "/" + $Network.id) } else { Write-Log $RunLog ("Network " + $ThisNetworkInfo.PortGroup + "/" + $Network.id + " has IP info available - attempting update...") Update-vRA-Network $Network.id $ThisNetworkInfo.CIDR $ThisNetworkInfo.Gateway #Rate Limit to avoid overload $Counter++ if ($Counter -gt $RateLimit) { sleep $RatePause $Counter=0 } } write-host -nonewline "." } # Clean up write-host Write-Host "More details available in the log - $RunLog" Disconnect-vRAServer -Confirm:$false
Next Steps
The next article in the series is Update IP Ranges on vSphere Networks. It will discuss update the IP Ranges defined in Aria Automation using this same input file.