The best way to solve this is to fix the underlying problem. Let's be clear: when everything is fine vCloud Director agent deployment works smooth but when something is not the host preparation may become quite a pita. A way to fix this is to proceed with a manual deployment of vCloud agent but, as you reckon, this is quite uncomfortable when dealing with a large number of ESXi hosts.
Today's post goal is to provide a PowerCLI script that allows you to automate the manual process of vCloud agent deployment as well as a mass agent upgrade.
The following script will:
-Connect to a vCenter Server
-Download to your local machine the specified vCloud agent version from vCloud Director virtual machine
-For each host on which you need to install vCloud agent script will:
-Place host into maintenance mode
-Enable SSH
-Check whether vCloud agent is installed or not. If already installed uninstall it first
-Transfer vCloud agent over to ESXi host using Putty Secure Copy
-Install vCloud agent
-Disable SSH
-Exit maintenance mode
Once vCloud agent is successfully installed ESXi host is seen as correctly prepared from vCloud Director.
This is the PowerCLI code. As always you can also find it in my GitHub repository. Automate vCloud Agent Deployment.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Variable declaration | |
$vCenterIPorFQDN="192.168.243.172" | |
$vCenterUsername="Administrator@vsphere.local" | |
$vCenterPassword="vmware" | |
$pathToVcloudAgent="/opt/vmware/vcloud-director/agent" #Path of vCloud Agent in vCloud Director virtual machine | |
$FileVcloudAgent="vcloudagent-esx51-5.1.0-799577.zip" #vCloud Agent file that will be installed | |
$downloadDestination="C:\Users\Paolo\Desktop" #Where to download vCloud Agent on local machine | |
$vCdVmName="vCloud Director" #Name of vCloud Director virtual machine | |
$vCdVmUsername="root" #vCloud Director virtual machine username | |
$vCdVmPassword="vmware" #vCloud Director virtual machine password | |
$InstallHosts= @("192.168.243.144") #IP or FQDN of ESXi hosts on which vcloud agent will be installed | |
$HostUsername="root" #This assumes every host in both clusters have same user/password | |
$HostPassword="mypassword" | |
$puttyScpLocation='C:\Users\Paolo\Downloads\pscp.exe' #Location of pscp.exe (Putty Secure Copy) executable - Please preserve single quotes | |
Write-Host "Connecting to vCenter" -foregroundcolor "magenta" | |
Connect-VIServer -Server $vCenterIPorFQDN -Username $vCenterUsername -Password $vCenterPassword | |
#Get new vCloud agent downloading it from the vCloud Director VM | |
Write-Host "Downloading" $FileVcloudAgent "to your local machine in" $downloadDestination "directory" -foregroundcolor "magenta" | |
Copy-VMGuestFile -Source "$($pathToVcloudAgent)/$($FileVcloudAgent)" -Destination $downloadDestination -VM $vCdVmName -GuestToLocal -GuestUser $vCdVmUsername -GuestPassword $vCdVmPassword | |
foreach ($element in $InstallHosts) { | |
#Set Host into Maintenance Mode | |
Write-Host "Entering maintenance mode" -foregroundcolor "magenta" | |
Get-VMHost -Name $element | Set-VMHost -State Maintenance | |
#Start SSH service on host in order to allow the copy of vcloud agent using Secure Copy | |
Start-VMHostService -HostService (Get-VMHostService -VMHost $element | Where-Object Label -eq "SSH") -Confirm:$false | |
#Check whether vcloud agent is installed or not | |
$esx=Get-EsxCli -VMHost $element | |
$isAlreadeyInstalled = $esx.software.vib.list($true) | Where-Object Name -like "vcloud-agent" | Select-Object Version | |
if ($isAlreadeyInstalled.Version -eq $null) { #No previous agent on host. | |
Write-Host "No vCloud agent found, proceeding to installation" -foregroundcolor "magenta" | |
}else{ #Uninstall previous agent | |
Write-Host "vCloud agent found. Let me uninstall it before proceeding" -foregroundcolor "magenta" | |
$esx.software.vib.remove($false,$false,$true,$false,"vcloud-agent") | |
} | |
#Transfer vcloud agent over to ESXi host using Putty Secure Copy | |
Write-Host "Copying vCloud agent over to ESXi host" -foregroundcolor "magenta" | |
& $puttyScpLocation -pw $HostPassword "$($downloadDestination)\$($FileVcloudAgent)" "$($HostUsername)@$($element):/tmp" | |
#Install vcloud agent | |
Write-Host "Installing vCloud agent" -foregroundcolor "magenta" | |
$esx.software.vib.install("/tmp/$($FileVcloudAgent)", $false, $false, $true, $false, $false) | |
#Stop SSH service on host | |
Stop-VMHostService -HostService (Get-VMHostService -VMHost $element | Where-Object Label -eq "SSH") -Confirm:$false | |
#Exit Maintenance Mode | |
Write-Host "Exiting maintenance mode" -foregroundcolor "magenta" | |
Get-VMHost -Name $element | Set-VMHost -State Connected | |
} |
Code is quite well commented but let me spend a few words on some cmdlets.
Initial part comprises variable declaration. $FileVcloudAgent is the version of vCloud agent that will be downloaded from vCloud Director virtual machine and later installed on all specified ESXi hosts. In the script I use vcloudagent-esx51-5.1.0-799577.zip file. As name states this version is for ESXi 5.1. If you have different ESXi versions you MUST choose the appropriate ones, like vcloudagent-esx55-5.5.0-1280396.zip if you are using ESXi 5.5 (with vCloud Director 5.5) or vcloudagent-esx50-5.1.0-799574.zip in case of ESXi 5.0, etc.
$puttyScpLocation points to the location of Putty Secure Copy (pscp.exe) that can be downloaded from here and must be placed somewhere on your local machine. Putty Secure Copy is used to copy vCloud agent over to /tmp/ folder of each ESXi host.
Copy-VMGuestFile is a PowerCLI cmdlet used to download specified file from vCloud Director virtual machine to your local machine. -GuestToLocal parameter states that we are downloading files from a VM. Au contraire -LocalToGuest would have been used.
Crucial commands of script above are the VIB uninstall/install ones. To perform a VIB installation on an ESXi host you must use esxcli commands using the following syntax:
$esx=Get-EsxCli -VMHost <ESXi host on which use esxcli>
esxcli commands can be executed from PowerCLI by exposing the esxcli functionalities. This is done using:
$esx=Get-EsxCli -VMHost <ESXi host on which use esxcli>
and later calling esxcli installation command:
$esx.software.vib.install("/tmp/$($FileVcloudAgent)", $false, $false, $true, $false, $false)
On which each parameter has the following meaning:
Value:vim.EsxCLI.software.vib.install.InstallationResult
install(string[] depot, boolean dryrun, boolean force,
boolean maintenancemode, boolean noliveinstall, boolean
nosigcheck, string proxy, string[] vibname, string[]
viburl)
That's all!!
Nessun commento:
Posta un commento