Membuat mesin virtual dari snapshot dengan CLI

Script ini membuat mesin virtual dari snapshot disk OS.

Untuk menjalankan sampel ini, instal versi terbaru Azure CLI. Untuk memulai, jalankan az login guna membuat koneksi dengan Azure.

Sampel untuk Azure CLI ditulis untuk bash shell. Untuk menjalankan sampel ini di Windows PowerShell atau Perintah, Anda perlu mengubah elemen skrip.

Jika Anda tidak memiliki langganan Azure, buat akun gratis Azure sebelum memulai.

Skrip sampel

# 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>

Membersihkan penyebaran

Jalankan perintah berikut untuk menghapus grup sumber daya, VM, dan semua sumber daya terkait.

az group delete --name myResourceGroup

Penjelasan skrip

Script ini menggunakan perintah berikut untuk membuat disk terkelola, mesin virtual, dan semua sumber daya terkait. Setiap perintah dalam tabel ditautkan ke dokumentasi spesifik perintah.

Perintah Catatan
az snapshot show Dapatkan snapshot menggunakan nama snapshot dan nama grup sumber daya. Properti id dari objek yang dikembalikan digunakan untuk membuat disk terkelola.
az disk create Membuat disk terkelola dari snapshot menggunakan Id snapshot, nama disk, jenis penyimpanan, dan ukuran
membuat vm az Membuat Mesin Virtual menggunakan disk OS terkelola

Langkah berikutnya

Untuk informasi selengkapnya tentang Azure CLI, lihat Dokumentasi Azure CLI.

Sampel skrip CLI mesin virtual lainnya dapat ditemukan di dokumentasi Mesin Virtual Azure Linux.