使用 Azure CLI 指派 Azure 角色

Azure 角色型存取控制 (Azure RBAC) 是您用來管理 Azure 資源的存取權的授權系統。 若要授與存取權,您可以將角色指派給特定範圍的使用者、群組、服務主體或受控識別。 本文說明如何使用 Azure CLI 指派角色。

必要條件

若要指派角色,您必須具備:

指派 Azure 角色的步驟

指派角色包含三個元素:安全性主體、角色定義和範圍。

步驟 1:判斷誰需要存取權

您可以將角色指派給使用者、群組、服務主體或受控識別。 若要指派角色,您可能需要指定物件的唯一標識符。 標識碼的格式為: 11111111-1111-1111-1111-111111111111。 您可以使用 Azure 入口網站 或 Azure CLI 來取得識別碼。

使用者

針對 Microsoft Entra 使用者,取得用戶主體名稱,例如 patlong@contoso.com 或使用者對象識別碼。 若要取得物件識別碼,您可以使用 az ad user show

az ad user show --id "{principalName}" --query "id" --output tsv

群組

針對 Microsoft Entra 群組,您需要群組對象識別碼。 若要取得對象識別碼,您可以使用 az ad group showaz ad group list

az ad group show --group "{groupName}" --query "id" --output tsv

服務主體

針對 Microsoft Entra 服務主體(應用程式所使用的身分識別),您需要服務主體對象識別碼。 若要取得物件識別碼,您可以使用 az ad sp list。 針對服務主體,請使用物件標識碼, 而不是 應用程式識別碼。

az ad sp list --all --query "[].{displayName:displayName, id:id}" --output tsv
az ad sp list --display-name "{displayName}"

受控識別

針對系統指派或使用者指派的受控識別,您需要對象識別碼。 若要取得物件識別碼,您可以使用 az ad sp list

az ad sp list --all --filter "servicePrincipalType eq 'ManagedIdentity'"

若要只列出使用者指派的受控識別,您可以使用 az identity list

az identity list

步驟 2:選取適當的角色

許可權會分組為角色。 您可以從數 個 Azure 內建角色 清單中選取,或使用自己的自訂角色。 最佳做法是使用所需的最低許可權來授與存取權,因此請避免指派更廣泛的角色。

若要列出角色並取得唯一的角色識別碼,您可以使用 az role definition list

az role definition list --query "[].{name:name, roleType:roleType, roleName:roleName}" --output tsv

以下說明如何列出特定角色的詳細資料。

az role definition list --name "{roleName}"

如需詳細資訊,請參閱 列出 Azure 角色定義

步驟 3:識別所需的範圍

Azure 提供四個範圍層級:資源、 資源群組 、訂用帳戶和管理 群組 。 最佳做法是使用所需的最低許可權來授與存取權,因此請避免在更廣泛的範圍內指派角色。 如需範圍的詳細資訊,請參閱 瞭解範圍

資源範圍

針對資源範圍,您需要資源的資源識別碼。 您可以在Azure 入口網站中查看資源的屬性,以尋找資源識別碼。 資源識別碼的格式如下。

/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{providerName}/{resourceType}/{resourceSubType}/{resourceName}

資源群組範圍

針對資源群組範圍,您需要資源群組的名稱。 您可以在Azure 入口網站的 [資源群組 ] 頁面上找到名稱 ,也可以使用 az group list

az group list --query "[].{name:name}" --output tsv

訂用帳戶範圍

針對訂用帳戶範圍,您需要訂用帳戶識別碼。 您可以在Azure 入口網站的 [訂 用帳戶] 頁面上找到 識別碼,也可以使用 az account list

az account list --query "[].{name:name, id:id}" --output tsv

管理群組範圍

針對管理群組範圍,您需要管理組名。 您可以在Azure 入口網站的 [管理群組 ] 頁面上找到名稱 ,也可以使用 az account management-group list

az account management-group list --query "[].{name:name, id:id}" --output tsv

步驟 4:指派角色

若要指派角色,請使用 az role assignment create 命令。 根據範圍,命令通常具有下列其中一種格式。

資源範圍

az role assignment create --assignee "{assignee}" \
--role "{roleNameOrId}" \
--scope "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{providerName}/{resourceType}/{resourceSubType}/{resourceName}"

資源群組範圍

az role assignment create --assignee "{assignee}" \
--role "{roleNameOrId}" \
--scope "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}"

訂用帳戶範圍

az role assignment create --assignee "{assignee}" \
--role "{roleNameOrId}" \
--scope "/subscriptions/{subscriptionId}"

管理群組範圍

az role assignment create --assignee "{assignee}" \
--role "{roleNameOrId}" \
--scope "/providers/Microsoft.Management/managementGroups/{managementGroupName}"

以下顯示當您將虛擬機器參與者 角色指派 給資源群組範圍的使用者時,輸出的範例。

{
  "canDelegate": null,
  "id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentId}",
  "name": "{roleAssignmentId}",
  "principalId": "{principalId}",
  "principalType": "User",
  "resourceGroup": "{resourceGroupName}",
  "roleDefinitionId": "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c",
  "scope": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}",
  "type": "Microsoft.Authorization/roleAssignments"
}

指派角色範例

為儲存體帳戶資源範圍中的所有 Blob 容器指派角色

儲存體 Blob 資料參與者 角色指派給物件識別碼 為 55555555-5555-5555-5555-555555555555555555 的服務 主體,該角色位於名為 storage12345 的儲存體帳戶的資源範圍。

