共用方式為


在虛擬機器擴展集上設定 Azure 資源的受控識別

Azure 資源受控識別是 Microsoft Entra ID 的一項功能。 每個支援適用於 Azure 資源的受控識別 Azure 服務均受限於其本身的時間表。 在開始之前,請務必先檢閱資源的受控識別可用性狀態和已知問題

資源的受控身分識別會在 Microsoft Entra ID 中為 Azure 服務提供自動受控身分識別。 您可以使用此身分識別來向任何支援 Microsoft Entra 驗證的服務進行驗證,不需要任何您程式碼中的認證。

如需 Azure 原則定義和詳細資料的相關資訊,請參閱使用 Azure 原則指派受控識別 (預覽)

在本文中,您將了解如何使用 Azure 入口網站,在虛擬機器擴展集上執行下列 Azure 資源受控識別作業:

  • 如果您不熟悉 Azure 資源的受控識別,請參閱概觀一節

  • 如果您還沒有 Azure 帳戶,請先註冊免費帳戶,再繼續進行。

  • 若要執行本文中的管理作業,您的帳戶需要下列 Azure 角色指派:

    注意

    不需要其他 Microsoft Entra 目錄角色指派。

系統指派的受控識別

在本節中,您會了解如何使用 Azure 入口網站,啟用和停用系統指派的受控識別。

在虛擬機器擴展集建立期間啟用系統指派的受控識別

目前,Azure 入口網站不支援在建立虛擬機器擴展集時啟用系統指派的受控識別。 相反地,請參閱下列虛擬機器擴展集建立快速入門文章,來先建立虛擬機器擴展集,然後繼續進行下一節,以了解如何在虛擬機器擴展集上啟用系統指派的受控識別:

在現有虛擬機器擴展集上啟用系統指派的受控識別

提示

本文中的步驟可能略有不同,具體取決於您從哪個入口網站展開作業。

若要在原本佈建時沒有使用系統指派受控識別的虛擬機器擴展集上啟用該識別:

  1. 使用與包含虛擬機器擴展集的 Azure 訂用帳戶相關聯的帳戶,登入 Azure 入口網站

  2. 瀏覽到需要的虛擬機器擴展集。

  3. 在 [系統指派]、[狀態] 底下,選取 [開啟],然後按一下 [儲存]

    此螢幕擷取畫面顯示已選取 [系統指派]、狀態為 [開啟],並反白顯示 [儲存] 按鈕的 [身分識別 (預覽)] 頁面。

從虛擬機器擴展集移除系統指派的受控識別

如果您的虛擬機器擴展集不再需要系統指派的受控識別:

  1. 使用與包含虛擬機器擴展集的 Azure 訂用帳戶相關聯的帳戶,登入 Azure 入口網站。 此外也請確定您的帳戶屬於在虛擬機器擴展集上具有寫入權限的角色。

  2. 瀏覽到需要的虛擬機器擴展集。

  3. 在 [系統指派]、[狀態] 底下,選取 [關閉],然後按一下 [儲存]

    顯示設定頁面的螢幕擷取畫面。

使用者指派的受控識別

在本節中,您會了解如何使用 Azure 入口網站,從虛擬機器擴展集新增和移除使用者指派的受控識別。

在虛擬機器擴展集建立期間指派使用者指派的受控識別

目前,Azure 入口網站不支援在建立虛擬機器擴展集時指派使用者指派的受控識別。 相反地,請參閱下列虛擬機器擴展集建立快速入門文章,來先建立虛擬機器擴展集,然後繼續進行下一節,以了解如何對它指派使用者指派的受控識別:

將使用者指派的受控識別指派給現有的虛擬機器擴展集

  1. 使用與包含虛擬機器擴展集的 Azure 訂用帳戶相關聯的帳戶,登入 Azure 入口網站

  2. 瀏覽至所需虛擬機器擴展集,然後依序按一下 [身分識別]、[使用者指派] 和 [+新增]

    將使用者指派的身分識別新增到虛擬機器擴展集的螢幕擷取畫面。

  3. 按一下想要新增到虛擬機器擴展集的使用者指派身分識別,然後按一下 [新增]

    顯示如何將使用者指派之身分識別新增到虛擬機器擴展集的螢幕擷取畫面。

從虛擬機器擴展集移除使用者指派的受控識別

  1. 使用與包含虛擬機器的 Azure 訂用帳戶相關聯的帳戶,登入 Azure 入口網站

  2. 瀏覽至所需虛擬機器擴展集,按一下 [身分識別]、[使用者指派] 和想要刪除的使用者指派受控識別名稱,然後按一下 [移除] (按一下 [確認] 窗格中的 [是])。

    顯示如何從虛擬機器擴展集中移除使用者指派之身分識別的螢幕擷取畫面。

下一步

在本文中,您將了解如何使用 Azure CLI,在 Azure 虛擬機器擴展集上執行下列 Azure 資源受控識別作業:

  • 在 Azure 虛擬機器擴展集上啟用和停用系統指派的受控識別
  • 在 Azure 虛擬機器擴展集上新增和移除使用者指派的受控識別

如果您還沒有 Azure 帳戶,請先註冊免費帳戶,再繼續進行。

必要條件

  • 如果不熟悉 Azure 資源的受控識別,請參閱 什麼是 Azure 資源受控識別?。 若要了解系統指派和使用者指派的受控識別類型,請參閱 受控識別類型

  • 若要執行本文中的管理作業,您的帳戶需要下列 Azure 角色型存取控制指派:

    • 虛擬機器參與者,可建立虛擬機器擴展集,並從虛擬機器擴展集啟用和移除系統和/或使用者指派的受控識別。

    • 受控識別參與者角色,可建立使用者指派的受控識別。

    • 受控識別操作員角色,可為虛擬機器擴展集指派和移除使用者指派的受控識別。

    注意

    不需要其他 Microsoft Entra 目錄角色指派。

系統指派的受控識別

在本節中,您將了解如何使用 Azure CLI,為 Azure 虛擬機器擴展集啟用和停用系統指派的受控識別。

