我创建了一个二头肌模板。在其中,我创建了一个用户分配的身份,并在其他资源中引用它,如下所示
var identityName = 'mid-dd-test'
var roleName = 'TestRole'
var roleDescription = 'Some test'
var roleScopes = [
resourceGroup().id
]
var resolvedActions = [
'Microsoft.Resources/subscriptions/resourcegroups/*'
'Microsoft.Compute/sshPublicKeys/*'
]
var permittedDataActions = []
resource userId 'Microsoft.ManagedIdentity/userAssignedIdentities@2018-11-30' = {
name: identityName
location: resourceGroup().location
}
resource roleDef 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' = {
name: guid(subscription().id, 'bicep', 'dsadsd')
properties: {
roleName: roleName
description: roleDescription
type: 'customRole'
assignableScopes: roleScopes
permissions: [
{
actions: resolvedActions
dataActions: permittedDataActions
}
]
}
}
resource roles 'Microsoft.Authorization/roleAssignments@2018-09-01-preview' = {
name: guid(subscription().id, 'bicep-roleassignments', 'dsddsd')
properties: {
principalId: userId.properties.principalId
roleDefinitionId: roleDef.id
}
}
每当我部署这个时,我都需要运行 2 次。第一次运行以错误消息结束:
Principal XXX does not exist in the directory YYY
其中 XXX
是用户分配的身份所具有的主体 ID,YYY
是我的租户 ID。如果我现在查看门户,则会创建身份,并且 XXX
是正确的 ID。
因此,当我现在只需重新运行部署时它就可以工作。
我认为这是 dependsOn
中的一个错误,它应该与 ARM 模板相关,而不是 Bicep。我找不到任何可以向 Microsoft 报告 ARM 模板问题的地方。
我请求确保我不会错过这里的其他内容。
编辑:添加了显示错误的完整工作示例。要使用它,请将脚本内容复制到本地的 test.bicep
中。然后创建一个资源组(我们称之为“rg-test”),确保您的本地 POSH 上下文设置正确,并在存储二头肌的文件夹中执行以下行:
New-AzResourceGroupDeployment -Name deploy -Mode Incremental -TemplateFile .\test.bicep -ResourceGroupName rg-test
最佳答案
在角色分配中,您需要将 principalType
指定为 ServicePrincipal
,并使用大于或等于的 api 版本:2018-09-01-预览
。
创建服务主体时,它是在 Azure AD 中创建的。服务主体在全局范围内复制需要一些时间。通过将 principalType
设置为 ServicePrincipal
,它告诉 ARM API 等待复制。
resource roles 'Microsoft.Authorization/roleAssignments@2018-09-01-preview' = {
name: guid(subscription().id, 'bicep-roleassignments', 'dsddsd')
properties: {
principalId: userId.properties.principalId
roleDefinitionId: roleDef.id
principalType: 'ServicePrincipal'
}
}
关于Azure Bicep - 角色分配 - 目录中不存在主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70581387/