从通用化映像版本创建 VM

从 Azure Compute Gallery(以前称为“共享映像库”)中存储的通用化映像版本创建 VM。 若要使用专用化映像创建 VM,请参阅从专用化映像创建 VM

本文介绍如何从通用映像创建 VM:

使用 az sig image-definition list 列出库中的映像定义,以查看定义的名称和 ID。

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

运行 az vm create 创建 VM。 若要使用最新版本的映像,请将 --image 设置为映像定义的 ID。

此示例演示如何创建一个使用 SSH 保护的 Linux VM。 对于 Windows 或要使用密码保护 Linux VM,请删除 --generate-ssh-keys,使系统提示输入密码。 如果要直接提供密码,请将 --generate-ssh-keys 替换为 --admin-password。 在此示例中,请根据需要替换资源名称。

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 来使用特定版本。 例如,若要使用映像版本 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"

RBAC - 在组织内共享

如果库所在的订阅位于同一租户中,则通过 RBAC 共享的映像可用于通过 CLI 和 PowerShell 创建 VM。

需要获取你要使用的映像的 imageID,并确保映像已复制到你要创建 VM 的区域。

请确保映像的状态为 Generalized。 如果要使用具有 Specialized 状态的映像,请参阅从专用映像版本创建 VM

imgDef="/SharedGalleries/1a2b3c4d-1234-abcd-1234-1a2b3c4d5e6f-MYDIRECTSHARED/Images/myDirectDefinition/Versions/latest"
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

RBAC - 从另一个租户共享

如果你要使用的映像存储在某个不在同一租户(目录)中的库中,则需登录到每个租户以验证你是否拥有访问权限。

还需要所要使用的映像的 imageID,并确保映像已复制到你要创建 VM 的区域。 你还需要用于源库的 tenantID,以及与用于创建 VM 的位置对应的 tenantID

此示例演示如何根据通用化映像创建 VM。 如果使用的是专用化映像,请参阅使用专用化映像版本创建 VM

你需要登录到存储映像的租户,获取访问令牌,然后登录到你要在其中创建 VM 的租户。 这是 Azure 验证你是否有权访问映像的方式。


tenant1='<ID for tenant 1>'
tenant2='<ID for tenant 2>'

az account clear
az cloud set -n AzureChinaCloud
az login --tenant $tenant1
az account get-access-token 
az login --tenant $tenant2
az account get-access-token

创建 VM。 请将示例中的信息替换为你自己的。 在创建 VM 之前,请确保将映像复制到你要在其中创建 VM 的区域。

imageid="<ID of the image that you want to use>"
resourcegroup="<name for the resource group>"
location="<location where the image is replicated>"
user='<username for the VM>'
name='<name for the VM>'

az group create --location $location --resource-group $resourcegroup
az vm create \
  --resource-group $resourcegroup \
  --name $name \
  --image $imageid \
  --admin-username $user \
  --generate-ssh-keys

后续步骤