此脚本从 OS 磁盘的快照创建虚拟机。
若要运行此示例,请安装最新版本的 Azure CLI。 若要开始,请运行 az login
以创建与 Azure 的连接。
适用于 Azure CLI 的示例是针对 bash
shell 编写的。 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。
如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户。
示例脚本
# Verified per Raman Kumar as of 2/23/2022
# <FullScript>
#Provide the subscription Id of the subscription where you want to create Managed Disks
subscriptionId="<subscriptionId>"
#Provide the name of your resource group
resourceGroupName=myResourceGroupName
#Provide the name of the snapshot that will be used to create Managed Disks
snapshotName=mySnapshotName
#Provide the name of the Managed Disk
osDiskName=myOSDiskName
#Provide the size of the disks in GB. It should be greater than the VHD file size.
diskSize=128
#Provide the storage type for Managed Disk. Premium_LRS or Standard_LRS.
storageType=Premium_LRS
#Provide the OS type
osType=linux
#Provide the name of the virtual machine
virtualMachineName=myVirtualMachineName
#Set the context to the subscription Id where Managed Disk will be created
az account set --subscription $subscriptionId
#Get the snapshot Id
snapshotId=$(az snapshot show --name $snapshotName --resource-group $resourceGroupName --query [id] -o tsv)
#Create a new Managed Disks using the snapshot Id
az disk create --resource-group $resourceGroupName --name $osDiskName --sku $storageType --size-gb $diskSize --source $snapshotId
#Create VM by attaching created managed disks as OS
az vm create --name $virtualMachineName --resource-group $resourceGroupName --attach-os-disk $osDiskName --os-type $osType
# </FullScript>
清理部署
运行以下命令来删除资源组、VM 和所有相关资源。
az group delete --name myResourceGroup
脚本说明
此脚本使用以下命令创建托管磁盘、虚拟机和所有相关资源。 表中的每条命令均链接到特定于命令的文档。
指令 | 注释 |
---|---|
az snapshot show (显示快照) | 使用快照名称和资源组名称获取快照。 返回对象的 ID 属性用于创建托管磁盘。 |
az disk create (创建磁盘) | 使用快照 ID、磁盘名称、存储类型和大小从快照创建托管磁盘 |
az vm create | 使用托管 OS 磁盘创建 VM |
后续步骤
有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档。
可以在 Azure Linux VM 文档中找到其他虚拟机 CLI 脚本示例。