使用模板在 Azure VM 上配置 Azure 资源的托管标识

Azure 资源的托管标识是 Microsoft Entra ID 的一项功能。 支持 Azure 资源的托管标识的每个 Azure 服务都受其自己的时间线限制。 在开始之前,请务必查看资源的托管标识的可用性状态以及已知问题

Azure 资源的托管标识在 Microsoft Entra ID 中为 Azure 服务提供了一个自动托管标识。 可以使用此标识向支持 Microsoft Entra 身份验证的任何服务进行身份验证,这样就无需在代码中插入凭据了。

本文将介绍如何在 Azure VM 上使用 Azure 资源管理器部署模板来执行以下 Azure 资源的托管标识操作:

先决条件

Azure Resource Manager 模板

与 Azure 门户和脚本一样,Azure 资源管理器模板可用于部署由 Azure 资源组定义的新资源或修改后的资源。 有多种可用于执行模板编辑和部署的方法(包括本地方法和基于门户的方法),包括:

无论选择哪种方法,在初始部署和重新部署期间,模板语法都是相同的。 在新的或现有 VM 上启用系统或用户分配的托管标识所采用的方式是相同的。 此外,默认情况下,Azure 资源管理器还会对部署执行增量更新

系统分配的托管标识

在此部分中,将使用 Azure 资源管理器模板启用和禁用系统分配的托管标识。

在创建 Azure VM 的过程中或在现有 VM 上启用系统分配的托管标识

若要在 VM 上启用系统分配的托管标识,你的帐户需要虚拟机参与者角色分配。 不需要其他 Microsoft Entra 目录角色分配。

  1. 无论是在本地登录到 Azure 还是通过 Azure 门户登录,请使用与包含 VM 的 Azure 订阅关联的帐户。

  2. 若要启用系统分配的托管标识,请将模板加载到编辑器中,在 resources 节中找到所关注的 Microsoft.Compute/virtualMachines 资源,并在与 "type": "Microsoft.Compute/virtualMachines" 属性相同的级别添加 "identity" 属性。 使用以下语法:

    "identity": {
        "type": "SystemAssigned"
    },
    
  3. 完成后,以下各节应当会添加到模板的 resource 节,该节应当呈现如下:

     "resources": [
         {
             //other resource provider properties...
             "apiVersion": "2018-06-01",
             "type": "Microsoft.Compute/virtualMachines",
             "name": "[variables('vmName')]",
             "location": "[resourceGroup().location]",
             "identity": {
                 "type": "SystemAssigned",
                 }                        
         }
     ]
    

向 VM 的系统分配的托管标识分配一个角色

在 VM 上启用系统分配的托管标识后,建议向其授予一个角色,例如对创建它的资源组的“读取者”访问权限。 可以在使用 Azure 资源管理器模板分配 Azure 角色一文中找到有助于完成此步骤的详细信息。

从 Azure VM 中禁用系统分配的托管标识

若要从 VM 中删除系统分配的托管标识,你的帐户需要虚拟机参与者角色分配。 不需要其他 Microsoft Entra 目录角色分配。

  1. 无论是在本地登录到 Azure 还是通过 Azure 门户登录,请使用与包含 VM 的 Azure 订阅关联的帐户。

  2. 将模板加载到编辑器,并在 resources 部分找到相关的 Microsoft.Compute/virtualMachines 资源。 如果 VM 只有系统分配的托管标识,则可以将标识类型更改为 None 来禁用它。

    Microsoft.Compute/virtualMachines API 版本 2018-06-01

    如果 VM 同时具有系统和用户分配的托管标识,请从标识类型中删除 SystemAssigned 并保留 UserAssigned 以及 userAssignedIdentities 字典值。

    Microsoft.Compute/virtualMachines API 版本 2018-06-01

    如果 apiVersion2017-12-01 并且 VM 同时具有系统和用户分配的托管标识,请从标识类型中删除 SystemAssigned 并保留 UserAssigned 以及用户分配的托管标识的 identityIds 数组。

以下示例演示如何从没有用户分配的托管标识的 VM 删除系统分配的托管标识:

{
    "apiVersion": "2018-06-01",
    "type": "Microsoft.Compute/virtualMachines",
    "name": "[parameters('vmName')]",
    "location": "[resourceGroup().location]",
    "identity": {
        "type": "None"
    }
}

用户分配的托管标识

