lunedì 24 febbraio 2014

VMware: VSAN Part4 - Automate VSAN using PowerCLI

VSAN deployment can be automated using PowerCLI. PowerCLI Extensions must be installed in order to add VSAN & vFRC cmdlets to PowerCLI.

As explained in Automating vFRC deployment with PowerCLI  post a few steps are required in order to register the VMware.VimAutomation.Extensions module.

After that several new cmdlets become available:


The following script allows you to automate the creation of a VSAN enabled cluster in just one click.

Here the steps performed by the script:

-Import VSAN cmdlets in PowerCLI session in order to use new cmdlets.
-Connect to vCenter Server.
-Create a Datacenter.
-Create a VSAN enabled Cluster.
-Insert and assign license to VSAN cluster (this is optional but in my vLab I was not able to claim VSAN disks without prior licensing VSAN solution).
-Add all hosts participating in VSAN cluster.
-Add a VSAN vmkernel to each host vSwitch.

Prior launching PowerCLI script make sure you correctly set required variables.

Here is the script, I've also added it on my GitHub repository:

Download Automating VSAN.ps1 from GitHub

 #Registering VSAN PowerCLI module  
 $p = [Environment]::GetEnvironmentVariable("PSModulePath")   
 echo $p #Show your current path to modules   
 $p += ";C:\Users\Paolo\WindowsPowerShell\Modules" #Add your custom location for modules   
 #Variable declaration  
 $VSANHosts= @("","","") #IP or FQDN of hosts participating in VSAN cluster  
 $vSwitchName="vSwitch0" #vSwitch on which create VSAN enabled vmkernel  
 $VSANvmkernelIP= @("","","") #IP for VSAN enabled vmkernel  
 $VSANvmkernelSubnetMask="" #Subnet Mask for VSAN enabled vmkernel  
 $vsanLicense="XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" #VSAN License code  
 Write-Host "Importing PowerCLI VSAN cmdlets" -foregroundcolor "magenta"   
 Import-Module VMware.VimAutomation.Extensions  
 Write-Host "Connecting to vCenter" -foregroundcolor "magenta"   
 Connect-VIServer -Server $vCenterIPorFQDN -User $vCenterUsername -Password $vCenterPassword  
 Write-Host "Creating Folder" -foregroundcolor "magenta"   
 Get-Folder -NoRecursion | New-Folder -Name $DatacenterFolder  
 Write-Host "Creating Datacenter and Cluster" -foregroundcolor "magenta"   
 New-Cluster -Location (   
 New-Datacenter -Location $DatacenterFolder -Name $DatacenterName   
 ) -Name $ClusterName -VsanEnabled:$true -VsanDiskClaimMode Automatic  
 $i = 0 #Initialize loop variable  
 Write-Host "Licensing VSAN cluster" -foregroundcolor "magenta"  
 #Credits to Mike Laverick -  
 $datacenterMoRef = (Get-Cluster -Name NewCluster | get-view).MoRef  
 $serviceinstance = Get-View ServiceInstance  
 $LicManView=Get-View $LicManRef  
 $licenseassetmanager = Get-View $LicManView.LicenseAssignmentManager  
 $licenseassetmanager.UpdateAssignedLicense($datacenterMoRef.value,$vsanLicense,"Virtual SAN 5.5 Advanced")  
 foreach ($element in $VSANHosts) {  
      Write-Host "Adding" $element "to Cluster" -foregroundcolor "magenta"   
      Add-VMHost $element -Location $ClusterName -User $HostUsername -Password $HostPassword -RunAsync -force:$true   
      Write-Host "One minute sleep in order to register" $element "into the cluster" -foregroundcolor "magenta"  
      Start-Sleep -s 60  
      Write-Host "Enabling VSAN vmkernel on" $element "host" -foregroundcolor "magenta"  
      if ($i -le $VSANHosts.Length) {  
           New-VMHostNetworkAdapter -VMHost (Get-VMHost -Name $element) -PortGroup VSAN -VirtualSwitch $vSwitchName -IP $VSANvmkernelIP[$i] -SubnetMask $VSANvmkernelSubnetMask -VsanTrafficEnabled:$true  

Other blog posts in VSAN Series:

VSAN Part1 - Introduction
VSAN Part2 - Initial Setup
VSAN Part3 - Storage Policies
VSAN Part4 - Automate VSAN using PowerCLI

Nessun commento:

Posta un commento