使用 CLI 从通用化映像版本创建 VMCreate a VM from a generalized image version using the CLI

共享映像库中存储的通用化映像版本创建 VM。Create a VM from a generalized image version stored in a Shared Image Gallery. 若要使用专用化映像创建 VM,请参阅从专用化映像创建 VMIf you want to create a VM using a specialized image, see Create a VM from a specialized image.

获取映像 IDGet the image ID

使用 az sig image-definition list 列出库中的映像定义,以查看定义的名称和 ID。List the image definitions in a gallery using az sig image-definition list to see the name and ID of the definitions.

resourceGroup=myGalleryRG
gallery=myGallery
az sig image-definition list --resource-group $resourceGroup --gallery-name $gallery --query "[].[name, id]" --output tsv

创建 VMCreate the VM

运行 az vm create 创建 VM。Create a VM using az vm create. 若要使用最新版本的映像,请将 --image 设置为映像定义的 ID。To use the latest version of the image, set --image to the ID of the image definition.

在此示例中,请根据需要替换资源名称。Replace resource names as needed in this example.

imgDef="/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition"
vmResourceGroup=myResourceGroup
location=chinaeast
vmName=myVM
adminUsername=azureuser

az group create --name $vmResourceGroup --location $location

az vm create\
   --resource-group $vmResourceGroup \
   --name $vmName \
   --image $imgDef \
   --admin-username $adminUsername \
   --generate-ssh-keys

也可以通过使用 --image 参数的映像版本 ID 来使用特定版本。You can also use a specific version by using the image version ID for the --image parameter. 例如,若要使用映像版本 1.0.0,请键入:--image "/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0"For example, to use image version 1.0.0 type: --image "/subscriptions/<subscription ID where the gallery is located>/resourceGroups/myGalleryRG/providers/Microsoft.Compute/galleries/myGallery/images/myImageDefinition/versions/1.0.0".