利用 Azure CLI 設定 Azure VM Image Builder 權限

適用於:✔️ Linux VM ✔️ 彈性擴展集

註冊 Azure VM Image Builder 時,您的訂用帳戶會取得 VM Image Builder 服務主體名稱 (SPN) 的存取權。 此註冊也會授與服務權限來建立、管理及刪除暫存資源群組。 對於映像編譯流程,暫存資源群組也需要有 [參與者] 角色指派。

如果您想要 VM Image Builder 發佈映像,您必須在 Azure 中建立使用者指派的身分識別,並具有讀取和寫入映像的權限。 舉例來說,您可能會想要將映像發佈至受控映像或 Azure Compute Gallery。 如果您要存取 Azure 儲存體,則您所建立的使用者指派身分識別需要讀取私人或公用容器的權限。

您必須先設定使用權限和存取權限後才能建置映像。 下列各節將詳細說明如何使用 Azure CLI 設定可能的案例。

必要條件

建立使用者指派的受控識別

VM Image Builder 需要您建立 Azure 使用者指派的受控識別。 VM Image Builder 會使用此身分識別來讀取映像、寫入映像,以及存取 Azure 儲存體帳戶。 您可以授與身分識別權限,以在您的訂閱中執行特定動作。

注意

使用者指派的受控識別是授與映像資源群組權限的正確方式。 因此,SPN 已遭淘汰。

下列範例示範如何建立 Azure 使用者指派的受控身分識別。 將預留位置設定以您的變數取代。

設定 描述
<資源群組> 要在其中建立使用者指派受控識別的資源群組。
identityName="aibIdentity"
imageResourceGroup=<Resource group>

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

如需詳細資訊,請參閱 Azure 使用者指派的受控識別

允許 Image Builder 發佈映像

若要讓 VM Image Builder 發佈映像,必須允許服務將映像插入資源群組。 若要授與所需的權限,請建立使用者指派的受控識別,並在映像組建所在的資源群組上授與其權限。 VM Image Builder 沒有可存取訂閱中其他資源群組中資源的權限。 您必須採取明確的動作來允許存取,以防止您的建置失敗。

您不需要在資源群組上授與使用者指派的受控識別參與者權限,也能散發映像。 不過,使用者指派的受控識別需要在散發資源群組中具有下列 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

自訂現有映像的權限

若要讓 VM Image Builder 從來源自定映像組建映像,則必須允許該服務將映像讀入這些資源群組中。 若要授與所需的權限,請建立使用者指派的受控識別,並在映像所在的資源群組上授與其權限。

從現有的自訂映像建置的方式如下:

Microsoft.Compute/images/read

從現有的 Azure Compute Gallery 版本建置的方法如下:

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

在虛擬網路上自訂映像的權限

VM Image Builder 具備在訂閱中部署和使用現有虛擬網路的功能,因此能允許自訂存取已連線的資源。

您不需要在資源群組上授與使用者指派的受控識別參與者權限,也能將 VM 部署到現存的虛擬網路上。 不過,使用者指派的受控識別需要在虛擬網路資源群組上具有下列 Azure Actions 權限:

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

建立 Azure 角色定義

下列範例會從上一節所述的動作建立 Azure 角色定義。 這些範例會在資源群組層級套用。 請評估及測試範例是否足以滿足您的需求。

映像動作允許讀取和寫入。 請決定適合您環境的內容。 例如,建立角色以允許 VM Image Builder 從資源群組 example-rg-1 讀取影像,並將映像寫入資源群組 example-rg-2

自訂映像 Azure 角色範例

下列範例會建立能使用和散發來源自訂映像的 Azure 角色。 您接著會將自訂角色授與 VM Image Builder 的使用者指派受控識別。

為了簡化取代範例中值的流程,請先設定下列變數。 將預留位置設定以您的變數取代。

設定 描述
<訂用帳戶識別碼> 您的 Azure 訂用帳戶識別碼。
<資源群組> 自訂映像的資源群組。
# 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 Image Builder 的使用者指派受控識別。

為了簡化取代範例中值的流程,請先設定下列變數。 將預留位置設定以您的變數取代。

設定 描述
<訂用帳戶識別碼> 您的 Azure 訂用帳戶識別碼。
<資源群組> 虛擬網路資源群組
# 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>/$VnetResourceGroup/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 Image Builder 需要使用者指派的受控識別。 VM Image Builder 會使用身分識別向 Azure 儲存體進行驗證。

注意

VM Image Builder 只會在您提交映像範本時使用身分識別。 組建 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 Image Builder 範本中,請提供使用者指派的受控識別:

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

取代下列預留位置設定:

設定 描述
<區域> 範本區域
<資源群組> 資源群組
<儲存體帳戶容器> 儲存體帳戶容器名稱
<訂用帳戶識別碼> Azure 訂用帳戶

如需詳細資訊,請參閱建立映像並以使用者指派的受控識別來存取 Azure 儲存體中的檔案。 您將了解如何建立和設定使用者指派的受控識別,藉此存取儲存體帳戶。

下一步

Azure Image Builder 概觀