You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/role-based-access-control/role-assignments-cli.md
+34-6Lines changed: 34 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ ms.devlang: na
12
12
ms.topic: conceptual
13
13
ms.tgt_pltfrm: na
14
14
ms.workload: identity
15
-
ms.date: 09/11/2019
15
+
ms.date: 11/21/2019
16
16
ms.author: rolyon
17
17
ms.reviewer: bagovind
18
18
---
@@ -261,6 +261,34 @@ az role assignment list --scope /providers/Microsoft.Management/managementGroups
261
261
az role assignment list --scope /providers/Microsoft.Management/managementGroups/marketing-group --output json | jq '.[] | {"principalName":.principalName, "roleDefinitionName":.roleDefinitionName, "scope":.scope}'
262
262
```
263
263
264
+
## Get object IDs
265
+
266
+
To list, add, or remove role assignments, you might need to specify the unique ID of an object. The ID has the format: `11111111-1111-1111-1111-111111111111`. You can get the ID using the Azure portal or Azure CLI.
267
+
268
+
### User
269
+
270
+
To get the object ID for an Azure AD user, you can use [az ad user show](/cli/azure/ad/user#az-ad-user-show).
271
+
272
+
```azurecli
273
+
az ad user show --id "{email}" --query objectId --output tsv
274
+
```
275
+
276
+
### Group
277
+
278
+
To get the object ID for an Azure AD group, you can use [az ad group show](/cli/azure/ad/group#az-ad-group-show) or [az ad group list](/cli/azure/ad/group#az-ad-group-list).
279
+
280
+
```azurecli
281
+
az ad group show --group "{name}" --query objectId --output tsv
282
+
```
283
+
284
+
### Application
285
+
286
+
To get the object ID for an Azure AD service principal (identity used by an application), you can use [az ad sp list](/cli/azure/ad/sp#az-ad-sp-list). For a service principal, use the object ID and **not** the application ID.
287
+
288
+
```azurecli
289
+
az ad sp list --display-name "{name}" --query [].objectId --output tsv
290
+
```
291
+
264
292
## Grant access
265
293
266
294
In RBAC, to grant access, you create a role assignment.
@@ -306,7 +334,7 @@ az role assignment create --role 9980e02c-c2be-4d73-94e8-173b1dc7cf3c --assignee
306
334
307
335
### Create a role assignment for a group
308
336
309
-
To grant access to a group, use [az role assignment create](/cli/azure/role/assignment#az-role-assignment-create). To get the ID of the group, you can use [az ad group list](/cli/azure/ad/group#az-ad-group-list) or [az ad group show](/cli/azure/ad/group#az-ad-group-show).
337
+
To grant access to a group, use [az role assignment create](/cli/azure/role/assignment#az-role-assignment-create). For information about how to get the object ID of the group, see [Get object IDs](#get-object-ids).
310
338
311
339
```azurecli
312
340
az role assignment create --role <role_name_or_id> --assignee-object-id <assignee_object_id> --resource-group <resource_group> --scope </subscriptions/subscription_id>
@@ -326,7 +354,7 @@ az role assignment create --role "Virtual Machine Contributor" --assignee-object
326
354
327
355
### Create a role assignment for an application at a resource group scope
328
356
329
-
To grant access to an application, use [az role assignment create](/cli/azure/role/assignment#az-role-assignment-create). To get the object ID of the application, you can use [az ad app list](/cli/azure/ad/app#az-ad-app-list) or [az ad app show](/cli/azure/ad/app#az-ad-app-show).
357
+
To grant access to an application, use [az role assignment create](/cli/azure/role/assignment#az-role-assignment-create). For information about how to get the object ID of the application, see [Get object IDs](#get-object-ids).
330
358
331
359
```azurecli
332
360
az role assignment create --role <role_name_or_id> --assignee-object-id <assignee_object_id> --resource-group <resource_group>
@@ -346,7 +374,7 @@ To grant access to a user at a subscription scope, use [az role assignment creat
346
374
az role assignment create --role <role_name_or_id> --assignee <assignee> --subscription <subscription_name_or_id>
347
375
```
348
376
349
-
The following example assigns the *Reader* role to to the *annm\@example.com* user at a subscription scope.
377
+
The following example assigns the *Reader* role to the *annm\@example.com* user at a subscription scope.
350
378
351
379
```azurecli
352
380
az role assignment create --role "Reader" --assignee [email protected] --subscription 00000000-0000-0000-0000-000000000000
@@ -360,7 +388,7 @@ To grant access to a user at a management group scope, use [az role assignment c
360
388
az role assignment create --role <role_name_or_id> --assignee <assignee> --scope /providers/Microsoft.Management/managementGroups/<group_id>
361
389
```
362
390
363
-
The following example assigns the *Billing Reader* role to to the *alain\@example.com* user at a management group scope.
391
+
The following example assigns the *Billing Reader* role to the *alain\@example.com* user at a management group scope.
364
392
365
393
```azurecli
366
394
az role assignment create --role "Billing Reader" --assignee [email protected] --scope /providers/Microsoft.Management/managementGroups/marketing-group
@@ -396,7 +424,7 @@ The following example removes the *Virtual Machine Contributor* role assignment
396
424
az role assignment delete --assignee [email protected] --role "Virtual Machine Contributor" --resource-group pharma-sales
397
425
```
398
426
399
-
The following example removes the *Reader* role from the *Ann Mack Team* group with ID 22222222-2222-2222-2222-222222222222 at a subscription scope. To get the ID of the group, you can use [az ad group list](/cli/azure/ad/group#az-ad-group-list) or [az ad group show](/cli/azure/ad/group#az-ad-group-show).
427
+
The following example removes the *Reader* role from the *Ann Mack Team* group with ID 22222222-2222-2222-2222-222222222222 at a subscription scope. For information about how to get the object ID of the group, see [Get object IDs](#get-object-ids).
400
428
401
429
```azurecli
402
430
az role assignment delete --assignee 22222222-2222-2222-2222-222222222222 --role "Reader" --subscription 00000000-0000-0000-0000-000000000000
In RBAC, to grant access, you create a role assignment.
271
+
## Get object IDs
274
272
275
-
### Search for object IDs
273
+
To list, add, or remove role assignments, you might need to specify the unique ID of an object. The ID has the format: `11111111-1111-1111-1111-111111111111`. You can get the ID using the Azure portal or Azure PowerShell.
276
274
277
-
To assign a role, you need to identify both the object (user, group, or application) and the scope.
275
+
### User
278
276
279
-
To get the subscription ID, you can find it on the **Subscriptions** blade in the Azure portal or you can use [Get-AzSubscription](/powershell/module/Az.Accounts/Get-AzSubscription).
280
-
281
-
To get the object ID for an Azure AD user, use [Get-AzADUser](/powershell/module/az.resources/get-azaduser).
277
+
To get the object ID for an Azure AD user, you can use [Get-AzADUser](/powershell/module/az.resources/get-azaduser).
282
278
283
279
```azurepowershell
284
280
Get-AzADUser -StartsWith <string_in_quotes>
281
+
(Get-AzADUser -DisplayName <name_in_quotes>).id
285
282
```
286
283
287
-
To get the object ID for an Azure AD group, use [Get-AzADGroup](/powershell/module/az.resources/get-azadgroup).
284
+
### Group
285
+
286
+
To get the object ID for an Azure AD group, you can use [Get-AzADGroup](/powershell/module/az.resources/get-azadgroup).
To get the object ID for an Azure AD service principal or application, use [Get-AzADServicePrincipal](/powershell/module/az.resources/get-azadserviceprincipal).
293
+
### Application
294
+
295
+
To get the object ID for an Azure AD service principal (identity used by an application), you can use [Get-AzADServicePrincipal](/powershell/module/az.resources/get-azadserviceprincipal). For a service principal, use the object ID and **not** the application ID.
The following example assigns the [Virtual Machine Contributor](built-in-roles.md#virtual-machine-contributor) role to *[email protected]* user at the *pharma-sales* resource group scope. To get the unique role ID, you can use [Get-AzRoleDefinition](/powershell/module/az.resources/get-azroledefinition) or see [Built-in roles for Azure resources](built-in-roles.md).
349
+
The following example assigns the [Virtual Machine Contributor](built-in-roles.md#virtual-machine-contributor) role to *alain\@example.com* user at the *pharma-sales* resource group scope. To get the unique role ID, you can use [Get-AzRoleDefinition](/powershell/module/az.resources/get-azroledefinition) or see [Built-in roles for Azure resources](built-in-roles.md).
### Create a role assignment for a group at a resource scope
359
366
360
-
To grant access to a group at a resource scope, use [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment).
367
+
To grant access to a group at a resource scope, use [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment). For information about how to get the object ID of the group, see [Get object IDs](#get-object-ids).
### Create a role assignment for an application at a subscription scope
390
397
391
-
To grant access to an application at a subscription scope, use [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment).
398
+
To grant access to an application at a subscription scope, use [New-AzRoleAssignment](/powershell/module/az.resources/new-azroleassignment). For information about how to get the object ID of the application, see [Get object IDs](#get-object-ids).
Copy file name to clipboardExpand all lines: articles/role-based-access-control/role-assignments-template.md
+53-25Lines changed: 53 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,14 +11,54 @@ ms.devlang: na
11
11
ms.topic: conceptual
12
12
ms.tgt_pltfrm: na
13
13
ms.workload: identity
14
-
ms.date: 09/20/2019
14
+
ms.date: 11/21/2019
15
15
ms.author: rolyon
16
16
ms.reviewer: bagovind
17
17
---
18
18
# Manage access to Azure resources using RBAC and Azure Resource Manager templates
19
19
20
20
[Role-based access control (RBAC)](overview.md) is the way that you manage access to Azure resources. In addition to using Azure PowerShell or the Azure CLI, you can manage access to Azure resources using [Azure Resource Manager templates](../azure-resource-manager/resource-group-authoring-templates.md). Templates can be helpful if you need to deploy resources consistently and repeatedly. This article describes how you can manage access using RBAC and templates.
21
21
22
+
## Get object IDs
23
+
24
+
To assign a role, you need to specify the ID of the user, group, or application you want to assign the role to. The ID has the format: `11111111-1111-1111-1111-111111111111`. You can get the ID using the Azure portal, Azure PowerShell, or Azure CLI.
25
+
26
+
### User
27
+
28
+
To get the ID of a user, you can use the [Get-AzADUser](/powershell/module/az.resources/get-azaduser) or [az ad user show](/cli/azure/ad/user#az-ad-user-show) commands.
objectid=$(az ad user show --id "{email}" --query objectId --output tsv)
36
+
```
37
+
38
+
### Group
39
+
40
+
To get the ID of a group, you can use the [Get-AzADGroup](/powershell/module/az.resources/get-azadgroup) or [az ad group show](/cli/azure/ad/group#az-ad-group-show) commands.
objectid=$(az ad group show --group "{name}" --query objectId --output tsv)
48
+
```
49
+
50
+
### Application
51
+
52
+
To get the ID of a service principal (identity used by an application), you can use the [Get-AzADServicePrincipal](/powershell/module/az.resources/get-azadserviceprincipal) or [az ad sp list](/cli/azure/ad/sp#az-ad-sp-list) commands. For a service principal, use the object ID and **not** the application ID.
objectid=$(az ad sp list --display-name "{name}" --query [].objectId --output tsv)
60
+
```
61
+
22
62
## Create a role assignment at a resource group scope (without parameters)
23
63
24
64
In RBAC, to grant access, you create a role assignment. The following template shows a basic way to create a role assignment. Some values are specified within the template. The following template demonstrates:
@@ -28,7 +68,7 @@ In RBAC, to grant access, you create a role assignment. The following template s
28
68
To use the template, you must do the following:
29
69
30
70
- Create a new JSON file and copy the template
31
-
- Replace `<your-principal-id>` with the unique identifier of a user, group, or application to assign the role to. The identifier has the format: `11111111-1111-1111-1111-111111111111`
71
+
- Replace `<your-principal-id>` with the ID of a user, group, or application to assign the role to
32
72
33
73
```json
34
74
{
@@ -71,9 +111,8 @@ The previous template isn't very flexible. The following template uses parameter
71
111
72
112
To use the template, you must specify the following inputs:
73
113
74
-
- The unique identifier of a user, group, or application to assign the role to
75
-
- The role to assign
76
-
- A unique identifier that will be used for the role assignment, or you can use the default identifier
114
+
- The ID of a user, group, or application to assign the role to
115
+
- A unique ID that will be used for the role assignment, or you can use the default ID
77
116
78
117
```json
79
118
{
@@ -124,39 +163,29 @@ To use the template, you must specify the following inputs:
124
163
}
125
164
```
126
165
127
-
To get the unique identifier of a user to assign the role to, you can use the [Get-AzADUser](/powershell/module/az.resources/get-azaduser) or [az ad user show](/cli/azure/ad/user#az-ad-user-show) commands.
128
-
129
-
```azurepowershell
130
-
$userid = (Get-AzADUser -DisplayName "{name}").id
131
-
```
132
-
133
-
```azurecli
134
-
userid=$(az ad user show --upn-or-object-id "{email}" --query objectId --output tsv)
135
-
```
166
+
> [!NOTE]
167
+
> This template is not idempotent unless the same `roleNameGuid` value is provided as a parameter for each deployment of the template. If no `roleNameGuid` is provided, by default a new GUID is generated on each deployment and subsequent deployments will fail with a `Conflict: RoleAssignmentExists` error.
136
168
137
169
The scope of the role assignment is determined from the level of the deployment. Here are example [New-AzResourceGroupDeployment](/powershell/module/az.resources/new-azresourcegroupdeployment) and [az group deployment create](/cli/azure/group/deployment#az-group-deployment-create) commands for how to start the deployment at a resource group scope.
az group deployment create --resource-group ExampleGroup --template-file rbac-test.json --parameters principalId=$userid builtInRoleType=Reader
176
+
az group deployment create --resource-group ExampleGroup --template-file rbac-test.json --parameters principalId=$objectid builtInRoleType=Reader
145
177
```
146
178
147
179
Here are example [New-AzDeployment](/powershell/module/az.resources/new-azdeployment) and [az deployment create](/cli/azure/deployment#az-deployment-create) commands for how to start the deployment at a subscription scope and specify the location.
az deployment create --location centralus --template-file rbac-test.json --parameters principalId=$userid builtInRoleType=Reader
186
+
az deployment create --location centralus --template-file rbac-test.json --parameters principalId=$objectid builtInRoleType=Reader
155
187
```
156
188
157
-
> [!NOTE]
158
-
> This template is not idempotent unless the same `roleNameGuid` value is provided as a parameter for each deployment of the template. If no `roleNameGuid` is provided, by default a new GUID is generated on each deployment and subsequent deployments will fail with a `Conflict: RoleAssignmentExists` error.
159
-
160
189
## Create a role assignment at a resource scope
161
190
162
191
If you need to create a role assignment at the level of a resource, the format of the role assignment is different. You provide the resource provider namespace and resource type of the resource to assign the role to. You also include the name of the resource in the name of the role assignment.
@@ -176,8 +205,7 @@ The following template demonstrates:
176
205
177
206
To use the template, you must specify the following inputs:
178
207
179
-
- The unique identifier of a user, group, or application to assign the role to
180
-
- The role to assign
208
+
- The ID of a user, group, or application to assign the role to
181
209
182
210
```json
183
211
{
@@ -243,11 +271,11 @@ To use the template, you must specify the following inputs:
243
271
To deploy the previous template, you use the resource group commands. Here are example [New-AzResourceGroupDeployment](/powershell/module/az.resources/new-azresourcegroupdeployment) and [az group deployment create](/cli/azure/group/deployment#az-group-deployment-create) commands for how to start the deployment at a resource scope.
0 commit comments