在 Azure 虛擬機器擴展集建立期間啟用系統指派的受控識別

若要建立已啟用系統指派受控識別的虛擬機器擴展集:

  1. 使用 az group create,為您的虛擬機器擴展集和其相關資源建立資源群組。 如果您已經有想要使用的資源群組,您可以略過此步驟:

    az group create --name myResourceGroup --location westus
    
  2. 建立虛擬機器擴展集。 下列範例會依 --assign-identity 參數的要求 (具有指定的 --role--scope),建立具有系統指派受控識別且名為 myVMSS 的虛擬機器擴展集。 --admin-username--admin-password 參數會指定虛擬機器登入的系統管理使用者名稱和密碼帳戶。 根據您的環境更新這些值:

    az vmss create --resource-group myResourceGroup --name myVMSS --image win2016datacenter --upgrade-policy-mode automatic --custom-data cloud-init.txt --admin-username azureuser --admin-password myPassword12 --assign-identity --generate-ssh-keys --role contributor --scope mySubscription
    

在現有 Azure 虛擬機器擴展集上啟用系統指派的受控識別

如果您需要在現有 Azure 虛擬機器擴展集上啟用系統指派的受控識別:

az vmss identity assign -g myResourceGroup -n myVMSS

從 Azure 虛擬機器擴展集停用系統指派的受控識別

如果您的虛擬機器擴展集已不再需要系統指派的受控識別,但仍需要使用者指派的受控識別,請使用下列命令:

az vmss update -n myVM -g myResourceGroup --set identity.type='UserAssigned' 

如果您的虛擬機器不再需要系統指派的受控識別,而且沒有使用者指派的受控識別,請使用下列命令:

注意

none 會區分大小寫。 它必須是小寫字母。

az vmss update -n myVM -g myResourceGroup --set identity.type="none"

使用者指派的受控識別

在本節中,您將了解如何使用 Azure CLI 啟用和移除使用者指派的受控識別。

在虛擬機器擴展集建立期間指派使用者指派的受控識別

本節會逐步引導您建立虛擬機器擴展集,並將使用者指派的受控識別指派至虛擬機器擴展集。 如果您已經有想要使用的虛擬機器擴展集,請略過本節並繼續進行下一節。

  1. 如果您已經有想要使用的資源群組,可以略過此步驟。 使用 az group create 建立資源群組,以便控制及部署使用者指派的受控識別。 請務必以您自己的值取代 <RESOURCE GROUP><LOCATION> 參數的值。 :

    az group create --name <RESOURCE GROUP> --location <LOCATION>
    
  2. 使用 az identity create,建立使用者指派的受控識別。 -g 參數會指定建立使用者指派受控識別的資源群組,而 -n 參數會指定其名稱。 請務必以您自己的值取代 <RESOURCE GROUP><USER ASSIGNED IDENTITY NAME> 參數的值:

    重要

    建立使用者指派的受控識別時,名稱必須以字母或數字開頭,並且包含英數字元、連字號 (-) 和底線 (_) 的組合。 若要讓虛擬機器或虛擬機器擴展集的指派正常運作,名稱長度上限為 24 個字元。 如需詳細資訊,請參閱常見問題集和已知問題

    az identity create -g <RESOURCE GROUP> -n <USER ASSIGNED IDENTITY NAME>
    

    回應會包含所建立使用者指派受控識別的詳細資料,與下列內容類似。 指派給使用者指派受控識別的資源 id 值會使用於下列步驟。

    {
         "clientId": "73444643-8088-4d70-9532-c3a0fdc190fz",
         "clientSecretUrl": "https://control-westcentralus.identity.azure.net/subscriptions/<SUBSCRIPTON ID>/resourcegroups/<RESOURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY NAME>/credentials?tid=5678&oid=9012&aid=73444643-8088-4d70-9532-c3a0fdc190fz",
         "id": "/subscriptions/<SUBSCRIPTON ID>/resourcegroups/<RESOURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY NAME>",
         "location": "westcentralus",
         "name": "<USER ASSIGNED IDENTITY NAME>",
         "principalId": "e5fdfdc1-ed84-4d48-8551-fe9fb9dedfll",
         "resourceGroup": "<RESOURCE GROUP>",
         "tags": {},
         "tenantId": "733a8f0e-ec41-4e69-8ad8-971fc4b533bl",
         "type": "Microsoft.ManagedIdentity/userAssignedIdentities"    
    }
    
  3. 建立虛擬機器擴展集。 下列範例會依 --assign-identity 參數的指定 (具有指定的 --role--scope),建立與新的使用者指派受控識別建立關聯的虛擬機器擴展集。 別忘了以您自己的值取代 <RESOURCE GROUP><VMSS NAME><USER NAME><PASSWORD><USER ASSIGNED IDENTITY><ROLE><SUBSCRIPTION> 參數的值。

    az vmss create --resource-group <RESOURCE GROUP> --name <VMSS NAME> --image <SKU Linux Image> --admin-username <USER NAME> --admin-password <PASSWORD> --assign-identity <USER ASSIGNED IDENTITY> --role <ROLE> --scope <SUBSCRIPTION>
    

