Remove the scope property from your resource definition...
TLDR;
roleAssignments can only be deployed at the scope they are being assigned to, so the property is extraneous. Also the scope property doesn't work with managementGroup extension resources (confusing I know) which is just a point in time gap. The scope property is generally used for targeting a resource to a different scope (i.e. different from the template deployment itself) but since roleAssignments can't be retargeted you don't need it and it's going to cause a problem for you in this case.
Here's my sample (note I don't have the principalType property so it uses the default):
{
"$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"principalId": {
"type": "string",
"metadata": {
"description": "principalId if the user that will be given contributor access to the resourceGroup"
}
},
"roleDefinitionId": {
"type": "string",
"defaultValue": "b24988ac-6180-42a0-ab88-20f7382dd24c",
"metadata": {
"description": "roleDefinition for the assignment - default is contributor"
}
},
"managementGroupName": {
"type": "string",
"metadata": {
"description": "Name of the managementGroup for the roleAssignment"
}
}
},
"variables": {
// this creates an idempotent GUID for the role assignment
"roleAssignmentName": "[guid(parameters('managementGroupName'), parameters('principalId'), parameters('roleDefinitionId'))]"
},
"resources": [
{
"type": "Microsoft.Authorization/roleAssignments",
"apiVersion": "2020-04-01-preview",
"name": "[variables('roleAssignmentName')]",
"properties": {
"roleDefinitionId": "[tenantResourceId('Microsoft.Authorization/roleDefinitions', parameters('roleDefinitionId'))]",
"principalId": "[parameters('principalId')]"
}
}
]
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…