如何以二头肌格式向资源组添加角色?

问题描述

param rg_la_dev_eastus_name string = 'rg-la-dev-eastus-001'
param rg_la_prod_eastus_name string = 'rg-la-prod-eastus-001'

targetScope = 'subscription'

resource rgLaDev 'Microsoft.Resources/resourceGroups@2020-06-01' = {
  name: rg_la_dev_eastus_name
  location: 'eastus'
}

resource rgLaProd 'Microsoft.Resources/resourceGroups@2020-06-01' = {
  name: rg_la_prod_eastus_name
  location: 'eastus'
}

我可以创建一个资源组,但我想从这里分配一个角色。我不想每次都图形化。

解决方法

使用此脚本通过 Bicep 分配 RBAC 角色:

resource symbolicname 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = {
  name: 'string'
  scope: 'string'
  properties: {
    roleDefinitionId: 'string'
    principalId: 'string'
    principalType: 'string'
    canDelegate: bool
    description: 'string'
    condition: 'string'
    conditionVersion: 'string'
  }
}

参考here

,

只是添加到@allen-wu 响应中 - 请记住,您不能在带有 targetScope = 'subscription' 的模块中使用此资源。您需要在以您要为其分配权限的资源组为目标的模块中使用它。

因此您需要有 3 个文件 - 一个是您创建资源组的位置,第二个是您放置 roleAssignments 资源的位置。然后,从第一个调用模块:

module rgLaDevPermissions 'devPermissions.bicep' = {
  name: '${deployment().name}-${rgLaDev.name}-permissions'
  scope: rgLaDev
}

module rgLaProdPermissions 'prodPermissions.bicep' = {
  name: '${deployment().name}-${rgLaProd.name}-permissions'
  scope: rgLaProd
}

您当然可以使用模块参数来拥有单个模块文件,如果 roleAssignments 的结构相同并且仅在分配给谁方面有所不同。

有关更多信息,请参见此处:https://github.com/Azure/bicep/issues/1388

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...