將使用者指派的受控識別指派給現有的虛擬機器擴展集

  1. 使用 az identity create,建立使用者指派的受控識別。 -g 參數會指定建立使用者指派受控識別的資源群組,而 -n 參數會指定其名稱。 請務必以您自己的值取代 <RESOURCE GROUP><USER ASSIGNED IDENTITY NAME> 參數的值:

    az identity create -g <RESOURCE GROUP> -n <USER ASSIGNED IDENTITY NAME>
    

    回應會包含所建立使用者指派受控識別的詳細資料,與下列內容類似。

    {
         "clientId": "73444643-8088-4d70-9532-c3a0fdc190fz",
         "clientSecretUrl": "https://control-westcentralus.identity.azure.net/subscriptions/<SUBSCRIPTON ID>/resourcegroups/<RESOURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY >/credentials?tid=5678&oid=9012&aid=73444643-8088-4d70-9532-c3a0fdc190fz",
         "id": "/subscriptions/<SUBSCRIPTON ID>/resourcegroups/<RESOURCE GROUP>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<USER ASSIGNED IDENTITY>",
         "location": "westcentralus",
         "name": "<USER ASSIGNED IDENTITY>",
         "principalId": "e5fdfdc1-ed84-4d48-8551-fe9fb9dedfll",
         "resourceGroup": "<RESOURCE GROUP>",
         "tags": {},
         "tenantId": "733a8f0e-ec41-4e69-8ad8-971fc4b533bl",
         "type": "Microsoft.ManagedIdentity/userAssignedIdentities"    
    }
    
  2. 將使用者指派的受控識別指派至虛擬機器擴展集。 請務必以您自己的值取代 <RESOURCE GROUP><VIRTUAL MACHINE SCALE SET NAME> 參數的值。 <USER ASSIGNED IDENTITY> 是使用者所指派身分識別的資源 name 屬性 (在上一個步驟中建立):

    az vmss identity assign -g <RESOURCE GROUP> -n <VIRTUAL MACHINE SCALE SET NAME> --identities <USER ASSIGNED IDENTITY>
    

從 Azure 虛擬機器擴展集移除使用者指派的受控識別

若要從虛擬機器擴展集移除使用者指派的受控識別,請使用 az vmss identity remove。 如果這是指派給虛擬機器擴展集的唯一使用者指派的受控識別,則會從識別類型值中移除 UserAssigned。 請務必以您自己的值取代 <RESOURCE GROUP><VIRTUAL MACHINE SCALE SET NAME> 參數的值。 <USER ASSIGNED IDENTITY> 是使用者指派受控識別的 name 屬性,您可以使用 az vmss identity show 在虛擬機器擴展集的識別區段中找到此項目:

az vmss identity remove -g <RESOURCE GROUP> -n <VIRTUAL MACHINE SCALE SET NAME> --identities <USER ASSIGNED IDENTITY>

如果您的虛擬機器擴展集沒有系統指派的受控識別,而您想要從其中移除所有使用者指派的受控識別,請使用下列命令:

注意

none 會區分大小寫。 它必須是小寫字母。

az vmss update -n myVMSS -g myResourceGroup --set identity.type="none" identity.userAssignedIdentities=null

如果您的虛擬機器擴展集同時具有系統指派和使用者指派的受控識別,您可以藉由切換為僅使用系統指派的受控識別,來移除所有使用者指派的身分識別。 使用下列命令:

az vmss update -n myVMSS -g myResourceGroup --set identity.type='SystemAssigned' identity.userAssignedIdentities=null 

下一步

在本文中,您將了解如何使用 PowerShell,在虛擬機器擴展集上執行 Azure 資源受控識別作業:

  • 在虛擬機器擴展集上啟用和停用系統指派的受控識別
  • 在虛擬機器擴展集上新增和移除使用者指派的受控識別

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

必要條件

  • 如果您不熟悉 Azure 資源的受控識別,請參閱概觀一節請務必檢閱系統指派和使用者受控指派身分識別之間的差異

  • 如果您還沒有 Azure 帳戶,請先註冊免費帳戶,再繼續進行。

  • 若要執行本文中的管理作業,您的帳戶需要下列 Azure 角色型存取控制指派:

    注意

    不需要其他 Microsoft Entra 目錄角色指派。

    • 虛擬機器參與者,可建立虛擬機器擴展集,並從虛擬機器擴展集啟用和移除系統指派受控和/或使用者指派的受控識別。
    • 受控識別參與者角色,可建立使用者指派的受控識別。
    • 受控識別操作員角色,可為虛擬機器擴展集指派和移除使用者指派的受控識別。
  • 若要執行範例指令碼,您有兩個選項:

    • 使用 Azure Cloud Shell,您可以使用程式碼區塊右上角的 [試用] 按鈕來開啟此服務。
    • 藉由安裝最新版本的 Azure PowerShell,在本機執行指令碼,然後使用 Connect-AzAccount 來登入 Azure。

系統指派的受控識別

在本節中,您會學習如何使用 Azure PowerShell 啟用和移除系統指派的受控識別。

在 Azure 虛擬機器擴展集建立期間啟用系統指派的受控識別

