使用 Azure CLI 配置 Azure VM 映像生成器权限

适用于:✔️ Linux VM ✔️ 灵活规模集

注册 Azure VM 映像生成器时,你的订阅可访问 VM 映像生成器服务主体名称 (SPN)。 此注册还会向该服务授予创建、管理和删除暂存资源组的权限。 对于映像生成过程,暂存资源组也需要分配“参与者”角色。

如果希望 VM 映像生成器分发映像,则需要在 Azure 中创建用户分配的标识,该标识具有读取和写入映像的权限。 例如,可能需要将映像分发到托管映像或 Azure Compute Gallery。 如果要访问 Azure 存储,则创建的用户分配标识需要读取专用容器或公共容器的权限。

必须先设置权限和特权才能生成映像。 以下部分详细介绍了如何使用 Azure CLI 配置可能的方案。

先决条件

  • 如需在本地运行 CLI 参考命令,请安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

创建用户分配的托管标识

VM 映像生成器要求创建 Azure 用户分配的托管标识。 VM 映像生成器使用该标识来读取映像、写入映像以及访问 Azure 存储帐户。 你授予标识在订阅中执行特定操作的权限。

注意

用户分配的托管标识是向映像资源组授予权限的正确方法。 为此,已弃用 SPN。

下面的示例演示如何创建 Azure 用户分配的托管标识。 替换占位符设置以设置变量。

设置 说明
<资源组> 要在其中创建用户分配的托管标识的资源组。
identityName="aibIdentity"
imageResourceGroup=<Resource group>

az identity create \
    --resource-group $imageResourceGroup \
    --name $identityName

有关详细信息,请参阅用户分配的托管标识

允许 VM 映像生成器分发映像

要使 VM 映像生成器能够分发映像,必须允许服务将映像注入资源组。 若要授予所需的权限,创建用户分配的托管标识,并授予其对生成映像的资源组的权限。 VM 映像生成器没有访问订阅中其他资源组中资源的权限。 需要执行显式操作以允许访问,从而防止生成失败。

无需向用户分配的托管标识参与者授予对资源组的权限就能分发映像。 但是,用户分配的托管标识需要在分发资源组中具有以下 Azure Actions 权限:

Microsoft.Compute/images/write
Microsoft.Compute/images/read
Microsoft.Compute/images/delete

如果要分发到 Azure Compute Gallery,还需要:

Microsoft.Compute/galleries/read
Microsoft.Compute/galleries/images/read
Microsoft.Compute/galleries/images/versions/read
Microsoft.Compute/galleries/images/versions/write

自定义现有映像的权限

要使 Azure VM 映像生成器从源自定义映像生成映像,必须允许服务将映像读取到这些资源组中。 若要授予所需的权限,创建用户分配的托管标识,并授予其对映像所在的资源组的权限。

下面介绍了如何从现有自定义映像生成:

Microsoft.Compute/images/read

下面介绍了如何从现有 Azure Compute Gallery 版本生成:

Microsoft.Compute/galleries/read
Microsoft.Compute/galleries/images/read
Microsoft.Compute/galleries/images/versions/read

用于自定义虚拟网络上的映像的权限

VM 映像生成器可以在订阅中部署和使用现有虚拟网络,从而允许自定义内容访问连接的资源。

无需向用户分配的托管标识参与者授予对资源组的权限就能将 VM 部署到现有的虚拟网络。 但是,用户分配的托管标识需要对虚拟网络资源组具有以下 Azure Actions 权限:

Microsoft.Network/virtualNetworks/read
Microsoft.Network/virtualNetworks/subnets/join/action

创建 Azure 角色定义

以下示例根据前面部分中所述的操作创建 Azure 角色定义。 这些示例在资源组级别应用。 评估并测试示例是否足以满足你的要求。

映像操作允许进行读写。 确定适合你的环境的操作。 例如,创建一个角色以允许 VM 映像生成器从资源组 example-rg-1 中读取映像并将相应映像写入资源组 example-rg-2。

自定义映像 Azure 角色示例

以下示例会创建一个 Azure 角色,以使用和分发源自定义映像。 然后,将自定义角色授给 VM 映像生成器的用户分配的托管标识。

若要简化示例中值的替换,请先设置以下变量。 替换占位符设置以设置变量。

