必須更新需要使用 Azure 監視代理程式的現有虛擬機器和虛擬機器擴展集,才能使用使用者指派的受控識別。 本文說明指派自訂定義所需的步驟,以透過 Azure 原則大規模將使用者指派的身分識別新增至這些資源。
注意
「必須」指派已停用強制模式 (DoNotEnforce) 的定義範本,以防止新建立的資源發生失敗。
必要條件
- 建立使用者指派的身分識別,其具有在資源內安裝 Azure 監視代理程式所需的權限。
建立、指派及補救原則定義
使用入口網站
若要補救現有的資源,請遵循下列步驟:
藉由按一下 [所有服務] 然後搜尋並選取 [原則],在 Azure 入口網站中啟動 Azure 原則服務。
選取 Azure 原則分頁左側的 [定義]。
使用 [+原則定義] 按鈕來建立自訂原則定義。
在索引標籤上,設定下列選項:
- 定義位置:設定為目標範圍。
- 名稱:設定為自訂定義的名稱。 範例:「修改現有 VM 和 VMSS 上的身分識別 [ASSIGN TO DO NOT ENFORCE]」
在 [原則規則] json 區塊中,移除範例 JSON,並貼上下列使用
modify
效果來新增使用者指派身分識別的定義:{ "mode": "Indexed", "parameters": { "userAssignedIdentities": { "type": "String", "metadata": { "displayName": "userAssignedIdentities" } } }, "policyRule": { "if": { "allOf": [ { "field": "type", "in": [ "Microsoft.Compute/virtualMachines", "Microsoft.Compute/virtualMachineScaleSets" ] }, { "value": "[requestContext().apiVersion]", "greaterOrEquals": "2018-10-01" }, { "field": "identity.userAssignedIdentities", "notContainsKey": "[parameters('userAssignedIdentities')]" } ] }, "then": { "effect": "modify", "details": { "roleDefinitionIds": [ "/providers/microsoft.authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c" ], "operations": [ { "operation": "AddOrReplace", "field": "identity.type", "value": "[if(contains(field('identity.type'), 'SystemAssigned'), 'SystemAssigned,UserAssigned', 'UserAssigned')]" }, { "operation": "addOrReplace", "field": "identity.userAssignedIdentities", "value": "[createObject(parameters('userAssignedIdentities'), createObject())]" } ] } } } }
選取 [儲存]。 成功建立自訂原則定義之後,就會填入定義檢視。 選取 [指派] 按鈕,或瀏覽至 [指派] 索引標籤以指派定義。
確定 [範圍] 和 [基本] 中的資訊已如預期進行設定。
將 [原則強制執行] 設定為 [已停用]。 EnforcementMode 會在資源建立或更新時間停用任何強制執行。 深入了解強制模式。
注意
「必須」指派已停用強制模式 (DoNotEnforce) 的定義範本,以防止新建立的資源發生失敗。
選取 [參數] 索引標籤。參數
userAssignedIdentities
預期現有使用者指派的身分識別識別碼具有虛擬機器或虛擬機器擴展集的適當權限。 應以下欄格式輸入識別碼:/subscriptions/subID/resourceGroups/RGName/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testUAMI
選取 [補救] 索引標籤,核取 [建立補救工作] 方塊,以便補救任何現有的虛擬機器和虛擬機器擴展集。 針對管理群組層級的指派,請遵循補救資源教學課程,即可在建立指派之後觸發補救。
請確定受控識別中的資訊如預期一般。
選取 [檢閱 + 建立]。 您的自訂定義已指派。 針對訂用帳戶層級或以下層級的指派,現有的虛擬機器和虛擬機器擴展集會透過原則補救工作來進行補救。 遵循補救資源教學課程,必須任何未來補救管理群組層級的虛擬機器和虛擬機器擴展集或指派。
使用 PowerShell
使用下列 JSON 程式碼片段建立名稱為 ModifyVMIdentities.json 的 JSON 檔案。
{ "mode": "Indexed", "parameters": { "userAssignedIdentities": { "type": "String", "metadata": { "displayName": "userAssignedIdentities" } } }, "policyRule": { "if": { "allOf": [ { "field": "type", "in": [ "Microsoft.Compute/virtualMachines", "Microsoft.Compute/virtualMachineScaleSets" ] }, { "value": "[requestContext().apiVersion]", "greaterOrEquals": "2018-10-01" }, { "field": "identity.userAssignedIdentities", "notContainsKey": "[parameters('userAssignedIdentities')]" } ] }, "then": { "effect": "modify", "details": { "roleDefinitionIds": [ "/providers/microsoft.authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c" ], "operations": [ { "operation": "AddOrReplace", "field": "identity.type", "value": "[if(contains(field('identity.type'), 'SystemAssigned'), 'SystemAssigned,UserAssigned', 'UserAssigned')]" }, { "operation": "addOrReplace", "field": "identity.userAssignedIdentities", "value": "[createObject(parameters('userAssignedIdentities'), createObject())]" } ] } } } }
如需撰寫原則定義的詳細資訊,請參閱 Azure 原則定義結構。
使用 ModifyVMIdentities.json 檔案,執行下列命令來建立原則定義。
New-AzPolicyDefinition -Name 'ModifyVMIdentities' -DisplayName 'Modify identities on existing VMs and VMSS' -Policy 'ModifyVMIdentities.json'
此命令會建立名為修改現有 VM 和 VMSS 上的身分識別的原則定義。
若在未指定位置參數的情況下呼叫,
New-AzPolicyDefinition
會預設儲存工作階段內容中所選訂用帳戶的原則定義。 若要將定義儲存至不同位置,請使用下列參數:- SubscriptionId - 儲存到不同的訂用帳戶。 需要 GUID 值。
- ManagementGroupName - 儲存至管理群組。 需要字串值。
建立原則定義之後,您可以執行下列命令來建立原則指派:
$Subscription = Get-AzSubscription -SubscriptionName 'Subscription01' $Policy = Get-AzPolicyDefinition -Name 'ModifyVMIdentities' $VMuserassignedidentity = Get-AzUserAssignedIdentity -ResourceGroupName 'NMidentityRG' -Name $VMuserassignedidentityname $VMuserassignedidentityid = $VMuserassignedidentity.Id New-AzPolicyAssignment -Name 'ModifyVMIdentities' -PolicyDefinition $Policy -Scope "/subscriptions/$($Subscription.Id)" -EnforcementMode DoNotEnforce -Location 'westus' -IdentityType "SystemAssigned" -PolicyParameterObject $VMuserassignedidentityid
注意
「必須」指派已停用強制模式 (DoNotEnforce) 的定義,以防止新建立的資源發生失敗。
以您想要的資源群組名稱取代 Subscription01。
New-AzPolicyAssignment
上的 Scope 參數可與管理群組、訂用帳戶、資源群組或單一資源搭配使用。 此參數會使用Get-AzResourceGroup
上 ResourceId 屬性傳回的完整資源路徑。 以下是每個容器的 Scope 模式。 請將{rName}
、{rgName}
、{subId}
和{mgName}
分別取代為您的資源名稱、資源群組名稱、訂用帳戶 ID 及管理群組名稱。{rType}
會取代為資源的資源類型,例如,如果是 VM,則為Microsoft.Compute/virtualMachines
。- 資源 -
/subscriptions/{subID}/resourceGroups/{rgName}/providers/{rType}/{rName}
- 資源群組 -
/subscriptions/{subId}/resourceGroups/{rgName}
- 訂用帳戶 -
/subscriptions/{subId}
- 管理群組 -
/providers/Microsoft.Management/managementGroups/{mgName}
- 資源 -
建立原則指派之後,您即可執行下列命令,建立將身分識別新增至現有虛擬機器和虛擬機器擴展集資源的補救工作:
Start-AzPolicyRemediation -Name 'remediationVMidentities' -PolicyAssignmentId '/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/policyAssignments/ModifyVMIdentities'
下一步
- 了解補救工作結構。
- 檢閱了解原則效果。
- 了解如何補救不符合規範的資源。
- 深入了解強制模式
- 深入了解如何使用 Azure 原則安裝 Azure 監視器代理程式