若要建立已啟用系統指派受控識別的虛擬機器擴展集:

  1. 請參閱 New-AzVmssConfig Cmdlet 參考一文中的「範例 1」,以建立具有系統指派受控識別的虛擬機器擴展集。 將參數 -IdentityType SystemAssigned 新增至 New-AzVmssConfig Cmdlet:

    $VMSS = New-AzVmssConfig -Location $Loc -SkuCapacity 2 -SkuName "Standard_A0" -UpgradePolicyMode "Automatic" -NetworkInterfaceConfiguration $NetCfg -IdentityType SystemAssigned`
    

在現有 Azure 虛擬機器擴展集上啟用系統指派的受控識別

如果您需要在現有 Azure 虛擬機器擴展集上啟用系統指派的受控識別:

  1. 請確定您的 Azure 帳戶屬於在虛擬機器擴展集上具有寫入權限的角色,例如「虛擬機器參與者」。

  2. 使用 Get-AzVmss Cmdlet 來取出虛擬機器擴展集屬性。 然後在 Update-AzVmss Cmdlet 上使用 -IdentityType 參數來啟用系統指派的受控識別:

    Update-AzVmss -ResourceGroupName myResourceGroup -Name -myVmss -IdentityType "SystemAssigned"
    

從 Azure 虛擬機器擴展集停用系統指派的受控識別

如果您的虛擬機器擴展集已不再需要系統指派的受控識別,但仍需要使用者指派的受控識別,請使用下列 Cmdlet:

  1. 請確定您的帳戶屬於在虛擬機器擴展集上具有寫入權限的角色,例如「虛擬機器參與者」。

  2. 執行下列 Cmdlet:

    Update-AzVmss -ResourceGroupName myResourceGroup -Name myVmss -IdentityType "UserAssigned"
    
  3. 如果您的虛擬機器擴展集不再需要系統指派的受控識別,而且沒有使用者指派的受控識別,請使用下列命令:

    Update-AzVmss -ResourceGroupName myResourceGroup -Name myVmss -IdentityType None
    

使用者指派的受控識別

在本節中,您將了解如何使用 Azure PowerShell,從虛擬機器擴展集新增和移除使用者指派的受控識別。

在 Azure 虛擬機器擴展集建立期間指派使用者指派的受控識別

目前無法透過 PowerShell 支援建立具有使用者指派受控識別的新虛擬機器擴展集。 請參閱下一節,以了解如何將使用者指派的受控識別新增至現有的虛擬機器擴展集。 因此請定期回來查看是否有更新。

將使用者指派的受控識別指派給現有的 Azure 虛擬機器擴展集

若要將使用者指派的受控識別指派給現有的 Azure 虛擬機器擴展集:

  1. 請確定您的帳戶屬於在虛擬機器擴展集上具有寫入權限的角色,例如「虛擬機器參與者」。

  2. 使用 Get-AzVM Cmdlet 來取出虛擬機器擴展集屬性。 然後使用 Update-AzVmss Cmdlet 上的 -IdentityType-IdentityID 參數,將使用者指派的受控識別新增至虛擬機器擴展集。 以您自己的值取代 <VM NAME><SUBSCRIPTION ID><RESROURCE GROUP><USER ASSIGNED ID1>USER ASSIGNED ID2

    重要

    建立使用者指派的受控識別時,名稱必須以字母或數字開頭,並且包含英數字元、連字號 (-) 和底線 (_) 的組合。 若要讓虛擬機器或虛擬機器擴展集的指派正常運作,名稱長度上限為 24 個字元。 如需詳細資訊,請參閱常見問題集和已知問題

    Update-AzVmss -ResourceGroupName <RESOURCE GROUP> -Name <VMSS NAME> -IdentityType UserAssigned -IdentityID "<USER ASSIGNED ID1>","<USER ASSIGNED ID2>"
    

從 Azure 虛擬機器擴展集移除使用者指派的受控識別

如果您的虛擬機器擴展集具有多個使用者指派的受控識別,則您可以使用下列命令來移除所有識別,但請留下最後一個。 請務必以您自己的值取代 <RESOURCE GROUP><VIRTUAL MACHINE SCALE SET NAME> 參數的值。 <USER ASSIGNED IDENTITY NAME> 是使用者指派受控識別的名稱屬性,它應該保留在虛擬機器擴展集上。 您可以使用 az vmss show,在虛擬機器擴展集的識別區段中找到此資訊:

Update-AzVmss -ResourceGroupName myResourceGroup -Name myVmss -IdentityType UserAssigned -IdentityID "<USER ASSIGNED IDENTITY NAME>"

如果您的虛擬機器擴展集沒有系統指派的受控識別,而您想要從其中移除所有使用者指派的受控識別,請使用下列命令:

Update-AzVmss -ResourceGroupName myResourceGroup -Name myVmss -IdentityType None

如果您的虛擬機器擴展集同時具有系統指派和使用者指派的受控識別,您可以藉由切換為僅使用系統指派的身分識別,來移除所有使用者指派的受控識別。

Update-AzVmss -ResourceGroupName myResourceGroup -Name myVmss -IdentityType "SystemAssigned"

下一步

在本文中,您將了解如何使用 Azure Resource Manager 部署範本,在 Azure 虛擬機器擴展集上執行以下 Azure 資源受控識別作業:

  • 在 Azure 虛擬機器擴展集上啟用和停用系統指派的受控識別
  • 在 Azure 虛擬機器擴展集上新增和移除使用者指派的受控識別

必要條件

Azure 資源管理員範本

如同 Azure 入口網站和指令碼,Azure Resource Manager 範本可讓您部署由 Azure 資源群組所定義的最新或已修改資源。 範本編輯和部署有幾個選項可用 (在本機和入口網站),包括:

不論您選擇的選項,在初始部署和重新部署期間,範本的語法都相同。 在新的或現有 VM 上啟用 Azure 資源受控識別,是以相同的方式來完成。 此外,根據預設,Azure Resource Manager 會對部署採取累加式更新

系統指派的受控識別

在本節中,您將使用 Azure Resource Manager 範本以啟用和停用系統指派的受控識別。

允許在建立虛擬機器擴展集期間,或在現有的虛擬機擴展集中,使用系統指派的受控識別

  1. 無論您是在本機登入 Azure 或透過 Azure 入口網站登入,都請使用與包含虛擬機器擴展集的 Azure 訂用帳戶相關聯的帳戶。

  2. 若要啟用系統指派的受控識別,請將範本載入到編輯器、在 resources 區段中找出感興趣的 Microsoft.Compute/virtualMachinesScaleSets 資源,然後在與 "type": "Microsoft.Compute/virtualMachinesScaleSets" 屬性相同的層級上新增 identity。 使用下列語法:

    "identity": {
        "type": "SystemAssigned"
    }
    
  3. 完成時,應會將下列區段新增至範本的資源區段,如下列範例所示:

     "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 虛擬機器擴展集停用系統指派的受控識別

如果您的虛擬機器擴展集不再需要系統指派的受控識別:

  1. 無論您是在本機登入 Azure 或透過 Azure 入口網站登入,都請使用與包含虛擬機器擴展集的 Azure 訂用帳戶相關聯的帳戶。

  2. 編輯器中載入範本,然後在 resources 區段找出想要的 Microsoft.Compute/virtualMachineScaleSets 資源。 若您的虛擬機器只有系統指派的受控識別,您可以將識別類型變更為 None 予以停用。

    Microsoft.Compute/virtualMachineScaleSets API 2018-06-01 版

    如果您的 apiVersion 為 2018-06-01 且您的 VM 同時具有系統與使用者指派的受控識別,請從識別類型中移除 SystemAssigned,並保留 UserAssigned 以及 userAssignedIdentities 字典值。

    Microsoft.Compute/virtualMachineScaleSets API 2018-06-01 版

    若您的 apiVersion 為 identityIds 且您的虛擬機器擴展集同時具有系統與使用者指派的受控識別,請從識別類型中移除 2017-12-01,並保留 SystemAssigned 以及使用者指派受控識別的 UserAssigned 陣列。

    下列範例示範如何從沒有使用者指派受控識別的虛擬機器擴展集中移除系統指派的受控識別:

    {
        "name": "[variables('vmssName')]",
        "apiVersion": "2018-06-01",
        "location": "[parameters(Location')]",
        "identity": {
            "type": "None"
         }
    
    }
    

使用者指派的受控識別

在本節中,您會使用 Azure Resource Manager 範本,將使用者指派的受控識別指派給虛擬機器擴展集。

注意

若要使用 Azure Resource Manager 範本建立使用者指派的受控識別,請參閱建立使用者指派的受控識別

將使用者指派的受控識別指派給虛擬機器擴展集

  1. 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 版

    如果您的 apiVersion2017-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>'))]"
            ]
        }
    }
    
  2. 完成之後,您的範本看起來應該如下所示:

    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 虛擬機器擴展集移除使用者指派的受控識別

如果您的虛擬機器擴展集不再需要使用者指派的受控識別:

  1. 無論您是在本機登入 Azure 或透過 Azure 入口網站登入,都請使用與包含虛擬機器擴展集的 Azure 訂用帳戶相關聯的帳戶。

  2. 編輯器中載入範本,然後在 resources 區段找出想要的 Microsoft.Compute/virtualMachineScaleSets 資源。 若您的虛擬機器擴展集只有使用者指派的受控識別,您可以將識別類型變更為 None 予以停用。

    下列範例示範如何從沒有系統指派受控識別的虛擬機器中移除所有使用者指派的受控識別:

    {
        "name": "[variables('vmssName')]",
        "apiVersion": "2018-06-01",
        "location": "[parameters(Location')]",
        "identity": {
            "type": "None"
         }
    }
    

    Microsoft.Compute/virtualMachineScaleSets API 2018-06-01 版

    若要從虛擬機器擴展集移除單一使用者指派的受控識別,請從 userAssignedIdentities 字典中移除它。

    如果您有系統指派的受控識別,則將其保存在 identity 值下方的 type 值中。

    Microsoft.Compute/virtualMachineScaleSets API 2017-12-01 版

    若要從虛擬機器擴展集移除單一使用者指派的受控識別,請從 identityIds 陣列中移除它。

    如果您有系統指派的受控識別,則將其保存在 identity 值下方的 type 值中。

下一步

在本文中,使用 CURL 來呼叫 Azure Resource Manager REST 端點,即可了解如何在虛擬機器擴展集上執行下列 Azure 資源受控識別作業:

  • 在 Azure 虛擬機器擴展集上啟用和停用系統指派的受控識別
  • 在 Azure 虛擬機器擴展集上新增和移除使用者指派的受控識別

如果您還沒有 Azure 帳戶,請先註冊免費帳戶,再繼續進行。

必要條件

  • 如果不熟悉 Azure 資源的受控識別,請參閱 什麼是 Azure 資源受控識別?。 若要了解系統指派和使用者指派的受控識別類型,請參閱 受控識別類型

  • 若要執行本文中的管理作業,您的帳戶需要下列 Azure 角色指派:

    • 虛擬機器參與者,可建立虛擬機器擴展集,並從虛擬機器擴展集啟用和移除系統和/或使用者指派的受控識別。

    • 受控識別參與者角色,可建立使用者指派的受控識別。

    • 受控識別操作員角色,可為虛擬機器擴展集指派和移除使用者指派的識別。

    注意

    不需要其他 Microsoft Entra 目錄角色指派。

系統指派的受控識別

在本節中,您將了解如何使用 CURL 呼叫 Azure Resource Manager REST 端點,在虛擬機器擴展集上啟用和停用系統指派的受控識別。

在虛擬機器擴展集建立期間啟用系統指派的受控識別

若要啟用系統指派的受控識別來建立虛擬機器擴展集,您需要建立虛擬機器擴展集,並擷取存取權杖,才能使用 CURL 以系統指派的受控識別類型值呼叫 Resource Manager 端點。

  1. 使用 az group create,為您的虛擬機器擴展集和其相關資源建立資源群組。 如果您已經有想要使用的資源群組,您可以略過此步驟:

    az group create --name myResourceGroup --location westus
    
  2. 為虛擬機器擴展集建立網路介面

     az network nic create -g myResourceGroup --vnet-name myVnet --subnet mySubnet -n myNic
    
  3. 擷取持有人存取權杖,在下一個步驟中,會使用此權杖搭配搭配系統指派的受控識別,在授權標頭中建立虛擬機器擴展集。

    az account get-access-token
    
  4. 使用 Azure Cloud Shell 並使用 CURL 呼叫 Azure Resource Manager REST 端點來建立虛擬機器擴展集。 下列範例會使用系統指派的受控識別 (如同在要求本文中由 "identity":{"type":"SystemAssigned"} 值所識別),在 myResourceGroup 中建立名為 myVMSS 的虛擬機器擴展集。 將上一個步驟中要求持有人存取權杖時所收到的值用以取代 <ACCESS TOKEN>值,<SUBSCRIPTION ID> 的值則為適用於您的環境的值。

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PUT -d '{"sku":{"tier":"Standard","capacity":3,"name":"Standard_D1_v2"},"location":"eastus","identity":{"type":"SystemAssigned"},"properties":{"overprovision":true,"virtualMachineProfile":{"storageProfile":{"imageReference":{"sku":"2016-Datacenter","publisher":"MicrosoftWindowsServer","version":"latest","offer":"WindowsServer"},"osDisk":{"caching":"ReadWrite","managedDisk":{"storageAccountType":"StandardSSD_LRS"},"createOption":"FromImage"}},"osProfile":{"computerNamePrefix":"myVMSS","adminUsername":"azureuser","adminPassword":"myPassword12"},"networkProfile":{"networkInterfaceConfigurations":[{"name":"myVMSS","properties":{"primary":true,"enableIPForwarding":true,"ipConfigurations":[{"name":"myVMSS","properties":{"subnet":{"id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"}}}]}}]}},"upgradePolicy":{"mode":"Manual"}}}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
    
    PUT https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "sku":{
           "tier":"Standard",
           "capacity":3,
           "name":"Standard_D1_v2"
        },
        "location":"eastus",
        "identity":{
           "type":"SystemAssigned"
        },
        "properties":{
           "overprovision":true,
           "virtualMachineProfile":{
              "storageProfile":{
                 "imageReference":{
                    "sku":"2016-Datacenter",
                    "publisher":"MicrosoftWindowsServer",
                    "version":"latest",
                    "offer":"WindowsServer"
                 },
                 "osDisk":{
                    "caching":"ReadWrite",
                    "managedDisk":{
                       "storageAccountType":"StandardSSD_LRS"
                    },
                    "createOption":"FromImage"
                 }
              },
              "osProfile":{
                 "computerNamePrefix":"myVMSS",
                 "adminUsername":"azureuser",
                 "adminPassword":"myPassword12"
              },
              "networkProfile":{
                 "networkInterfaceConfigurations":[
                    {
                       "name":"myVMSS",
                       "properties":{
                          "primary":true,
                          "enableIPForwarding":true,
                          "ipConfigurations":[
                             {
                                "name":"myVMSS",
                                "properties":{
                                   "subnet":{
                                      "id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"
                                   }
                                }
                             }
                          ]
                       }
                    }
                 ]
              }
           },
           "upgradePolicy":{
              "mode":"Manual"
           }
        }
     }  
    

在現有虛擬機器擴展集上啟用系統指派的受控識別

若要在現有的虛擬機器擴展集上啟用系統指派的受控識別,您需要取得存取權杖,然後使用 CURL 呼叫 Resource Manager REST 端點來更新識別類型。

  1. 擷取持有人存取權杖,在下一個步驟中,會使用此權杖搭配搭配系統指派的受控識別,在授權標頭中建立虛擬機器擴展集。

    az account get-access-token
    
  2. 使用下列 CURL 命令呼叫 Azure Resource Manager REST 端點,以便在您的虛擬機器擴展集上啟用系統指派的受控識別 (如同在要求本文中由 myVMSS 虛擬機器擴展集的 {"identity":{"type":"SystemAssigned"} 值所識別)。 將上一個步驟中要求持有人存取權杖時所收到的值用以取代 <ACCESS TOKEN>值,<SUBSCRIPTION ID> 的值則為適用於您的環境的值。

    重要

    若要確保您不會刪除虛擬機器擴展集指派的任何現有使用者指派識別,需要使用下列 CURL 命令來列出使用者指派的受控識別:curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>"。 如有任何使用者指派的受控識別是指派給回應中 identity 值識別的虛擬機器擴展集,請跳至步驟 3,此步驟將說明如何保留使用者指派的受控識別,同時在虛擬機器擴展集上啟用系統指派的受控識別。

     curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned"}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned"
        }
     }
    
  3. 若要在有現有使用者指派受控識別的虛擬機器擴展集上啟用系統指派的受控識別,您需要將 SystemAssigned 新增至 type 值。

    舉例而言,若您的虛擬機器擴展集有使用者指派的受控識別 ID1ID2,而且您想要將系統指派的受控識別新增至虛擬機器擴展集,請使用下列 CURL 呼叫。 以適用於您的環境的值取代 <ACCESS TOKEN><SUBSCRIPTION ID>

    API 版本 2018-06-01 會將使用者指派的受控身分識別儲存在 userAssignedIdentities 值內 (採用字典格式),而不是儲存在 identityIds 值內 (採用 API 版本 2017-12-01 中所使用的陣列格式)。

    API 版本 2018-06-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned,UserAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{},"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{}}}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned,UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
              },
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{
    
              }
           }
        }
     }
    

    API 版本 2017-12-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"SystemAssigned,UserAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1","/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"]}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned,UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1",
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"
           ]
        }
     }
    

從虛擬機器擴展集停用系統指派的受控識別

若要在現有的虛擬機器擴展集上停用系統指派的識別,您需要取得存取權杖,然後使用 CURL 呼叫 Resource Manager REST 端點,將識別類型更新為 None

  1. 擷取持有人存取權杖,在下一個步驟中,會使用此權杖搭配搭配系統指派的受控識別,在授權標頭中建立虛擬機器擴展集。

    az account get-access-token
    
  2. 使用 CURL 呼叫 Azure Resource Manager REST 端點來停用系統指派的受控識別,以便更新虛擬機器擴展集。 下列範例會停用系統指派的受控識別 (如同在要求本文中由 myVMSS 虛擬機器擴展集中的 {"identity":{"type":"None"}} 值所識別)。 將上一個步驟中要求持有人存取權杖時所收到的值用以取代 <ACCESS TOKEN>值,<SUBSCRIPTION ID> 的值則為適用於您的環境的值。

    重要

    若要確保您不會刪除虛擬機器擴展集指派的任何現有使用者指派識別,需要使用下列 CURL 命令來列出使用者指派的受控識別:curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>"。 如有任何使用者指派的受控識別是指派給虛擬機器擴展集,請跳至步驟 3,此步驟將說明如何保留使用者指派的受控識別,同時從虛擬機器擴展集移除系統指派的受控識別。

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"None"}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"None"
        }
     }
    

    若要從具有使用者指派受控識別的虛擬機器擴展集移除系統指派的受控識別,請從 {"identity":{"type:" "}} 值移除 SystemAssigned,但同時保留 UserAssigned 值和 userAssignedIdentities 字典值 (如果您使用 API 版本 2018-06-01)。 如果您使用 API 版本 2017-12-01 或先前版本,則請保留 identityIds 陣列。

使用者指派的受控識別

在本節中,您將了解如何使用 CURL 呼叫 Azure Resource Manager REST 端點,在虛擬機器擴展集上新增和移除使用者指派的受控識別。

在虛擬機器擴展集建立期間指派使用者指派的受控識別

  1. 擷取持有人存取權杖,在下一個步驟中,會使用此權杖搭配搭配系統指派的受控識別,在授權標頭中建立虛擬機器擴展集。

    az account get-access-token
    
  2. 為虛擬機器擴展集建立網路介面

     az network nic create -g myResourceGroup --vnet-name myVnet --subnet mySubnet -n myNic
    
  3. 擷取持有人存取權杖,在下一個步驟中,會使用此權杖搭配搭配系統指派的受控識別,在授權標頭中建立虛擬機器擴展集。

    az account get-access-token
    
  4. 使用以下找到的指示建立使用者指派的受控識別:建立使用者指派的受控識別

  5. 使用 CURL 呼叫 Azure Resource Manager REST 端點來建立虛擬機器擴展集。 下列範例會使用使用者指派的受控識別 ID1 (如同在要求本文中由 "identity":{"type":"UserAssigned"} 值所識別),在資源群組 myResourceGroup 中建立名為 myVMSS 的虛擬機器擴展集。 將上一個步驟中要求持有人存取權杖時所收到的值用以取代 <ACCESS TOKEN>值,<SUBSCRIPTION ID> 的值則為適用於您的環境的值。

    API 版本 2018-06-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PUT -d '{"sku":{"tier":"Standard","capacity":3,"name":"Standard_D1_v2"},"location":"eastus","identity":{"type":"UserAssigned","userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{}}},"properties":{"overprovision":true,"virtualMachineProfile":{"storageProfile":{"imageReference":{"sku":"2016-Datacenter","publisher":"MicrosoftWindowsServer","version":"latest","offer":"WindowsServer"},"osDisk":{"caching":"ReadWrite","managedDisk":{"storageAccountType":"StandardSSD_LRS"},"createOption":"FromImage"}},"osProfile":{"computerNamePrefix":"myVMSS","adminUsername":"azureuser","adminPassword":"myPassword12"},"networkProfile":{"networkInterfaceConfigurations":[{"name":"myVMSS","properties":{"primary":true,"enableIPForwarding":true,"ipConfigurations":[{"name":"myVMSS","properties":{"subnet":{"id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"}}}]}}]}},"upgradePolicy":{"mode":"Manual"}}}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
    
    PUT https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "sku":{
           "tier":"Standard",
           "capacity":3,
           "name":"Standard_D1_v2"
        },
        "location":"eastus",
        "identity":{
           "type":"UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
    
              }
           }
        },
        "properties":{
           "overprovision":true,
           "virtualMachineProfile":{
              "storageProfile":{
                 "imageReference":{
                    "sku":"2016-Datacenter",
                    "publisher":"MicrosoftWindowsServer",
                    "version":"latest",
                    "offer":"WindowsServer"
                 },
                 "osDisk":{
                    "caching":"ReadWrite",
                    "managedDisk":{
                       "storageAccountType":"StandardSSD_LRS"
                    },
                    "createOption":"FromImage"
                 }
              },
              "osProfile":{
                 "computerNamePrefix":"myVMSS",
                 "adminUsername":"azureuser",
                 "adminPassword":"myPassword12"
              },
              "networkProfile":{
                 "networkInterfaceConfigurations":[
                    {
                       "name":"myVMSS",
                       "properties":{
                          "primary":true,
                          "enableIPForwarding":true,
                          "ipConfigurations":[
                             {
                                "name":"myVMSS",
                                "properties":{
                                   "subnet":{
                                      "id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"
                                   }
                                }
                             }
                          ]
                       }
                    }
                 ]
              }
           },
           "upgradePolicy":{
              "mode":"Manual"
           }
        }
     }
    

    API 版本 2017-12-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PUT -d '{"sku":{"tier":"Standard","capacity":3,"name":"Standard_D1_v2"},"location":"eastus","identity":{"type":"UserAssigned","identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]},"properties":{"overprovision":true,"virtualMachineProfile":{"storageProfile":{"imageReference":{"sku":"2016-Datacenter","publisher":"MicrosoftWindowsServer","version":"latest","offer":"WindowsServer"},"osDisk":{"caching":"ReadWrite","managedDisk":{"storageAccountType":"StandardSSD_LRS"},"createOption":"FromImage"}},"osProfile":{"computerNamePrefix":"myVMSS","adminUsername":"azureuser","adminPassword":"myPassword12"},"networkProfile":{"networkInterfaceConfigurations":[{"name":"myVMSS","properties":{"primary":true,"enableIPForwarding":true,"ipConfigurations":[{"name":"myVMSS","properties":{"subnet":{"id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"}}}]}}]}},"upgradePolicy":{"mode":"Manual"}}}' -H "Content-Type: application/json" -H "Authorization: Bearer <ACCESS TOKEN>"
    
    PUT https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "sku":{
           "tier":"Standard",
           "capacity":3,
           "name":"Standard_D1_v2"
        },
        "location":"eastus",
        "identity":{
           "type":"UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        },
        "properties":{
           "overprovision":true,
           "virtualMachineProfile":{
              "storageProfile":{
                 "imageReference":{
                    "sku":"2016-Datacenter",
                    "publisher":"MicrosoftWindowsServer",
                    "version":"latest",
                    "offer":"WindowsServer"
                 },
                 "osDisk":{
                    "caching":"ReadWrite",
                    "managedDisk":{
                       "storageAccountType":"StandardSSD_LRS"
                    },
                    "createOption":"FromImage"
                 }
              },
              "osProfile":{
                 "computerNamePrefix":"myVMSS",
                 "adminUsername":"azureuser",
                 "adminPassword":"myPassword12"
              },
              "networkProfile":{
                 "networkInterfaceConfigurations":[
                    {
                       "name":"myVMSS",
                       "properties":{
                          "primary":true,
                          "enableIPForwarding":true,
                          "ipConfigurations":[
                             {
                                "name":"myVMSS",
                                "properties":{
                                   "subnet":{
                                      "id":"/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVnet/subnets/mySubnet"
                                   }
                                }
                             }
                          ]
                       }
                    }
                 ]
              }
           },
           "upgradePolicy":{
              "mode":"Manual"
           }
        }
     }
    

將使用者指派的受控識別指派給現有的 Azure 虛擬機器擴展集

  1. 擷取持有人存取權杖,在下一個步驟中,會使用此權杖搭配搭配系統指派的受控識別,在授權標頭中建立虛擬機器擴展集。

    az account get-access-token
    
  2. 使用以下找到的指示建立使用者指派的受控識別:建立使用者指派的受控識別

  3. 若要確保您不會刪除指派給虛擬機器擴展集的任何現有使用者或系統指派識別,需要使用下列 CURL 命令來列出指派給虛擬機器擴展集的識別類型。 若有指派給虛擬機器擴展集的受控識別,則會列在 identity 值內。

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>"
    
    GET https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    授權 必要。 設定為有效的 Bearer 存取權杖。
  4. 如果您沒有任何使用者或系統指派的受控識別指派給您的虛擬機器擴展集,請使用下列 CURL 命令呼叫 Azure Resource Manager REST 端點,將第一個使用者指派的受控識別指派給虛擬機器擴展集。 如有使用者或系統指派的受控識別是指派給虛擬機器擴展集,請跳至步驟 5,此步驟將說明如何將多個使用者指派的受控識別新增到虛擬機器擴展集,同時維持系統指派的受控識別。

    下列範例會將使用者指派的受控識別 ID1 指派給資源群組 myResourceGroup 中名為 myVMSS 的虛擬機器擴展集。 將上一個步驟中要求持有人存取權杖時所收到的值用以取代 <ACCESS TOKEN>值,<SUBSCRIPTION ID> 的值則為適用於您的環境的值。

    API 版本 2018-06-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-12-01' -X PATCH -d '{"identity":{"type":"userAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{}}}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"userAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
    
              }
           }
        }
     }
    

    API 版本 2017-12-01

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"userAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"userAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        }
     }
    
  5. 如果您將現有的使用者指派或系統指派受控識別指派給虛擬機器擴展集:

    API 版本 2018-06-01

    將使用者指派的受控識別新增至 userAssignedIdentities 字典值。

    例如,如果您目前將系統指派的受控識別和使用者指派的受控識別 ID1 指派給虛擬機器擴展集,而且想要在其中新增使用者指派的受控識別 ID2

    curl  'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{},"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{}}}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned, UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1":{
    
              },
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":{
    
              }
           }
        }
     }
    

    API 版本 2017-12-01

    保留您想要留在 identityIds 陣列值內的使用者指派受控識別,同時新增使用者指派的受控識別。

    例如,如果您目前將系統指派的受控識別和使用者指派的受控識別 ID1 指派給虛擬機器擴展集,而且想要在其中新增使用者指派的受控識別 ID2

    curl  'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1","/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"]}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned, UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1",
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2"
           ]
        }
     }
    

從虛擬機器擴展集移除使用者指派的受控識別

  1. 擷取持有人存取權杖,在下一個步驟中,會使用此權杖搭配搭配系統指派的受控識別,在授權標頭中建立虛擬機器擴展集。

    az account get-access-token
    
  2. 若要確保您不會刪除您要維持指派給虛擬機器擴展集的任何現有使用者指派受控識別,或者要移除系統指派的受控識別,需要使用下列 CURL 命令來列出受控識別:

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01' -H "Authorization: Bearer <ACCESS TOKEN>" 
    
    GET https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Compute/virtualMachineScaleSets/<VMSS NAME>?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    授權 必要。 設定為有效的 Bearer 存取權杖。

    若您有指派給虛擬機器的受控識別,則會在 identity 值的回應中列出。

    例如,如果您將使用者指派的受控識別 ID1ID2 指派給虛擬機器擴展集,而且只想要繼續指派 ID1,並保留系統指派的受控識別:

    API 版本 2018-06-01

    null 新增至您想要移除的使用者指派受控識別:

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned, UserAssigned", "userAssignedIdentities":{"/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":null}}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned, UserAssigned",
           "userAssignedIdentities":{
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID2":null
           }
        }
     }
    

    API 版本 2017-12-01

    只保留您想要留在 identityIds 陣列中的使用者指派受控識別:

    curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01' -X PATCH -d '{"identity":{"type":"SystemAssigned,UserAssigned", "identityIds":["/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"]}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
    
    PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2017-12-01 HTTP/1.1
    

    要求標頭

    要求標頭 描述
    Content-Type 必要。 設定為 application/json
    授權 必要。 設定為有效的 Bearer 存取權杖。

    要求本文

     {
        "identity":{
           "type":"SystemAssigned,UserAssigned",
           "identityIds":[
              "/subscriptions/<SUBSCRIPTION ID>/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/ID1"
           ]
        }
     }
    

若您的虛擬機器擴展集同時具有系統指派和使用者指派的受控識別,只要使用下列命令切換為僅使用系統指派的受控識別,即可移除所有使用者指派的受控識別:

curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"SystemAssigned"}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1

要求標頭

要求標頭 描述
Content-Type 必要。 設定為 application/json
授權 必要。 設定為有效的 Bearer 存取權杖。

要求本文

{
   "identity":{
      "type":"SystemAssigned"
   }
}

若您的虛擬機器擴展集只有使用者指派的受控識別,而且您想要全部移除,請使用下列命令:

curl 'https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01' -X PATCH -d '{"identity":{"type":"None"}}' -H "Content-Type: application/json" -H Authorization:"Bearer <ACCESS TOKEN>"
PATCH https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/myResourceGroup/providers/Microsoft.Compute/virtualMachineScaleSets/myVMSS?api-version=2018-06-01 HTTP/1.1

要求標頭

要求標頭 描述
Content-Type 必要。 設定為 application/json
授權 必要。 設定為有效的 Bearer 存取權杖。

要求本文

{
   "identity":{
      "type":"None"
   }
}

下一步

如需如何使用 REST 建立、列出或刪除使用者指派的受控識別相關資訊,請參閱: