使用範本在 Azure 虛擬機器規模上設定 Azure 資源的受控識別
Azure 資源的受控識別是 Microsoft Entra ID 的功能。 每個支援適用於 Azure 資源的受控識別 Azure 服務均受限於其本身的時間表。 開始之前,請務必檢閱 資源受控識別的可用性 狀態和 已知問題 。
Azure 資源受控識別會在 Microsoft Entra ID 中為 Azure 服務提供自動受控識別。 您可以使用此身分識別來向任何支援 Microsoft Entra 驗證的服務進行驗證,不需要任何您程式碼中的認證。
在本文中,您將瞭解如何使用 Azure Resource Manager 部署範本,針對 Azure 虛擬機器擴展集上的 Azure 資源作業執行下列受控識別:
- 在 Azure 虛擬機器擴展集上啟用和停用系統指派的受控識別
- 在 Azure 虛擬機器擴展集上新增和移除使用者指派的受控識別
必要條件
如果您不熟悉 Azure 資源的受控識別,請參閱概 觀一節 。 請務必檢閱 系統指派和使用者指派的受控識別 之間的差異。
如果您還沒有 Azure 帳戶, 請先註冊免費帳戶 ,再繼續進行。
若要執行本文中的管理作業,您的帳戶需要下列 Azure 角色型存取控制指派:
注意
不需要其他 Microsoft Entra 目錄角色指派。
- 用來建立虛擬機器擴展集的虛擬機器參與者 ,並從虛擬機器擴展集啟用和移除系統和/或使用者指派的受控識別。
- 受控識別參與者 角色,用來建立使用者指派的受控識別。
- 受控識別操作員 角色,可從虛擬機器擴展集指派和移除使用者指派的受控識別。
Azure 資源管理員範本
如同Azure 入口網站和腳本, Azure Resource Manager 範本可讓您部署 Azure 資源群組所定義的新或修改的資源。 有數個選項可用於範本編輯和部署,包括本機和入口網站型:
- 使用 Azure Marketplace 中的自訂範本,可讓您從頭開始建立範本,或以現有的通用或 快速入門範本 為基礎。
- 從原始部署 或部署 的目前狀態匯出範本,以衍生自 現有的資源群組。
- 使用本機 JSON 編輯器(例如 VS Code), 然後使用 PowerShell 或 CLI 上傳和部署。
- 使用 Visual Studio Azure 資源群組專案 來建立和部署範本。
無論您選擇的選項為何,在初始部署和重新部署期間,範本語法都相同。 在新的或現有的 VM 上啟用 Azure 資源的受控識別,會以相同方式完成。 此外,根據預設,Azure Resource Manager 會 對部署執行累加式更新 。
系統指派的受控識別
在本節中,您將使用 Azure Resource Manager 範本來啟用和停用系統指派的受控識別。
在建立虛擬機器擴展集或現有的虛擬機器擴展集期間啟用系統指派的受控識別
無論您是在本機或透過Azure 入口網站登入 Azure,請使用與包含虛擬機器擴展集之 Azure 訂用帳戶相關聯的帳戶。
若要啟用系統指派的受控識別,請將範本載入編輯器,在 resources 區段中找出
Microsoft.Compute/virtualMachinesScaleSets
感興趣的資源,並將 屬性新增identity
至與 屬性相同的層級"type": "Microsoft.Compute/virtualMachinesScaleSets"
。 使用下列語法:"identity": { "type": "SystemAssigned" }
當您完成時,下列各節應該新增至範本的資源區段,且應該類似以下所示的範例:
"resources": [ { //other resource provider properties... "apiVersion": "2018-06-01", "type": "Microsoft.Compute/virtualMachineScaleSets", "name": "[variables('vmssName')]", "location": "[resourceGroup().location]", "identity": { "type": "SystemAssigned", }, "properties": { //other resource provider properties... "virtualMachineProfile": { //other virtual machine profile properties... } } } ]
從 Azure 虛擬機器擴展集停用系統指派的受控識別
如果您有不再需要系統指派受控識別的虛擬機器擴展集:
無論您是在本機或透過Azure 入口網站登入 Azure,請使用與包含虛擬機器擴展集之 Azure 訂用帳戶相關聯的帳戶。
將範本 載入編輯器 ,並在 區段中找出
Microsoft.Compute/virtualMachineScaleSets
感興趣的resources
資源。 如果您的 VM 只有系統指派的受控識別,您可以將身分識別類型變更為None
來停用它。Microsoft.Compute/virtualMachineScaleSets API 2018-06-01 版
如果您的 apiVersion 是
2018-06-01
,且您的 VM 同時具有系統和使用者指派的受控識別,請從身分識別類型中移除SystemAssigned
,並保留UserAssigned
與 userAssignedIdentities 字典值。Microsoft.Compute/virtualMachineScaleSets API 2018-06-01 版
如果您的 apiVersion 是
2017-12-01
,而且您的虛擬機器擴展集同時具有系統和使用者指派的受控識別,請從身分識別類型中移除SystemAssigned
,並保留UserAssigned
identityIds
使用者指派的受控識別陣列。下列範例示範如何從沒有使用者指派受控識別的虛擬機器擴展集移除系統指派的受控識別:
{ "name": "[variables('vmssName')]", "apiVersion": "2018-06-01", "location": "[parameters(Location')]", "identity": { "type": "None" } }
使用者指派的受控識別
在本節中,您會使用 Azure Resource Manager 範本,將使用者指派的受控識別指派給虛擬機器擴展集。
注意
若要使用 Azure Resource Manager 範本建立使用者指派的受控識別,請參閱 建立使用者指派的受控識別 。
將使用者指派的受控識別指派給虛擬機器擴展集
在 元素底
resources
下,新增下列專案,將使用者指派的受控識別指派給您的虛擬機器擴展集。 請務必將 取代<USERASSIGNEDIDENTITY>
為您建立的使用者指派受控識別名稱。Microsoft.Compute/virtualMachineScaleSets API 2018-06-01 版
如果您的 apiVersion 是
2018-06-01
,則使用者指派的受控識別會以字典格式儲存userAssignedIdentities
,而且<USERASSIGNEDIDENTITYNAME>
值必須儲存在範本 區段中定義的variables
變數中。{ "name": "[variables('vmssName')]", "apiVersion": "2018-06-01", "location": "[parameters(Location')]", "identity": { "type": "userAssigned", "userAssignedIdentities": { "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {} } } }
Microsoft.Compute/virtualMachineScaleSets API 2017-12-01 版
apiVersion
如果您的 是2017-12-01
或更早版本,則使用者指派的受控識別會儲存在陣列中identityIds
,而且<USERASSIGNEDIDENTITYNAME>
值必須儲存在範本之 variables 區段中定義的變數中。{ "name": "[variables('vmssName')]", "apiVersion": "2017-03-30", "location": "[parameters(Location')]", "identity": { "type": "userAssigned", "identityIds": [ "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITY>'))]" ] } }
當您完成時,您的範本看起來應該如下所示:
Microsoft.Compute/virtualMachineScaleSets API 2018-06-01 版
"resources": [ { //other resource provider properties... "apiVersion": "2018-06-01", "type": "Microsoft.Compute/virtualMachineScaleSets", "name": "[variables('vmssName')]", "location": "[resourceGroup().location]", "identity": { "type": "UserAssigned", "userAssignedIdentities": { "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {} } }, "properties": { //other virtual machine properties... "virtualMachineProfile": { //other virtual machine profile properties... } } } ]
Microsoft.Compute/virtualMachines API 2017-12-01 版
"resources": [ { //other resource provider properties... "apiVersion": "2017-12-01", "type": "Microsoft.Compute/virtualMachineScaleSets", "name": "[variables('vmssName')]", "location": "[resourceGroup().location]", "identity": { "type": "UserAssigned", "identityIds": [ "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]" ] }, "properties": { //other virtual machine properties... "virtualMachineProfile": { //other virtual machine profile properties... } } } ]
從 Azure 虛擬機器擴展集移除使用者指派的受控識別
如果您有不再需要使用者指派受控識別的虛擬機器擴展集:
無論您是在本機或透過Azure 入口網站登入 Azure,請使用與包含虛擬機器擴展集之 Azure 訂用帳戶相關聯的帳戶。
將範本 載入編輯器 ,並在 區段中找出
Microsoft.Compute/virtualMachineScaleSets
感興趣的resources
資源。 如果您有只有使用者指派受控識別的虛擬機器擴展集,您可以將身分識別類型變更為None
來停用它。下列範例示範如何從沒有系統指派受控識別的 VM 中移除所有使用者指派的受控識別:
{ "name": "[variables('vmssName')]", "apiVersion": "2018-06-01", "location": "[parameters(Location')]", "identity": { "type": "None" } }
Microsoft.Compute/virtualMachineScaleSets API 2018-06-01 版
若要從虛擬機器擴展集移除單一使用者指派的受控識別,請從
userAssignedIdentities
字典中移除它。如果您有系統指派的身分識別,請將它保留在
type
值之下的值identity
中。Microsoft.Compute/virtualMachineScaleSets API 2017-12-01 版
若要從虛擬機器擴展集移除單一使用者指派的受控識別,請從
identityIds
陣列中移除它。如果您有系統指派的受控識別,請將它保留在
type
值下方的值identity
中。