Azure 自訂角色 (機器翻譯)

如果 Azure 內建角色不符合組織的特定需求,您可以建立自己的自定義角色。 如同內建角色,您可將自訂角色指派給管理群組、訂用帳戶和資源群組範圍內的使用者、群組和服務主體。

自定義角色可以在信任相同 Microsoft Entra 租使用者的訂用帳戶之間共用。 每個租使用者的限制為 5,000 個自定義角色。 (對於由 21Vianet 運作的 Microsoft Azure,限制為 2,000 個自定義角色。您可以使用 Azure 入口網站、Azure PowerShell、Azure CLI 或 REST API 來建立自定義角色。

建立自定義角色的步驟

以下是建立自定義角色的基本步驟。

  1. 判斷您需要的許可權。

    當您建立自定義角色時,必須知道可用來定義許可權的動作。 一般而言,您會從現有的內建角色開始,然後根據您的需求加以修改。 您會將動作新增至Actions角色定義的NotActions 屬性。 如果您有數據動作,您會將這些動作新增至 DataActionsNotDataActions 屬性。

    如需詳細資訊,請參閱下一節 如何判斷您需要的許可權。

  2. 決定您想要如何建立自定義角色。

    您可以使用 Azure 入口網站、Azure PowerShellAzure CLIREST API 來建立自定義角色。

  3. 建立自定義角色。

    最簡單的方式是使用 Azure 入口網站。 如需如何使用 Azure 入口網站 建立自定義角色的步驟,請參閱使用 Azure 入口網站 建立或更新 Azure 自定義角色。

  4. 測試自定義角色。

    擁有自定義角色之後,您必須測試它,以確認其如預期般運作。 如果您需要稍後進行調整,您可以更新自定義角色。

如何判斷您所需的權限

Azure 有數千個許可權,您可以納入自定義角色。 以下是一些可協助您判斷要新增至自定義角色的許可權的方法:

  • 查看現有的 內建角色

    您可能想要修改現有的角色,或合併多個角色中使用的許可權。

  • 列出您想要授與存取權的 Azure 服務。

  • 判斷對應 至 Azure 服務的資源提供者。

    Azure 服務會透過 資源提供者公開其功能和許可權。 例如,Microsoft.Compute 資源提供者會提供虛擬機資源,而 Microsoft.Billing 資源提供者會提供訂用帳戶和計費資源。 了解資源提供者可協助您縮小範圍,並判斷自定義角色所需的許可權。

    當您使用 Azure 入口網站 建立自定義角色時,您也可以搜尋關鍵詞來判斷資源提供者。 使用 Azure 入口網站 建立或更新 Azure 自定義角色中會說明這項搜尋功能。

    Add permissions pane with resource provider

  • 搜尋可用的許可權,以尋找您想要包含的許可權。

    當您使用 Azure 入口網站 建立自定義角色時,您可以依關鍵詞搜尋許可權。 例如,您可以搜尋 虛擬機計費 許可權。 您也可以下載所有許可權作為 CSV 檔案,然後搜尋此檔案。 使用 Azure 入口網站 建立或更新 Azure 自定義角色中會說明這項搜尋功能。

    Add permissions list

自訂角色範例

以下顯示使用 JSON 格式的 Azure PowerShell 顯示自定義角色的外觀。 此自定義角色可用於監視和重新啟動虛擬機。

{
  "Name": "Virtual Machine Operator",
  "Id": "88888888-8888-8888-8888-888888888888",
  "IsCustom": true,
  "Description": "Can monitor and restart virtual machines.",
  "Actions": [
    "Microsoft.Storage/*/read",
    "Microsoft.Network/*/read",
    "Microsoft.Compute/*/read",
    "Microsoft.Compute/virtualMachines/start/action",
    "Microsoft.Compute/virtualMachines/restart/action",
    "Microsoft.Authorization/*/read",
    "Microsoft.ResourceHealth/availabilityStatuses/read",
    "Microsoft.Resources/subscriptions/resourceGroups/read",
    "Microsoft.Insights/alertRules/*",
    "Microsoft.Insights/diagnosticSettings/*",
    "Microsoft.Support/*"
  ],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": [
    "/subscriptions/{subscriptionId1}",
    "/subscriptions/{subscriptionId2}",
    "/providers/Microsoft.Management/managementGroups/{groupId1}"
  ]
}

以下顯示與使用 Azure CLI 顯示相同的自定義角色。

