教學課程:使用 Azure CLI 建立 Azure 自訂角色

如果 Azure 內建角色 不符合組織的特定需求,您可以建立自己的自訂角色。 在本教學課程中,您會使用 Azure CLI 建立名為讀者支援票證的自訂角色。 自訂角色可讓使用者在訂用帳戶的控制平面中檢視所有專案,並開啟支援票證。

在本教學課程中,您會了解如何:

  • 建立自訂角色
  • 列出自訂角色
  • 更新自訂角色
  • 刪除自訂角色

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

必要條件

若要完成本教學課程,您將會需要:

登入 Azure CLI

登入 Azure CLI

建立自訂角色

建立自訂角色最簡單的方式,就是從 JSON 範本開始,新增變更,然後建立新的角色。

  1. 檢閱 Microsoft.Support 資源提供者 動作清單。 瞭解可用來建立許可權的動作會很有説明。

    動作 描述
    Microsoft.Support/register/action 向支援資源提供者註冊
    Microsoft.Support/supportTickets/read 取得支援票證詳細資料(包括狀態、嚴重性、連絡人詳細資料和通訊),或取得跨訂用帳戶的支援票證清單。
    Microsoft.Support/supportTickets/write 建立或更新支援票證。 您可以建立技術、計費、配額或訂用帳戶管理相關問題的支援票證。 您可以更新嚴重性、連絡詳細資料和現有支援票證的通訊。
  2. 建立名為 ReaderSupportRole.json 的新檔案。

  3. 在編輯器中開啟 ReaderSupportRole.json,並新增下列 JSON。

    如需不同屬性的相關資訊,請參閱 Azure 自訂角色

    {
      "Name": "",
      "IsCustom": true,
      "Description": "",
      "Actions": [],
      "NotActions": [],
      "DataActions": [],
      "NotDataActions": [],
      "AssignableScopes": [
        "/subscriptions/{subscriptionId1}"
      ]
    }
    
  4. 將下列動作新增至 Actions 屬性。 這些動作可讓使用者檢視訂用帳戶中的所有內容,並建立支援票證。

    "*/read",
    "Microsoft.Support/*"
    
  5. 使用 az account list 命令取得訂用 帳戶的識別碼。

    az account list --output table
    
  6. 在 中 AssignableScopes ,將 取代 {subscriptionId1} 為您的訂用帳戶識別碼。

    您必須新增明確的訂用帳戶識別碼,否則您無法將角色匯入訂用帳戶。

  7. NameDescription 屬性變更為「讀者支援票證」和「檢視訂用帳戶中的所有內容,並開啟支援票證」。

    您的 JSON 檔案看起來應該如下所示:

    {
      "Name": "Reader Support Tickets",
      "IsCustom": true,
      "Description": "View everything in the subscription and also open support tickets.",
      "Actions": [
        "*/read",
        "Microsoft.Support/*"
      ],
      "NotActions": [],
      "DataActions": [],
      "NotDataActions": [],
      "AssignableScopes": [
        "/subscriptions/00000000-0000-0000-0000-000000000000"
      ]
    }
    
  8. 若要建立新的自訂角色,請使用 az role definition create 命令並指定 JSON 角色定義檔案。

    az role definition create --role-definition "~/CustomRoles/ReaderSupportRole.json"
    
    {
      "additionalProperties": {},
      "assignableScopes": [
        "/subscriptions/00000000-0000-0000-0000-000000000000"
      ],
      "description": "View everything in the subscription and also open support tickets.",
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/22222222-2222-2222-2222-222222222222",
      "name": "22222222-2222-2222-2222-222222222222",
      "permissions": [
        {
          "actions": [
            "*/read",
            "Microsoft.Support/*"
          ],
          "additionalProperties": {},
          "dataActions": [],
          "notActions": [],
          "notDataActions": []
        }
      ],
      "roleName": "Reader Support Tickets",
      "roleType": "CustomRole",
      "type": "Microsoft.Authorization/roleDefinitions"
    }
    

    新的自訂角色現已可供使用,而且可以指派給使用者、群組或服務主體,就像內建角色一樣。

列出自訂角色

  • 若要列出所有自訂角色,請使用 az role definition list 命令搭配 --custom-role-only 參數。

    az role definition list --custom-role-only true
    
    [
      {
        "additionalProperties": {},
        "assignableScopes": [
          "/subscriptions/00000000-0000-0000-0000-000000000000"
        ],
        "description": "View everything in the subscription and also open support tickets.",
        "id": "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/22222222-2222-2222-2222-222222222222",
        "name": "22222222-2222-2222-2222-222222222222",
        "permissions": [
          {
            "actions": [
              "*/read",
              "Microsoft.Support/*",
              "Microsoft.Resources/deployments/*",
              "Microsoft.Insights/diagnosticSettings/*/read"
            ],
            "additionalProperties": {},
            "dataActions": [],
            "notActions": [],
            "notDataActions": []
          }
        ],
        "roleName": "Reader Support Tickets",
        "roleType": "CustomRole",
        "type": "Microsoft.Authorization/roleDefinitions"
      }
    ]
    

    您也可以在Azure 入口網站中看到自訂角色。

    screenshot of custom role imported in the Azure portal

更新自訂角色

若要更新自訂角色,請更新 JSON 檔案,然後更新自訂角色。

  1. 開啟 ReaderSupportRole.json 檔案。

  2. 在 中 Actions ,新增動作以建立和管理資源群組部署 "Microsoft.Resources/deployments/*" 。 請務必在上一個動作後面加上逗號。

    更新的 JSON 檔案看起來應該如下所示:

    {
      "Name": "Reader Support Tickets",
      "IsCustom": true,
      "Description": "View everything in the subscription and also open support tickets.",
      "Actions": [
        "*/read",
        "Microsoft.Support/*",
        "Microsoft.Resources/deployments/*"
      ],
      "NotActions": [],
      "DataActions": [],
      "NotDataActions": [],
      "AssignableScopes": [
        "/subscriptions/00000000-0000-0000-0000-000000000000"
      ]
    }
    
  3. 若要更新自訂角色,請使用 az role definition update 命令並指定更新的 JSON 檔案。

    az role definition update --role-definition "~/CustomRoles/ReaderSupportRole.json"
    
    {
      "additionalProperties": {},
      "assignableScopes": [
        "/subscriptions/00000000-0000-0000-0000-000000000000"
      ],
      "description": "View everything in the subscription and also open support tickets.",
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/22222222-2222-2222-2222-222222222222",
      "name": "22222222-2222-2222-2222-222222222222",
      "permissions": [
        {
          "actions": [
            "*/read",
            "Microsoft.Support/*",
            "Microsoft.Resources/deployments/*"
          ],
          "additionalProperties": {},
          "dataActions": [],
          "notActions": [],
          "notDataActions": []
        }
      ],
      "roleName": "Reader Support Tickets",
      "roleType": "CustomRole",
      "type": "Microsoft.Authorization/roleDefinitions"
    }
    

刪除自訂角色

  • 使用 az role definition delete 命令,並指定角色名稱或角色識別碼來刪除自訂角色。

    az role definition delete --name "Reader Support Tickets"
    

下一步