在此部分中,将使用 Azure 资源管理器模板向 Azure VM 分配用户分配的托管标识。

注意

要使用 Azure 资源管理器模板创建用户分配托管标识,请参阅创建用户分配托管标识

向 Azure VM 分配用户分配的托管标识

若要将用户分配的标识分配给 VM,你的帐户需要托管标识操作员角色分配。 不需要其他 Microsoft Entra 目录角色分配。

  1. resources 元素下添加以下条目,以向 VM 分配用户分配的托管标识。 请务必将 <USERASSIGNEDIDENTITY> 替换为你创建的用户分配的托管标识的名称。

    Microsoft.Compute/virtualMachines API 版本 2018-06-01

    如果 apiVersion2018-06-01,则用户分配的托管标识以 userAssignedIdentities 字典格式存储,并且 <USERASSIGNEDIDENTITYNAME> 值必须存储在模板的 variables 节中定义的某个变量中。

     {
         "apiVersion": "2018-06-01",
         "type": "Microsoft.Compute/virtualMachines",
         "name": "[variables('vmName')]",
         "location": "[resourceGroup().location]",
         "identity": {
             "type": "userAssigned",
             "userAssignedIdentities": {
                 "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {}
             }
         }
     }
    

    Microsoft.Compute/virtualMachines API 版本 2017-12-01

    如果 apiVersion2017-12-01,则用户分配的托管标识存储在 identityIds 数组中,并且 <USERASSIGNEDIDENTITYNAME> 值必须存储在模板的 variables 节中定义的某个变量中。

    {
        "apiVersion": "2017-12-01",
        "type": "Microsoft.Compute/virtualMachines",
        "name": "[variables('vmName')]",
        "location": "[resourceGroup().location]",
        "identity": {
            "type": "userAssigned",
            "identityIds": [
                "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]"
            ]
        }
    }
    
  2. 完成后,以下各节应当会添加到模板的 resource 节,该节应当呈现如下:

    Microsoft.Compute/virtualMachines API 版本 2018-06-01

      "resources": [
         {
             //other resource provider properties...
             "apiVersion": "2018-06-01",
             "type": "Microsoft.Compute/virtualMachines",
             "name": "[variables('vmName')]",
             "location": "[resourceGroup().location]",
             "identity": {
                 "type": "userAssigned",
                 "userAssignedIdentities": {
                    "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]": {}
                 }
             }
         }
     ] 
    

    Microsoft.Compute/virtualMachines API 版本 2017-12-01

    "resources": [
         {
             //other resource provider properties...
             "apiVersion": "2017-12-01",
             "type": "Microsoft.Compute/virtualMachines",
             "name": "[variables('vmName')]",
             "location": "[resourceGroup().location]",
             "identity": {
                 "type": "userAssigned",
                 "identityIds": [
                    "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/',variables('<USERASSIGNEDIDENTITYNAME>'))]"
                 ]
             }
         }
    ]
    

从 Azure VM 中删除用户分配的托管标识

若要从 VM 中删除用户分配的标识,你的帐户需要虚拟机参与者角色分配。 不需要其他 Microsoft Entra 目录角色分配。

  1. 无论是在本地登录到 Azure 还是通过 Azure 门户登录,请使用与包含 VM 的 Azure 订阅关联的帐户。

  2. 将模板加载到编辑器,并在 resources 部分找到相关的 Microsoft.Compute/virtualMachines 资源。 如果 VM 只有用户分配的托管标识,则可以通过将标识类型更改为 None 来禁用它。

    以下示例演示如何从没有系统分配的托管标识的 VM 中删除所有用户分配的托管标识:

     {
       "apiVersion": "2018-06-01",
       "type": "Microsoft.Compute/virtualMachines",
       "name": "[parameters('vmName')]",
       "location": "[resourceGroup().location]",
       "identity": {
           "type": "None"
           },
     }
    

    Microsoft.Compute/virtualMachines API 版本 2018-06-01

    若要从 VM 中删除单个用户分配的托管标识,请将其从 useraAssignedIdentities 字典中删除。

    如果具有系统分配的托管标识,请将其保持在 identity 值下的 type 值中。

    Microsoft.Compute/virtualMachines API 版本 2017-12-01

    若要从 VM 中删除单个用户分配的托管标识,请将其从 identityIds 数组中删除。

    如果具有系统分配的托管标识,请将其保持在 identity 值下的 type 值中。

后续步骤