az role assignment create --assignee "55555555-5555-5555-5555-555555555555" \
--role "Storage Blob Data Contributor" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/storage12345"

指派特定 Blob 容器資源範圍的角色

儲存體 Blob 資料參與者 角色指派給物件識別碼 為 5555555-5555-5555-5555-5555-55555555555555555, 在名為 blob-container-01 之 Blob 容器的資源範圍的服務主體。

az role assignment create --assignee "55555555-5555-5555-5555-555555555555" \
--role "Storage Blob Data Contributor" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Example-Storage-rg/providers/Microsoft.Storage/storageAccounts/storage12345/blobServices/default/containers/blob-container-01"

為特定虛擬網路資源範圍中的群組指派角色

虛擬機器參與者 角色指派給 識別碼為 222222222-2222-2222-2222-2222-2222222222222222 的 Ann Mack 小組 群組,該群組位於名為 pharma-sales-project-network 的虛擬網路資源範圍。

az role assignment create --assignee "22222222-2222-2222-2222-222222222222" \
--role "Virtual Machine Contributor" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/pharma-sales/providers/Microsoft.Network/virtualNetworks/pharma-sales-project-network"

為資源群組範圍的使用者指派角色

虛擬機器參與者 角色指派給 patlong@contoso.com 製藥銷售 資源群組範圍的使用者

az role assignment create --assignee "patlong@contoso.com" \
--role "Virtual Machine Contributor" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/pharma-sales"

在資源群組範圍使用唯一角色識別碼為使用者指派角色

角色名稱可能會變更幾次,例如:

  • 您正在使用自己的自訂角色,並決定變更名稱。
  • 您使用的是名稱中有 (預覽) 的預覽角色 。 釋放角色時,角色會重新命名。

即使已重新命名角色,角色識別碼也不會變更。 如果您使用腳本或自動化來建立角色指派,最佳做法是使用唯一的角色識別碼,而不是角色名稱。 因此,如果重新命名角色,您的腳本更有可能運作。

下列範例會將 虛擬機器參與者 角色指派給 patlong@contoso.com 製藥銷售 資源群組範圍的使用者

az role assignment create --assignee "patlong@contoso.com" \
--role "9980e02c-c2be-4d73-94e8-173b1dc7cf3c" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/pharma-sales"

為資源群組範圍中的所有 Blob 容器指派角色

儲存體 Blob 資料參與者 角色指派給物件識別碼 為 55555555-5555-5555-5555-5555555555555555 在 Example-儲存體-rg 資源群組範圍的服務 主體。

az role assignment create --assignee "55555555-5555-5555-5555-555555555555" \
--role "Storage Blob Data Contributor" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Example-Storage-rg"

在資源群組範圍指派應用程式的角色

虛擬機器參與者 角色指派給服務主體物件識別碼為 444444444-4444-4444-4444-4444444444444 在醫藥銷售 資源群組範圍的應用程式

az role assignment create --assignee "44444444-4444-4444-4444-444444444444" \
--role "Virtual Machine Contributor" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/pharma-sales"

在資源群組範圍指派新服務主體的角色

如果您建立新的服務主體,並立即嘗試將角色指派給該服務主體,在某些情況下,該角色指派可能會失敗。 例如,如果您使用腳本來建立新的受控識別,然後嘗試將角色指派給該服務主體,角色指派可能會失敗。 此失敗的原因可能是複寫延遲。 服務主體會在一個區域中建立;不過,角色指派可能會發生在尚未複寫服務主體的不同區域中。 若要解決此案例,您應該在建立角色指派時指定主體類型。

若要指派角色,請使用 az role assignment create 、指定 的值 --assignee-object-id ,然後將 設定 --assignee-principal-typeServicePrincipal

az role assignment create --assignee-object-id "{assigneeObjectId}" \
--assignee-principal-type "{assigneePrincipalType}" \
--role "{roleNameOrId}" \
--scope "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}"

下列範例會將虛擬機器參與者 角色指派給 製藥銷售 資源群組範圍的 msi-test 受控識別

az role assignment create --assignee-object-id "33333333-3333-3333-3333-333333333333" \
--assignee-principal-type "ServicePrincipal" \
--role "Virtual Machine Contributor" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/pharma-sales"

為訂用帳戶範圍的使用者指派角色

讀者 角色指派給 annm@example.com 訂用帳戶範圍的使用者。

az role assignment create --assignee "annm@example.com" \
--role "Reader" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000"

在訂用帳戶範圍指派群組的角色

讀者 角色指派給 訂用帳戶範圍識別碼為 222222222-2222-2222-2222-22222222222222 的 Ann Mack 小組 群組。

az role assignment create --assignee "22222222-2222-2222-2222-222222222222" \
--role "Reader" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000"

為訂用帳戶範圍中的所有 Blob 容器指派角色

儲存體 Blob 資料讀取者 角色指派給 alain@example.com 訂用帳戶範圍的使用者。

az role assignment create --assignee "alain@example.com" \
--role "Storage Blob Data Reader" \
--scope "/subscriptions/00000000-0000-0000-0000-000000000000"

為管理群組範圍的使用者指派角色

計費讀者 角色指派給 alain@example.com 管理群組範圍的使用者。

az role assignment create --assignee "alain@example.com" \
--role "Billing Reader" \
--scope "/providers/Microsoft.Management/managementGroups/marketing-group"

下一步