设置 说明
<订阅 ID> Azure 订阅 ID。
<资源组> 自定义映像的资源组。
# Subscription ID - You can get this using `az account show | grep id` or from the Azure portal.
subscriptionID=$(az account show --query id --output tsv)
# Resource group - image builder will only support creating custom images in the same Resource Group as the source managed image.
imageResourceGroup=<Resource group>
identityName="aibIdentity"

# Use *cURL* to download the a sample JSON description 
curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json

# Create a unique role name to avoid clashes in the same Azure Active Directory domain
imageRoleDefName="Azure Image Builder Image Def"$(date +'%s')

# Update the JSON definition using stream editor
sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleImageCreation.json
sed -i -e "s/<rgName>/$imageResourceGroup/g" aibRoleImageCreation.json
sed -i -e "s/Azure Image Builder Service Image Creation Role/$imageRoleDefName/g" aibRoleImageCreation.json

# Create a custom role from the sample aibRoleImageCreation.json description file.
az role definition create --role-definition ./aibRoleImageCreation.json

# Get the user-assigned managed identity id
imgBuilderCliId=$(az identity show -g $imageResourceGroup -n $identityName --query clientId -o tsv)

# Grant the custom role to the user-assigned managed identity for Azure Image Builder.
az role assignment create \
    --assignee $imgBuilderCliId \
    --role $imageRoleDefName \
    --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup

现有虚拟网络 Azure 角色示例

以下示例会创建一个 Azure 角色,以使用和分发现有的虚拟网络映像。 然后,将自定义角色授给 VM 映像生成器的用户分配的托管标识。

若要简化示例中值的替换,请先设置以下变量。 替换占位符设置以设置变量。

设置 说明
<订阅 ID> Azure 订阅 ID。
<资源组> 虚拟网络资源组
# Subscription ID - You can get this using `az account show | grep id` or from the Azure portal.
subscriptionID=$(az account show --query id --output tsv)
VnetResourceGroup=<Resource group>
identityName="aibIdentity"

# Use *cURL* to download the a sample JSON description 
curl https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleNetworking.json -o aibRoleNetworking.json

# Create a unique role name to avoid clashes in the same domain
netRoleDefName="Azure Image Builder Network Def"$(date +'%s')

# Update the JSON definition using stream editor
sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleNetworking.json
sed -i -e "s/<vnetRgName>/$vnetRgName/g" aibRoleNetworking.json
sed -i -e "s/Azure Image Builder Service Networking Role/$netRoleDefName/g" aibRoleNetworking.json

# Create a custom role from the aibRoleNetworking.json description file.
az role definition create --role-definition ./aibRoleNetworking.json

# Get the user-assigned managed identity id
imgBuilderCliId=$(az identity show -g $imageResourceGroup -n $identityName --query clientId -o tsv)

# Grant the custom role to the user-assigned managed identity for Azure Image Builder.
az role assignment create \
    --assignee $imgBuilderCliId \
    --role $netRoleDefName \
    --scope /subscriptions/$subscriptionID/resourceGroups/$VnetResourceGroup

使用托管标识访问 Azure 存储

如果要向 Azure 存储进行身份验证并使用私有容器,VM 映像生成器需要一个由用户分配的托管标识。 VM 映像生成器使用该标识向 Azure 存储进行身份验证。

注意

VM 映像生成器仅在提交映像模板时使用标识。 编译 VM 在映像编译过程中无权访问标识。

使用 Azure CLI 创建用户分配的托管标识:

az role assignment create \
    --assignee <Image Builder client ID> \
    --role "Storage Blob Data Reader" \
    --scope /subscriptions/<Subscription ID>/resourceGroups/<Resource group>/providers/Microsoft.Storage/storageAccounts/$scriptStorageAcc/blobServices/default/containers/<Storage account container>

在 VM 映像生成器模板中,提供由用户分配的托管标识:

    "type": "Microsoft.VirtualMachineImages/imageTemplates",
    "apiVersion": "2020-02-14",
    "location": "<Region>",
    ..
    "identity": {
    "type": "UserAssigned",
          "userAssignedIdentities": {
            "<Image Builder ID>": {}     
        }

替换以下占位符设置:

设置 说明
<区域> 模板区域
<资源组> 资源组
<存储帐户容器> 存储帐户容器名称
<订阅 ID> Azure 订阅

有关详细信息,请参阅创建映像并使用用户分配的托管标识来访问 Azure 存储中的文件。 你将了解如何创建和配置用户分配的托管标识以访问存储帐户。

后续步骤

Azure VM 映像生成器概述