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:

Get-VsanDisk  
Get-VsanDiskGroup  
New-VsanDisk  
New-VsanDiskGroup  
Remove-VsanDisk  
Remove-VsanDiskGroup  

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   
 [Environment]::SetEnvironmentVariable("PSModulePath",$p)  
 #Variable declaration  
 $vCenterIPorFQDN="192.168.243.40"  
 $vCenterUsername="Administrator@vsphere.local"  
 $vCenterPassword="vmware"  
 $DatacenterFolder="DCFolder"  
 $DatacenterName="VSANDC"  
 $ClusterName="NewCluster"  
 $VSANHosts= @("192.168.243.137","192.168.243.142","192.168.243.141") #IP or FQDN of hosts participating in VSAN cluster  
 $HostUsername="root"  
 $HostPassword="mypassword"  
 $vSwitchName="vSwitch0" #vSwitch on which create VSAN enabled vmkernel  
 $VSANvmkernelIP= @("10.24.45.1","10.24.45.2","10.24.45.3") #IP for VSAN enabled vmkernel  
 $VSANvmkernelSubnetMask="255.255.255.0" #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 - http://www.mikelaverick.com/2013/11/back-to-basics-post-configuration-of-vcenter-5-5-install-powercli/  
 $datacenterMoRef = (Get-Cluster -Name NewCluster | get-view).MoRef  
 $serviceinstance = Get-View ServiceInstance  
 $LicManRef=$serviceinstance.Content.LicenseManager  
 $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  
      }       
      $i++  
 }  

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