Azure 自訂角色

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

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

建立自訂角色的步驟

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

  1. 判斷所需權限。

    在建立自訂角色時,您需要了解可定義權限的動作。 一般而言,您可以從使用現有的內建角色開始,然後針對您的需求進行修改。 您需要將動作新增至Actions的 NotActions 或 屬性。 如果您有資料動作,可以將其新增至 DataActions 或 NotDataActions 屬性。

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

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

    您可以使用 Azure 入口網站、Azure PowerShell、Azure CLI 或 REST API 來建立自訂角色。

  3. 建立自訂角色。

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

  4. 測試自訂角色。

    一旦具有自訂角色,您必須測試它來驗證是否如預期般運作。 稍後如需進行調整,您可以更新自訂角色。

如何判斷您所需的權限

Azure 有數千種權限,您可以將其包含在自訂角色中。 以下是一些方法,可協助您判斷要新增至自訂角色的權限:

  • 查看現有的內建角色。

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

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

  • 識別對應至 Azure 服務的資源提供者。

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

    當您使用 Azure 入口網站建立自訂角色時,也可以藉由搜尋關鍵字來判斷資源提供者。 如需此搜尋功能的詳細資訊,請參閱使用 Azure 入口網站建立或更新 Azure 自訂角色。

    新增包含資源提供者的權限窗格

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

    當您使用 Azure 入口網站建立自訂角色時,可以依關鍵字來搜尋權限。 例如,您可以搜尋「虛擬機器」或「計費」權限。 您也可以將所有權限下載為 CSV 檔案,然後搜尋此檔案。 如需此搜尋功能的詳細資訊,請參閱使用 Azure 入口網站建立或更新 Azure 自訂角色。

    新增權限清單

自訂角色範例

以下顯示使用 Azure PowerShell 以 JSON 格式顯示時,自訂角色的樣子。 此自訂角色可用於監視和重新啟動虛擬機器。

{
  "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 租用戶範疇內必須是唯一的。 可以包含字母、數字、空格和特殊字元。 建議的最大值:256 個字元。 最多:512 字元。
Id
name
Yes String 自訂角色的唯一識別碼。 針對 Azure PowerShell 和 Azure CLI,當您建立新角色時,會自動產生這個識別碼。
IsCustom
roleType
Yes String 表示這是否為自訂角色。 若為自訂角色,請設為 true 或 CustomRole。 若為內建角色,請設為 false 或 BuiltInRole。
Description
description
Yes String 自訂角色的描述。 可以包含字母、數字、空格和特殊字元。 建議的最大值:512 個字元。 最大字元數:2,048 字元。
Actions
actions
Yes String[] 字串陣列,用來指定角色允許執行的控制平面動作。 如需詳細資訊,請參閱 Actions。
NotActions
notActions
否 String[] 字串陣列,用於指定從允許的 Actions 中排除的控制平面動作。 如需詳細資訊,請參閱 NotActions。
DataActions
dataActions
否 String[] 字串陣列,用於指定角色允許在該物件內對您的資料執行的資料平面動作。 如果您使用 DataActions 來建立自訂角色,則無法在管理群組範圍指派該角色。 如需詳細資訊,請參閱 DataActions。
NotDataActions
notDataActions
否 String[] 字串陣列,用於指定從允許的 DataActions 中排除的資料平面動作。 如需詳細資訊,請參閱 NotDataActions。
AssignableScopes
assignableScopes
Yes String[] 字串陣列,指定自訂角色可用於指派的範圍。 AssignableScopes 的數目上限為 2,000。 如需詳細資訊,請參閱 AssignableScopes。

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

萬用字元權限

Actions、NotActions、DataActions 和 NotDataActions 支援萬用字元 (*) 來定義權限。 萬用字元 (*) 會將權限延伸到符合所提供動作字串的所有項目。 例如,假設您想要新增與 Microsoft 成本管理和匯出相關的所有許可權。 您可以新增所有這些動作字串:

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

與其新增所有這些字串,不如只新增一個萬用字元字串。 例如,下列萬用字元字串的意義等同前五個字串。 其中也包括可能新增的任何未來匯出權限。

Microsoft.CostManagement/exports/*

備註

但與其使用萬用字元 (Actions),建議您還是明確指定 DataActions 和 * 為佳。 其他透過未來 Actions 或 DataActions 授與的存取權和使用權限,可能會於使用萬用字元時出現不想要的行為。

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

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

任務 動作 描述
建立/刪除自訂角色 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": ""
}

下一步