给运维团队使用的虚拟机自动化部署工具

在日常VMware虚拟化平台运维中,有大量的虚拟机部署的需求,基于此需求,我使用Excel+PowerCLI做了一个可视化的VMware虚拟机自动化部署工具。

在开发或者使用该工具前,确保已完成如下工作:

1、创建各类型操作系统的虚拟机模板,并且并且模板中已安装最新版的VMware Tools。
2、建立针对Windows和Linux的虚拟机自定义规范:使用web client登陆VC,菜单->主页->策略和配置文件->虚拟机自定义规范,点击新建。我建立了两个,auto_win和auto_linux分别供Windows系统和Linux系统使用。两个规范中网络均使用DHCP,针对Windows的规范中工作组或域里输入域名、用户名以及密码。

(经过我测试,Windows 2008R2及以后版本均支持,包括Windows 2019;Linux支持RHEL6、RHEL7、Ubuntu 16.04,不支持Ubuntu 1804)

使用该工具能实现如下功能:

1、使用Excel实现可视化的参数输入,界面如下图:

2、各参数输入尽量使用下拉框实现,避免误操作,例如:

实现方法:在excel中,数据->数据验证->数据验证:

来源中填写公式:

=IF(A2="Test",OFFSET(Sheet2!$A$1,,,SUMPRODUCT(N(LEN(Sheet2!$A:$A)>0)),),OFFSET(Sheet2!$G$1,,,SUMPRODUCT(N(LEN(Sheet2!$G:$G)>0)),))

同时在Sheet2中的A列中将所有可供测试环境选择的参数填入各行,在G列中 将所有可供开发环境选择的参数填入各行 。如果还要支持更多的环境,就需要写嵌套的if语句了。

3、支持单次部署多台虚拟机,并支持多VC环境部署(我的代码里写入了我们这里开发和测试环境的VC信息),支持同时部署多种类型的操作系统。

4、点击“点击开始部署虚拟机按钮”之后开始批量化部署,Excel会调用D:\automation\VMDeployment.ps1脚本,按钮的源代码如下:

Private Sub CommandButton1_Click()
Dim retval
retval = Shell("powershell ""D:\automation\VMDeployment.ps1""", 1)

End Sub

5、部署完毕后,VC会根据我们输入的信息,自动完成虚拟机的主机名更改、IP、DNS、网关等网络配置、Windows系统的加域以及CPU、内存参数的设置。

以下为VMDeployment.ps1脚本的内容,连接VC的密码为隐藏,详细方法可参考之前的文章:

$encryptedText = "01000000d08c9ddf0115d1118c7a00c04fc297eb010000007e60d35c88a6754d97d685911ef7ce3a000000000200000000001066000000010000200000004f64c1c377a3d41cd7f5126ee11de07ade363c1fe75a7046e19f1c3564c2616a000000000e8000000002000020000000d89d6a13999fdae8f3aa94a3391e4577e7007a23058df3dd775baef097a5721d200000009f319764aaa0333a6ebbb535794d9193c02195bb710922bccb223cb4482fe48b40000000bf4e33bad709c942feeafdc5a1f3b254c17b3274212caa5219857f2621252d75f5e983bbf9afc92430cc120a2d585ab73b45943ea8c87e696039a70601d90fbd"

$encryptedPassword = $encryptedText | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential("JustGiveAName", $encryptedPassword)

Echo "Importing Excel file..."
$FilePath = "D:\automation\VMInput.xlsm"
$SheetName = "Sheet1"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$WorkBook = $objExcel.Workbooks.Open($FilePath)
$WorkSheet = $WorkBook.sheets.item($SheetName)

Echo "Deploying virtual machines..."
$k = 1
$endRows = $k + $WorkSheet.Range("M21").Text 