[
  {
    "assignableScopes": [
      "/subscriptions/{subscriptionId1}",
      "/subscriptions/{subscriptionId2}",
      "/providers/Microsoft.Management/managementGroups/{groupId1}"
    ],
    "description": "Can monitor and restart virtual machines.",
    "id": "/subscriptions/{subscriptionId1}/providers/Microsoft.Authorization/roleDefinitions/88888888-8888-8888-8888-888888888888",
    "name": "88888888-8888-8888-8888-888888888888",
    "permissions": [
      {
        "actions": [
          "Microsoft.Storage/*/read",
          "Microsoft.Network/*/read",
          "Microsoft.Compute/*/read",
          "Microsoft.Compute/virtualMachines/start/action",
          "Microsoft.Compute/virtualMachines/restart/action",
          "Microsoft.Authorization/*/read",
          "Microsoft.ResourceHealth/availabilityStatuses/read",
          "Microsoft.Resources/subscriptions/resourceGroups/read",
          "Microsoft.Insights/alertRules/*",
          "Microsoft.Insights/diagnosticSettings/*",
          "Microsoft.Support/*"
        ],
        "dataActions": [],
        "notActions": [],
        "notDataActions": []
      }
    ],
    "roleName": "Virtual Machine Operator",
    "roleType": "CustomRole",
    "type": "Microsoft.Authorization/roleDefinitions"
  }
]

自訂角色屬性

下表描述自定義角色屬性的意義。

屬性 必要 類型​​ 描述
Name
roleName
Yes String 自訂角色的顯示名稱。 雖然角色定義是管理群組或訂用帳戶層級資源,但角色定義可以在共用相同 Microsoft Entra 租使用者的多個訂用帳戶中使用。 此顯示名稱在 Microsoft Entra 租使用者的範圍內必須是唯一的。 可以包含字母、數位、空格和特殊字元。 字元數上限為 512。
Id
name
Yes String 自定義角色的唯一標識碼。 針對 Azure PowerShell 和 Azure CLI,當您建立新角色時,會自動產生此標識符。
IsCustom
roleType
Yes String 指出這是否為自定義角色。 設定為 trueCustomRole 用於自定義角色。 設定為 falseBuiltInRole 用於內建角色。
Description
description
Yes String 自訂角色的描述。 可以包含字母、數位、空格和特殊字元。 字元數上限為 2048。
Actions
actions
Yes String[] 字串陣列,指定角色允許執行的控制平面動作。 如需詳細資訊,請參閱 動作
NotActions
notActions
No String[] 字串陣列,指定從允許 Actions排除的控制平面動作。 如需詳細資訊,請參閱 NotActions
DataActions
dataActions
No String[] 字串數位,指定角色允許對該物件內的數據執行的數據平面動作。 如果您使用 建立自定義角色 DataActions,則無法在管理群組範圍中指派該角色。 如需詳細資訊,請參閱 DataActions
NotDataActions
notDataActions
No String[] 字串陣列,指定從允許 DataActions排除的數據平面動作。 如需詳細資訊,請參閱 NotDataActions
AssignableScopes
assignableScopes
Yes String[] 字串陣列,指定自定義角色可供指派的範圍。 的最大數目 AssignableScopes 為 2,000。 如需詳細資訊,請參閱 AssignableScopes

許可權字串不區分大小寫。 當您建立自定義角色時,慣例是符合您在 Azure 資源提供者作業中看到的許可權案例。

萬用字元權限

ActionsNotActionsDataActionsNotDataActions 支援通配符 (*) 來定義許可權。 通配符 (*) 會將許可權延伸至符合您提供的動作字串的所有專案。 例如,假設您想要新增與 Azure 成本管理和匯出相關的所有許可權。 您可以新增所有這些動作字串:

Microsoft.CostManagement/exports/action
Microsoft.CostManagement/exports/read
Microsoft.CostManagement/exports/write
Microsoft.CostManagement/exports/delete
Microsoft.CostManagement/exports/run/action

您可以只新增通配符字串,而不是新增所有這些字串。 例如,下列通配符字串相當於前五個字元串。 這也包括可能新增的任何未來導出許可權。

