適用於:✔️ Linux 虛擬機 ✔️ 彈性規模設定
當您註冊 Azure VM 映像產生器時,這會授與服務建立、管理和刪除預備資源群組的權限。 此服務也有權將資源新增至映射組建所需的資源群組。 在成功註冊期間,您的訂用帳戶會存取 VM Image Builder 服務主體名稱 (SPN)。
備註
如果您使用自訂階段資源群組,使用者指派的受控識別也需要具備此資源群組的參與者權限,以便能在程序期間部署範本化 VM。
如果您想要 VM 映像產生器散發映像,您必須在 Azure 中建立使用者指派的身分識別,並具有讀取和寫入映像的許可權。 舉例來說,您可能會想要將映像發佈至受控映像或 Azure Compute Gallery。 如果您要存取 Azure 儲存體,則您建立的使用者指派身分識別需要讀取私人或公用容器的許可權。
您必須先設定許可權和權限,才能建置映像。 下列各節詳細說明如何使用 PowerShell 設定可能的案例。
建立使用者指派的管理型身分識別
VM 映像產生器需要您建立 Azure 使用者指派的受控識別。 VM 映像產生器會使用此身分識別來讀取映像、寫入映像,以及存取 Azure 儲存體帳戶。 您授與身分識別許可權,以在訂用帳戶中執行特定動作。
備註
使用者指派的受控識別是授與映像資源群組許可權的正確方式。 因此,SPN 已遭淘汰。
下列範例示範如何建立 Azure 使用者指派的受控識別。 將預留位置設定以您的變數取代。
| Setting | Description |
|---|---|
| <資源群組> | 您想要建立使用者指派的系統管理身分所在的資源群組。 |
## Add AZ PS module to support AzUserAssignedIdentity
Install-Module -Name Az.ManagedServiceIdentity
$parameters = @{
Name = 'aibIdentity'
ResourceGroupName = '<Resource group>'
}
# create identity
New-AzUserAssignedIdentity @parameters
如需詳細資訊,請參閱 Azure 使用者指派的受控識別。
允許 VM Image Builder 散發映像
若要讓 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
自訂現有影像的權限
若要讓 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 Image Builder 從資源群組 example-rg-1 讀取映像,並將映像寫入資源群組 example-rg-2。
Azure 自訂映像角色範例
下列範例會建立 Azure 角色,以使用和散發來源自訂映像。 您接著會將自訂角色授與 VM Image Builder 的使用者指派受控識別。
若要簡化範例中值的取代,請先設定下列變數。 將預留位置設定以您的變數取代。
| Setting | Description |
|---|---|
| <訂閱標識碼> | 您的 Azure 訂用帳戶識別碼。 |
| <資源群組> | 自訂映像的資源群組。 |
$sub_id = "<Subscription ID>"
# 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 a web request to download the sample JSON description
$sample_uri="https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json"
$role_definition="aibRoleImageCreation.json"
Invoke-WebRequest -Uri $sample_uri -Outfile $role_definition -UseBasicParsing
# Create a unique role name to avoid clashes in the same Azure Active Directory domain
$timeInt=$(get-date -UFormat "%s")
$imageRoleDefName="Azure Image Builder Image Def"+$timeInt
# Update the JSON definition placeholders with variable values
((Get-Content -path $role_definition -Raw) -replace '<subscriptionID>',$sub_id) | Set-Content -Path $role_definition
((Get-Content -path $role_definition -Raw) -replace '<rgName>', $imageResourceGroup) | Set-Content -Path $role_definition
((Get-Content -path $role_definition -Raw) -replace 'Azure Image Builder Service Image Creation Role', $imageRoleDefName) | Set-Content -Path $role_definition
# Create a custom role from the aibRoleImageCreation.json description file.
New-AzRoleDefinition -InputFile $role_definition
# Get the user-identity properties
$identityNameResourceId=$(Get-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $identityName).Id
$identityNamePrincipalId=$(Get-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $identityName).PrincipalId
# Grant the custom role to the user-assigned managed identity for Azure Image Builder.
$parameters = @{
ObjectId = $identityNamePrincipalId
RoleDefinitionName = $imageRoleDefName
Scope = '/subscriptions/' + $sub_id + '/resourceGroups/' + $imageResourceGroup
}
New-AzRoleAssignment @parameters
現有的虛擬網路 Azure 角色範例
下列範例將創建一個 Azure 角色,用來使用並分發現有的虛擬網路映像。 您接著會將自訂角色授與 VM Image Builder 的使用者指派受控識別。
若要簡化範例中值的取代,請先設定下列變數。 替換佔位符設定來設定變數。
| Setting | Description |
|---|---|
| <訂閱標識碼> | 您的 Azure 訂用帳戶識別碼。 |
| <資源群組> | 虛擬網路資源群組。 |
$sub_id = "<Subscription ID>"
$res_group = "<Resource group>"
$identityName = "aibIdentity"
# Use a web request to download the sample JSON description
$sample_uri="https://raw.githubusercontent.com/azure/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleNetworking.json"
$role_definition="aibRoleNetworking.json"
Invoke-WebRequest -Uri $sample_uri -Outfile $role_definition -UseBasicParsing
# Create a unique role name to avoid clashes in the same AAD domain
$timeInt=$(get-date -UFormat "%s")
$networkRoleDefName="Azure Image Builder Network Def"+$timeInt
# Update the JSON definition placeholders with variable values
((Get-Content -path $role_definition -Raw) -replace '<subscriptionID>',$sub_id) | Set-Content -Path $role_definition
((Get-Content -path $role_definition -Raw) -replace '<vnetRgName>', $res_group) | Set-Content -Path $role_definition
((Get-Content -path $role_definition -Raw) -replace 'Azure Image Builder Service Networking Role',$networkRoleDefName) | Set-Content -Path $role_definition
# Create a custom role from the aibRoleNetworking.json description file
New-AzRoleDefinition -InputFile $role_definition
# Get the user-identity properties
$identityNameResourceId=$(Get-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $identityName).Id
$identityNamePrincipalId=$(Get-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $identityName).PrincipalId
# Assign the custom role to the user-assigned managed identity for Azure Image Builder
$parameters = @{
ObjectId = $identityNamePrincipalId
RoleDefinitionName = $networkRoleDefName
Scope = '/subscriptions/' + $sub_id + '/resourceGroups/' + $res_group
}
New-AzRoleAssignment @parameters