if($WorkSheet.Range("M21").Text -gt 0)
{
 for($i=2; $i -le $endRows; $i++)
 {
 $Environment = $WorkSheet.Range("A$i").Text
 $Hostname = $WorkSheet.Range("B$i").Text
 $IPAddr = $WorkSheet.Range("C$i").Text
 $Netmask = $WorkSheet.Range("D$i").Text
 $Gateway = $WorkSheet.Range("E$i").Text
 $DNSA = $WorkSheet.Range("F$i").Text
 $DNSB = $WorkSheet.Range("G$i").Text
 $PortGroupName = $WorkSheet.Range("H$i").Text
 $Storagelocation = $WorkSheet.Range("I$i").Text
 $CPUs = $WorkSheet.Range("J$i").Text
 $Memory = $WorkSheet.Range("K$i").Text
 $VMTemplate = $WorkSheet.Range("L$i").Text
 echo $Hostname, $IPAddr, $Netmask, $Gateway, $DNSA, $DNSB, $PortGroupName, $Storagelocation, $CPUs, $Memory, $VMTemplate

 switch($Environment)
 {
 "Test" {$vCenterServer = "192.168.102.51"; $vCenterUser = "test\vmadmin"; $ClusterName = "Test-Cluster"}
 "Dev" {$vCenterServer = "172.16.15.51"; $vCenterUser = "dev\vmadmin"; $ClusterName = "Dev-Cluster"}
 }
 
 switch($VMTemplate)
 {
 "auto-win2008r2-cn" {$OSType = "Windows"}
 "auto-win2016-cn" {$OSType = "Windows"}
 "auto-win2019-en" {$OSType = "Windows"}
 "auto-win2019-cn" {$OSType = "Windows"}
 "auto-rhel69-en" {$OSType = "Linux"}
 "auto-rhel73-en" {$OSType = "Linux"}
 "auto-ubuntu1804-en" {$OSType = "Linux"}
 "auto-ubuntu1604-en" {$OSType = "Linux"}
 }
 
 switch($OSType)
 {
 "Linux" {$basespec = "auto_linux"}
 "Windows" {$basespec = "auto_win"}
 }

 
 
 Echo "Input the credential for vCenter Server"
 Echo "Connecting to vCenter Server..."
 Connect-VIServer $vCenterServer -user $vCenterUser -password $cred.GetNetworkCredential().Password
 
 
 if($OSType -eq "Windows")
 {
 $SpecName = "TempSpec" + (Get-Random)
 Get-OSCustomizationSpec -Name $basespec |New-OSCustomizationSpec -Name $SpecName -Type NonPersistent
 Get-OSCustomizationSpec $SpecName |Get-OSCustomizationNicMapping | Remove-OSCustomizationNicMapping -Confirm:$false
 New-OSCustomizationNicMapping -Spec $specName -IpMode UseStaticIP -IpAddress $IPAddr -SubnetMask $Netmask -DefaultGateway $Gateway -DNS $DNSA,$DNSA2
 New-VM -Name $Hostname -ResourcePool $ClusterName -Datastore $Storagelocation -Template $VMTemplate 
 Get-VM -Name $Hostname | Set-VM -MemoryGB $Memory -NumCpu $CPUs -confirm:$false
 Get-VM -Name $Hostname | Get-NetworkAdapter |Set-NetworkAdapter -NetworkName $PortGroupName -Confirm:$false
 Get-VM -Name $Hostname| Get-NetworkAdapter|Set-NetworkAdapter -StartConnected $True -confirm:$false
 Get-VM -Name $Hostname | Set-VM -OSCustomizationSpec $specName -Confirm:$false |Start-VM
 Remove-OSCustomizationSpec $specName -Confirm:$false
 Disconnect-VIServer -Confirm:$false
 }
 else
 {
 $SpecName = "TempSpec" + (Get-Random)
 Get-OSCustomizationSpec -Name $basespec |New-OSCustomizationSpec -Name $SpecName -Type NonPersistent
 Get-OSCustomizationSpec $SpecName |Get-OSCustomizationNicMapping | Remove-OSCustomizationNicMapping -Confirm:$false
 New-OSCustomizationNicMapping -Spec $specName -IpMode UseStaticIP -IpAddress $IPAddr -SubnetMask $Netmask -DefaultGateway $Gateway 
 New-VM -Name $Hostname -ResourcePool $ClusterName -Datastore $Storagelocation -Template $VMTemplate 
 Get-VM -Name $Hostname | Set-VM -MemoryGB $Memory -NumCpu $CPUs -confirm:$false
 Get-VM -Name $Hostname | Get-NetworkAdapter |Set-NetworkAdapter -NetworkName $PortGroupName -Confirm:$false
 Get-VM -Name $Hostname| Get-NetworkAdapter|Set-NetworkAdapter -StartConnected $True -confirm:$false
 Get-VM -Name $Hostname | Set-VM -OSCustomizationSpec $specName -Confirm:$false 
 Get-VM -Name $Hostname| Get-NetworkAdapter|Set-NetworkAdapter -StartConnected $True -confirm:$false
 Get-VM -Name $Hostname | Start-VM 
 Remove-OSCustomizationSpec $specName -Confirm:$false
 Get-VM -Name $Hostname| Get-NetworkAdapter|Set-NetworkAdapter -Connected $True -confirm:$false
 Get-VM -Name $Hostname| Get-NetworkAdapter|Set-NetworkAdapter -StartConnected $True -confirm:$false
 Disconnect-VIServer -Confirm:$false
 }
 }

 $WorkBook.close()
 $objExcel.quit()
 get-process excel |stop-process

 Write-Host 'Deployment Finished, Press Any Key to Exit!' -NoNewline
 $null = [Console]::Readkey('?')
}

else
{
 Echo "No VM to deploy"
 $WorkBook.close()
 $objExcel.quit()
 get-process excel |stop-process

 Write-Host 'Press Any Key to Exit!' -NoNewline
 $null = [Console]::Readkey('?')
}

此条目发表在Powershell分类目录。将固定链接加入收藏夹。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注