Microsoft.CostManagement/exports/*

注意

建議您明確指定 ActionsDataActions 而不是使用通配符 (*) 字元。 透過未來 Actions 授與的其他存取權和許可權,或使用 DataActions 通配符可能不想要的行為。

誰可以建立、刪除、更新或檢視自訂角色

就像內建角色一樣, AssignableScopes 屬性會指定角色可供指派的範圍。 AssignableScopes自定義角色的屬性也會控制誰可以建立、刪除、更新或檢視自定義角色。

Task 動作 描述
建立/刪除自定義角色 Microsoft.Authorization/ roleDefinitions/write 授與此動作的所有 AssignableScopes 自定義角色的使用者,可以建立或刪除自定義角色,以供這些範圍使用。 例如,管理群組、訂用帳戶和資源群組的擁有者和使用者存取 管理員 管理員。
更新自定義角色 Microsoft.Authorization/ roleDefinitions/write 授與此動作的所有 AssignableScopes 自定義角色的使用者都可以在這些範圍中更新自定義角色。 例如,管理群組、訂用帳戶和資源群組的擁有者和使用者存取 管理員 管理員
檢視自定義角色 Microsoft.Authorization/ roleDefinitions/read 在範圍中授與此動作的使用者,可以檢視可在該範圍指派的自定義角色。 所有內建角色都允許自訂角色以供指派。

尋找要刪除自定義角色的角色指派

您必須先移除任何使用自定義角色的角色指派,才能刪除自定義角色。 如果您嘗試刪除具有角色指派的自定義角色,您會收到訊息: There are existing role assignments referencing role (code: RoleDefinitionHasAssignments)

以下是在刪除自定義角色之前,協助尋找角色指派的步驟:

自訂角色限制

下列清單會說明自訂角色的限制。

  • 每個租使用者最多可以有 5000 個自定義角色。
  • 由 21Vianet 運作的 Microsoft Azure 每個租使用者最多可以有 2000 個自定義角色。
  • 您無法將 設定 AssignableScopes 為根範圍 ("/")。
  • 您不能在 中使用AssignableScopes通配符 (*) 。 此萬用字元限制有助於確保使用者無法藉由更新角色定義來取得範圍的存取權。
  • 動作字串中只能有一個萬用字元。
  • 您只能在自訂角色的 AssignableScopes 中定義一個管理群組。
  • Azure Resource Manager 不會驗證管理群組是否存在於角色定義的 AssignableScopes中。
  • 無法在管理群組範圍上指派具有 DataActions 的自訂角色。
  • 您可以在 中建立具有 DataActions 和一個管理群組的 AssignableScopes自定義角色。 您無法在管理群組範圍本身指派自定義角色;不過,您可以在管理群組內的訂用帳戶範圍內指派自定義角色。 如果您需要使用多個訂用帳戶中指派的單一自定義角色來建立單一自定義角色 DataActions ,而不是為每個訂用帳戶建立個別的自定義角色,這會很有説明。

如需自訂角色和管理群組的詳細資訊,請參閱什麼是 Azure 管理群組?

輸入和輸出格式

若要使用命令行建立自定義角色,您通常會使用 JSON 來指定自定義角色的屬性。 視您使用的工具而定,輸入和輸出格式看起來會稍有不同。 本節會根據工具列出輸入和輸出格式。

Azure PowerShell

若要使用 Azure PowerShell 建立自定義角色,您必須提供下列輸入。

{
  "Name": "",
  "Description": "",
  "Actions": [],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": []
}

若要使用 Azure PowerShell 更新自定義角色,您必須提供下列輸入。 請注意, Id 已新增 屬性。

{
  "Name": "",
  "Id": "",
  "Description": "",
  "Actions": [],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": []
}

下列顯示當您使用 Azure PowerShell 和 ConvertTo-Json 命令列出自定義角色時的輸出範例。

{
  "Name": "",
  "Id": "",
  "IsCustom": true,
  "Description": "",
  "Actions": [],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": []
}

Azure CLI

若要使用 Azure CLI 建立或更新自定義角色,您必須提供下列輸入。 當您使用 Azure PowerShell 建立自定義角色時,此格式會相同。

{
  "Name": "",
  "Description": "",
  "Actions": [],
  "NotActions": [],
  "DataActions": [],
  "NotDataActions": [],
  "AssignableScopes": []
}

以下顯示當您使用 Azure CLI 列出自訂角色時的輸出範例。

[
  {
    "assignableScopes": [],
    "description": "",
    "id": "",
    "name": "",
    "permissions": [
      {
        "actions": [],
        "dataActions": [],
        "notActions": [],
        "notDataActions": []
      }
    ],
    "roleName": "",
    "roleType": "CustomRole",
    "type": "Microsoft.Authorization/roleDefinitions"
  }
]

REST API

若要使用 REST API 建立或更新自訂角色,您必須提供下列輸入。 此格式與您使用 Azure 入口網站 建立自訂角色時所產生的格式相同。

{
  "properties": {
    "roleName": "",
    "description": "",
    "assignableScopes": [],
    "permissions": [
      {
        "actions": [],
        "notActions": [],
        "dataActions": [],
        "notDataActions": []
      }
    ]
  }
}

以下顯示當您使用 REST API 列出自訂角色時的輸出範例。

{
    "properties": {
        "roleName": "",
        "type": "CustomRole",
        "description": "",
        "assignableScopes": [],
        "permissions": [
            {
                "actions": [],
                "notActions": [],
                "dataActions": [],
                "notDataActions": []
            }
        ],
        "createdOn": "",
        "updatedOn": "",
        "createdBy": "",
        "updatedBy": ""
    },
    "id": "",
    "type": "Microsoft.Authorization/roleDefinitions",
    "name": ""
}

下一步