如果 Azure 内置角色不满足组织的特定需求,你可以创建自己的自定义角色。 本文介绍如何使用 Azure 资源管理器模板(ARM 模板)来创建或更新自定义角色。
Azure 资源管理器模板是定义项目基础结构和配置的 JavaScript 对象表示法 (JSON) 文件。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。
若要创建自定义角色,请指定角色名称、权限以及可使用角色的位置。 在本文中,你将创建一个名为“自定义角色 - RG 读者”的角色,其资源权限可在订阅或更低层次的范围内分配。
如果你的环境满足先决条件,并且你熟悉如何使用 ARM 模板,请选择“部署到 Azure”按钮。 Azure 门户中会打开模板。
先决条件
若要创建自定义角色,必须:
- 创建自定义角色的权限,例如用户访问管理员。
必须使用以下版本:
- 2018-07-01或更高版本
有关详细信息,请参阅 Azure RBAC REST API 的 API 版本。
查看模板
本文中使用的模板来自 Azure 快速入门模板。 该模板具有四个参数和一个资源部分。 这四个参数为:
- 默认值为 ["Microsoft.Resources/subscriptions/resourceGroups/read"]的操作数组。
- 默认值为空的 notActions数组。
- 默认值为 Custom Role - RG Reader的角色名称。
- 默认值为 Subscription Level Deployment of a Role Definition的角色说明。
将可分配此自定义角色的范围设置为当前订阅。
{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.25.53.49325",
      "templateHash": "16704138909949665309"
    }
  },
  "parameters": {
    "actions": {
      "type": "array",
      "defaultValue": [
        "Microsoft.Resources/subscriptions/resourceGroups/read"
      ],
      "metadata": {
        "description": "Array of actions for the roleDefinition"
      }
    },
    "notActions": {
      "type": "array",
      "defaultValue": [],
      "metadata": {
        "description": "Array of notActions for the roleDefinition"
      }
    },
    "roleName": {
      "type": "string",
      "defaultValue": "Custom Role - RG Reader",
      "metadata": {
        "description": "Friendly name of the role definition"
      }
    },
    "roleDescription": {
      "type": "string",
      "defaultValue": "Subscription Level Deployment of a Role Definition",
      "metadata": {
        "description": "Detailed description of the role definition"
      }
    }
  },
  "variables": {
    "roleDefName": "[guid(parameters('roleName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Authorization/roleDefinitions",
      "apiVersion": "2022-04-01",
      "name": "[variables('roleDefName')]",
      "properties": {
        "roleName": "[parameters('roleName')]",
        "description": "[parameters('roleDescription')]",
        "type": "customRole",
        "permissions": [
          {
            "actions": "[parameters('actions')]",
            "notActions": "[parameters('notActions')]"
          }
        ],
        "assignableScopes": [
          "[subscription().id]"
        ]
      }
    }
  ]
}
该模板中定义了以下资源:
部署模板
请遵照以下步骤部署上一模板。
- 登录到 Azure 门户。 
- 打开 PowerShell。 登录到你的订阅,然后复制并粘贴以下脚本。 - $location = Read-Host -Prompt "Enter a location (i.e. chinanorth)" [string[]]$actions = Read-Host -Prompt "Enter actions as a comma-separated list (i.e. action1,action2)" $actions = $actions.Split(',') $templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/subscription-deployments/create-role-def/azuredeploy.json" New-AzDeployment -Location $location -TemplateUri $templateUri -actions $actions
- 输入部署的位置,例如 chinanorth。 
- 以逗号分隔的列表(如 - Microsoft.Resources/resources/read,Microsoft.Resources/subscriptions/resourceGroups/read)形式输入自定义角色的操作列表。
- 如有必要,请按 Enter 运行 - New-AzDeployment命令。- New-AzDeployment 命令部署模板来创建自定义角色。 - 会得到类似于下面的输出: - PS> New-AzDeployment -Location $location -TemplateUri $templateUri -actions $actions Id : /subscriptions/{subscriptionId}/providers/Microsoft.Resources/deployments/azuredeploy DeploymentName : azuredeploy Location : chinanorth ProvisioningState : Succeeded Timestamp : 6/25/2020 8:08:32 PM Mode : Incremental TemplateLink : Uri : https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/subscription-deployments/create-role-def/azuredeploy.json ContentVersion : 1.0.0.0 Parameters : Name Type Value ================= ========================= ========== actions Array [ "Microsoft.Resources/resources/read", "Microsoft.Resources/subscriptions/resourceGroups/read" ] notActions Array [] roleName String Custom Role - RG Reader roleDescription String Subscription Level Deployment of a Role Definition Outputs : DeploymentDebugLogLevel :
查看已部署的资源
按照以下步骤验证是否已创建自定义角色。
- 运行 Get-AzRoleDefinition 命令以列出自定义角色。 - Get-AzRoleDefinition "Custom Role - RG Reader" | ConvertTo-Json- 应该会看到与下面类似的输出: - { "Name": "Custom Role - RG Reader", "Id": "11111111-1111-1111-1111-111111111111", "IsCustom": true, "Description": "Subscription Level Deployment of a Role Definition", "Actions": [ "Microsoft.Resources/resources/read", "Microsoft.Resources/subscriptions/resourceGroups/read" ], "NotActions": [], "DataActions": [], "NotDataActions": [], "AssignableScopes": [ "/subscriptions/{subscriptionId}" ] }
- 在 Azure 门户中,打开你的订阅。 
- 在左侧菜单中,选择“访问控制(IAM)”。 
- 选择“角色”选项卡。 
- 将“类型”列表设置为 CustomRole 。 
- 验证是否列出了“自定义角色 - RG 读者”角色。  
更新自定义角色
与创建自定义角色类似,可以通过使用模板来更新现有的自定义角色。 若要更新自定义角色,必须指定需要更新的角色。
下面是为了更新自定义角色而需要对上一个快速入门模板做出的更改。
- 将角色 ID 作为参数包括在内。 - ... "roleDefName": { "type": "string", "metadata": { "description": "ID of the role definition" } ...
- 在角色定义中包括角色 ID 参数。 - ... "resources": [ { "type": "Microsoft.Authorization/roleDefinitions", "apiVersion": "2022-04-01", "name": "[parameters('roleDefName')]", "properties": { ...
以下示例演示如何部署该模板。
$location = Read-Host -Prompt "Enter a location (i.e. chinanorth)"
[string[]]$actions = Read-Host -Prompt "Enter actions as a comma-separated list (i.e. action1,action2)"
$actions = $actions.Split(',')
$roleDefName = Read-Host -Prompt "Enter the role ID to update"
$templateFile = "rg-reader-update.json"
New-AzDeployment -Location $location -TemplateFile $templateFile -actions $actions -roleDefName $roleDefName
清理资源
若要删除自定义角色,请按照以下步骤操作。
- 运行以下命令以删除自定义角色。 - Get-AzRoleDefinition -Name "Custom Role - RG Reader" | Remove-AzRoleDefinition
- 输入“Y”以确认要删除该自定义角色。