Idempotency of az role assignment create is not working
Describe the bug
The az role assignment create command used to implement the idempotency, it means when the role assignment already exists, it will return the existing role assignment without error. It was implemented here. However, recently, I am seeing the idempotency assumption is broken.
Related command
az role assignment create
Errors
az role assignment create --assignee 292fc6f0-c845-4f60-9b2a-84253e963e2c --role "Key Vault Contributor" --scope /subscriptions/【mysub】/resourceGroups/[myrg]/providers/Microsoft.KeyVault/vaults/[mykv]--only-show-errors
(RoleAssignmentExists) A role assignment with ID '0b24f0d38ec546f5a10b26557bb60958' already exists with the same scope, principal ID, and role definition ID. Code: RoleAssignmentExists Message: A role assignment with ID '0b24f0d38ec546f5a10b26557bb60958' already exists with the same scope, principal ID, and role definition ID.
Issue script & Debug output
Expected behavior
Succeeds and return the role assignment list result
Environment Summary
{ "azure-cli": "2.76.0", "azure-cli-core": "2.76.0", "azure-cli-telemetry": "1.1.0", "extensions": { "account": "0.2.5", "aksarc-lm": "0.9.1", "arcappliance": "1.5.0", "arcdata": "1.5.25", "azure-devops": "1.0.2", "connectedk8s": "1.10.8", "customlocation": "0.1.4", "k8s-configuration": "2.2.0", "k8s-extension": "1.6.7", "k8s-runtime": "2.0.0", "providerhub": "1.0.0b2" } }
Additional context
No response
Thank you for opening this issue, we will look into it.
Here are some similar issues that might help you. Please check if they can solve your problem.
- #8568
- #31579
- #9868
- #25872
Possible solution (Extracted from existing issue, might be incorrect; please verify carefully)
Solution 1:
I ended up flipping around the strategy, and asking if the role assignment exists first instead of reacting to a 409. Figured it was a much cleaner solution, and it matches how Terraform works around this problem.
Reference:
- https://github.com/azure/azure-cli/issues/8568#issuecomment-466097358
Solution 2:
Currently, the workaround is to check before assigning:
existingAssignment=$(az synapse role assignment list --workspace-name ${synapseName} --role "6e4bf58a-b8e1-4cc3-bbf9-d73143322b78" --assignee ${adGroup} --only-show-errors);
if [ -z "$existingAssignment" ];
then az synapse role assignment create --workspace-name ${synapseName} --role "6e4bf58a-b8e1-4cc3-bbf9-d73143322b78" --assignee ${adGroup} --only-show-errors;
fi
Reference:
- https://github.com/azure/azure-cli/issues/25872#issuecomment-1473792504
Solution 3:
I would suggest we proceed by fetching the assignment by leveraging the existing list_role_assignments. Idempotent is the main command authoring guideline we would like to persist which community has appreciated very much.
Reference:
- https://github.com/azure/azure-cli/issues/8568#issuecomment-465868129
Powered by issue-sentinel
Here are some similar issues that might help you. Please check if they can solve your problem.
- #8568
- #25872
- #31579
- #11081
Possible solution (Extracted from existing issue, might be incorrect; please verify carefully)
Solution 1:
I suggest we catch the error and ignore it if the error code is RoleAssignmentExists. Letting the service team fix it would likely take years based on the experience we have accumulated on other service issues. Technically this is a breaking change, but I am fine we go ahead to swallow it.
Reference:
- https://github.com/azure/azure-cli/issues/8568#issuecomment-465408538
Solution 2:
I ended up flipping around the strategy, and asking if the role assignment exists first instead of reacting to a 409. Figured it was a much cleaner solution, and it matches how Terraform works around this problem.
Reference:
- https://github.com/azure/azure-cli/issues/8568#issuecomment-466097358
Solution 3:
Currently, the workaround is to check before assigning:
existingAssignment=$(az synapse role assignment list --workspace-name ${synapseName} --role "6e4bf58a-b8e1-4cc3-bbf9-d73143322b78" --assignee ${adGroup} --only-show-errors);
if [ -z "$existingAssignment" ];
then az synapse role assignment create --workspace-name ${synapseName} --role "6e4bf58a-b8e1-4cc3-bbf9-d73143322b78" --assignee ${adGroup} --only-show-errors;
fi
Reference:
- https://github.com/azure/azure-cli/issues/25872#issuecomment-1473792504
Powered by issue-sentinel
Hi @yonzhan any update on this bug?
This broke all of our deployments, this is a regression. We are migrating to Bicep for this currently so we can do this deployment with idempotence.
This has also caused us problems.
Is the command's behavior being intentionally changed?
According to the documentation, this command should be indepotent: https://github.com/Azure/azure-cli/blob/dev/doc/command_guidelines.md#standard-command-types
This issue emerged for our team today and is now breaking multiple ADO deployment pipelines. It is somewhat concerning that it has been raised under different issue numbers in the past (#25872) and is unresolved (apart from the workaround).
Would it be possible to prioritize a fix for this?
Seems that the service changed the error that was returned for "Already existing role assignment". It broke our make-shift error handling in some pipelines that was deploying using SDKs.
it seems that it broke the idempotency implementation in the CLI
And it broke deployments that were relying on this idempotency in the CLI
I wiould expect this to be a very wide-spread breaking change for all Azure user. it should be a very high priority fix.
The underlying Role Assignments - Create REST API is not idempotent.
Idempotence is implemented by Azure CLI on the client side:
https://github.com/Azure/azure-cli/blob/a4c9d4d5721f7156c58675dbaac71d9df41e16d1/src/azure-cli/azure/cli/command_modules/role/custom.py#L1334-L1335
The code was introduced by https://github.com/Azure/azure-cli/pull/9108 in 2019 and hasn't been changed since then.
Even now when you create a role assignment on the subscription level /subscriptions/xxx, it returns 409 with The role assignment already exists. error message:
> az role assignment create --assignee a7003d8c-e50f-4371-91a6-ef37bba4ab23 --role reader --scope /subscriptions/xxx --debug
...
DEBUG: cli.azure.cli.core.sdk.policies: Request URL: 'https://management.azure.com/subscriptions/xxx/providers/Microsoft.Authorization/roleAssignments/42bb4263-916a-4922-8e15-e058798500b5?api-version=2022-04-01'
DEBUG: cli.azure.cli.core.sdk.policies: Request method: 'PUT'
...
DEBUG: cli.azure.cli.core.sdk.policies: Request body:
DEBUG: cli.azure.cli.core.sdk.policies: {"properties": {"roleDefinitionId": "/subscriptions/xxx/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7", "principalId": "7fe989f6-5c63-416a-94ca-46e5fe1297c5", "principalType": "ServicePrincipal"}}
...
DEBUG: cli.azure.cli.core.sdk.policies: Response status: 409
...
DEBUG: cli.azure.cli.core.sdk.policies: Response content:
DEBUG: cli.azure.cli.core.sdk.policies: {"error":{"code":"RoleAssignmentExists","message":"The role assignment already exists."}}
However, when creating an existing role assignment on a key vault /subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.KeyVault/vaults/xxx, it fails with:
> az role assignment create --assignee a7003d8c-e50f-4371-91a6-ef37bba4ab23 --role "Key Vault Contributor" --scope /subscriptions/xxx/resourceGroups/test/providers/Microsoft.KeyVault/vaults/testkv --debug
...
cli.azure.cli.core.sdk.policies: Request URL: 'https://management.azure.com/subscriptions/xxx/resourceGroups/test/providers/Microsoft.KeyVault/vaults/testkv/providers/Microsoft.Authorization/roleAssignments/4588e8f6-d046-48a2-a55e-c2f1323bbebf?api-version=2022-04-01'
cli.azure.cli.core.sdk.policies: Request method: 'PUT'
...
cli.azure.cli.core.sdk.policies: Request body:
cli.azure.cli.core.sdk.policies: {"properties": {"roleDefinitionId": "/subscriptions/xxx/providers/Microsoft.Authorization/roleDefinitions/f25e0fa2-a7c8-4377-a976-54943a77a395", "principalId": "7fe989f6-5c63-416a-94ca-46e5fe1297c5", "principalType": "ServicePrincipal"}}
...
cli.azure.cli.core.sdk.policies: Response status: 409
...
cli.azure.cli.core.sdk.policies: Response content:
cli.azure.cli.core.sdk.policies: {"error":{"code":"RoleAssignmentExists","message":"A role assignment with ID 'bd86dde3d7de4ed99f4e298145cd3552' already exists with the same scope, principal ID, and role definition ID."}}
Notice the error message becomes A role assignment with ID 'bd86dde3d7de4ed99f4e298145cd3552' already exists with the same scope, principal ID, and role definition ID., thus cannot be captured by the current error handling logic.
A safer way would be to check the error code RoleAssignmentExists, instead of error message. However, https://github.com/Azure/azure-cli/issues/25872 shows that even the error code can be different - RoleAssignmentAlreadyExists. I haven't looked into az synapse, but it seems to be a data-plane role assignment.
I have observed different responses from Role Assignments - Create REST API from different ARM servers.
When the request goes to SOUTHINDIA, the response is as before - The role assignment already exists.:
> az role assignment create --assignee a7003d8c-e50f-4371-91a6-ef37bba4ab23 --role reader --scope /subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590 --debug
...
DEBUG: cli.azure.cli.core.sdk.policies: Request URL: 'https://management.azure.com/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/providers/Microsoft.Authorization/roleAssignments/42bb4263-916a-4922-8e15-e058798500b5?api-version=2022-04-01'
DEBUG: cli.azure.cli.core.sdk.policies: Request method: 'PUT'
DEBUG: cli.azure.cli.core.sdk.policies: Request headers:
DEBUG: cli.azure.cli.core.sdk.policies: 'Content-Type': 'application/json'
DEBUG: cli.azure.cli.core.sdk.policies: 'Content-Length': '270'
DEBUG: cli.azure.cli.core.sdk.policies: 'Accept': 'application/json'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-client-request-id': '49f235fc-849d-11f0-8d9d-1091d1f98a2a'
DEBUG: cli.azure.cli.core.sdk.policies: 'CommandName': 'role assignment create'
DEBUG: cli.azure.cli.core.sdk.policies: 'ParameterSetName': '--assignee --role --scope --debug'
DEBUG: cli.azure.cli.core.sdk.policies: 'User-Agent': 'AZURECLI/2.77.0 (PIP) azsdk-python-core/1.35.0 Python/3.13.7 (Windows-11-10.0.26100-SP0)'
DEBUG: cli.azure.cli.core.sdk.policies: 'Authorization': '*****'
DEBUG: cli.azure.cli.core.sdk.policies: Request body:
DEBUG: cli.azure.cli.core.sdk.policies: {"properties": {"roleDefinitionId": "/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7", "principalId": "7fe989f6-5c63-416a-94ca-46e5fe1297c5", "principalType": "ServicePrincipal"}}
...
DEBUG: cli.azure.cli.core.sdk.policies: Response status: 409
DEBUG: cli.azure.cli.core.sdk.policies: Response headers:
DEBUG: cli.azure.cli.core.sdk.policies: 'Cache-Control': 'no-cache'
DEBUG: cli.azure.cli.core.sdk.policies: 'Pragma': 'no-cache'
DEBUG: cli.azure.cli.core.sdk.policies: 'Content-Length': '89'
DEBUG: cli.azure.cli.core.sdk.policies: 'Content-Type': 'application/json; charset=utf-8'
DEBUG: cli.azure.cli.core.sdk.policies: 'Expires': '-1'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-request-id': 'ddc85213-ad87-47c0-a178-208d48a2ac97'
DEBUG: cli.azure.cli.core.sdk.policies: 'X-Content-Type-Options': 'nosniff'
DEBUG: cli.azure.cli.core.sdk.policies: 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-operation-identifier': 'tenantId=54826b22-38d6-4fb2-bad9-b7b93a3e9c5a,objectId=0d504196-1423-4569-9a6e-15149656f0ee/southindia/13e31107-d83b-4913-b93f-e03338959268'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-ratelimit-remaining-subscription-writes': '199'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-ratelimit-remaining-subscription-global-writes': '2999'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-correlation-request-id': 'd5d37658-4efa-426c-8574-0c8b1ea2b8b4'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-routing-request-id': 'SOUTHINDIA:20250829T055919Z:d5d37658-4efa-426c-8574-0c8b1ea2b8b4'
DEBUG: cli.azure.cli.core.sdk.policies: 'X-Cache': 'CONFIG_NOCACHE'
DEBUG: cli.azure.cli.core.sdk.policies: 'X-MSEdge-Ref': 'Ref A: 2EC04269E1AE4FADBCA48B51722E0188 Ref B: MAA201060516029 Ref C: 2025-08-29T05:59:17Z'
DEBUG: cli.azure.cli.core.sdk.policies: 'Date': 'Fri, 29 Aug 2025 05:59:19 GMT'
DEBUG: cli.azure.cli.core.sdk.policies: Response content:
DEBUG: cli.azure.cli.core.sdk.policies: {"error":{"code":"RoleAssignmentExists","message":"The role assignment already exists."}}
But when the request goes to SOUTHEASTASIA, the response is A role assignment with ID 'd511b881d13648fe9656191d8562b126' already exists with the same scope, principal ID, and role definition ID.:
> az role assignment create --assignee a7003d8c-e50f-4371-91a6-ef37bba4ab23 --role reader --scope /subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590 --debug
...
DEBUG: cli.azure.cli.core.sdk.policies: Request URL: 'https://management.azure.com/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/providers/Microsoft.Authorization/roleAssignments/bca546d8-e3af-4acb-a886-a9ce62f89817?api-version=2022-04-01'
DEBUG: cli.azure.cli.core.sdk.policies: Request method: 'PUT'
DEBUG: cli.azure.cli.core.sdk.policies: Request headers:
DEBUG: cli.azure.cli.core.sdk.policies: 'Content-Type': 'application/json'
DEBUG: cli.azure.cli.core.sdk.policies: 'Content-Length': '270'
DEBUG: cli.azure.cli.core.sdk.policies: 'Accept': 'application/json'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-client-request-id': '94518439-84a2-11f0-b5e4-1091d1f98a2a'
DEBUG: cli.azure.cli.core.sdk.policies: 'CommandName': 'role assignment create'
DEBUG: cli.azure.cli.core.sdk.policies: 'ParameterSetName': '--assignee --role --scope --debug'
DEBUG: cli.azure.cli.core.sdk.policies: 'User-Agent': 'AZURECLI/2.77.0 (PIP) azsdk-python-core/1.35.0 Python/3.13.7 (Windows-11-10.0.26100-SP0)'
DEBUG: cli.azure.cli.core.sdk.policies: 'Authorization': '*****'
DEBUG: cli.azure.cli.core.sdk.policies: Request body:
DEBUG: cli.azure.cli.core.sdk.policies: {"properties": {"roleDefinitionId": "/subscriptions/0b1f6471-1bf0-4dda-aec3-cb9272f09590/providers/Microsoft.Authorization/roleDefinitions/acdd72a7-3385-48ef-bd42-f606fba81ae7", "principalId": "7fe989f6-5c63-416a-94ca-46e5fe1297c5", "principalType": "ServicePrincipal"}}
...
DEBUG: cli.azure.cli.core.sdk.policies: Response status: 409
DEBUG: cli.azure.cli.core.sdk.policies: Response headers:
DEBUG: cli.azure.cli.core.sdk.policies: 'Cache-Control': 'no-cache'
DEBUG: cli.azure.cli.core.sdk.policies: 'Pragma': 'no-cache'
DEBUG: cli.azure.cli.core.sdk.policies: 'Content-Length': '188'
DEBUG: cli.azure.cli.core.sdk.policies: 'Content-Type': 'application/json; charset=utf-8'
DEBUG: cli.azure.cli.core.sdk.policies: 'Expires': '-1'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-request-id': '3e2ad954-9be2-4594-b5ae-1b45d80b01fe'
DEBUG: cli.azure.cli.core.sdk.policies: 'X-Content-Type-Options': 'nosniff'
DEBUG: cli.azure.cli.core.sdk.policies: 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-operation-identifier': 'tenantId=54826b22-38d6-4fb2-bad9-b7b93a3e9c5a,objectId=0d504196-1423-4569-9a6e-15149656f0ee/southeastasia/1d6a3687-865f-4503-b13e-e469ee50f7fc'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-ratelimit-remaining-subscription-writes': '199'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-ratelimit-remaining-subscription-global-writes': '2999'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-correlation-request-id': '8f7f8d34-3465-455c-9132-992d3788ab20'
DEBUG: cli.azure.cli.core.sdk.policies: 'x-ms-routing-request-id': 'SOUTHEASTASIA:20250829T063713Z:8f7f8d34-3465-455c-9132-992d3788ab20'
DEBUG: cli.azure.cli.core.sdk.policies: 'X-Cache': 'CONFIG_NOCACHE'
DEBUG: cli.azure.cli.core.sdk.policies: 'X-MSEdge-Ref': 'Ref A: 4B46E9A5C98E489B9A46F30B4F8CA5E3 Ref B: MAA201060516011 Ref C: 2025-08-29T06:37:11Z'
DEBUG: cli.azure.cli.core.sdk.policies: 'Date': 'Fri, 29 Aug 2025 06:37:13 GMT'
DEBUG: cli.azure.cli.core.sdk.policies: Response content:
DEBUG: cli.azure.cli.core.sdk.policies: {"error":{"code":"RoleAssignmentExists","message":"A role assignment with ID 'd511b881d13648fe9656191d8562b126' already exists with the same scope, principal ID, and role definition ID."}}
We have reached out to ARM RBAC team (IcM 677297176) and they are now aware of this issue. This change will be rolled back in the next several hours.
Thanks for the update, issue appears to be resolved for us now. Will your fix in the PR be released as part of an upcoming version of the CLI?
Just if somebody is interested, a workaround that is pretty easy to apply:
- add a function to your script:
function az-ignore-role-exists {
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$Args
)
# Run Azure CLI with the provided arguments, capture stdout+stderr
$output = & az @Args 2>&1
if ($LASTEXITCODE -ne 0) {
# Treat "already exists..." (duplicate role assignment) as success
if ($output -match "already exists with the same") {
# Azure Pipelines log output syntax
Write-Host "##[debug]Role assignment already exists — ignoring."
$global:LASTEXITCODE = 0
}
else {
Write-Error "Azure CLI command failed: $output"
exit 1
}
}
return $output
}
- then replace the call to az with a call to az-ignore-role-exists
# ... exemple from inside a AzureCLI pscore task in Azure Pipelines
az-ignore-role-exists role assignment create --assignee $app[0].id --role "Azure Service Bus Data Owner" --scope "/subscriptions/$subscriptionId/resourcegroups/$resourceGroupName/providers/Microsoft.ServiceBus/namespaces/$namespaceName/queues/$queueName"
# ....
Thanks for the update, issue appears to be resolved for us now. Will your fix in the PR be released as part of an upcoming version of the CLI?
We aim to release https://github.com/Azure/azure-cli/pull/32027 in 2.78.0: https://github.com/Azure/azure-cli/milestone/160
Note to CLI users affected:
While the error handling is nice from a UX perspective, for real scripted deployment, the proper usage is to meaningfully name your roleassignment and not let the CLI generate a guid as a name. The service handles idempotency just fine, but a role assignment is a resource, the same as a resource group is a resource.
if you do PUT with a different name, then the API fails warning you that you already have this same RA. if you do a PUT with the same name, it will succeed as a no-op.
@serbrech is correct. az role assignment create does support --name argument (added by #24324):
> az role assignment create -h
...
--name -n : A GUID for the role assignment. It must be unique and different
for each role assignment. If omitted, a new GUID is generated.
...
Create role assignment with your own assignment name.
az role assignment create --assignee-object-id 00000000-0000-0000-0000-000000000000
--assignee-principal-type ServicePrincipal --role Reader --scope
/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/MyResourceGroup --name
00000000-0000-0000-0000-000000000000
Specifying the --name argument will leverage the service-side idempotence.
the proper usage is to meaningfully name your role assignment
I don't think you can use a meaningful name. According to https://learn.microsoft.com/en-us/rest/api/authorization/role-assignments/create?view=rest-authorization-2022-04-01&tabs=HTTP, roleAssignmentName must be a GUID:
roleAssignmentName: The name of the role assignment. It can be any valid GUID.