【Azure 环境】使用Azure bicep对多个ServicePrinciple 进行role assignment分配

问题描述

使用Azure bicep对多个ServicePrinciple 进行role assignment分配

 

步骤如下

第一步:定义传参,里面包括object ID和role的一个map如:

param servicePrincipals array = [
  {
    objectId: 'service-principal-object-id-1'
    roles: [
      'Contributor'
      'Reader'
    ]
  }
  {
    objectId: 'service-principal-object-id-2'
    roles: [
      'Contributor'
    ]
  }
]

 

第二步:把以上map转化为数组

Azure bicep现在不支持多层循环嵌套,因此只能使用一个数组

var assignments = [

  for sp in servicePrincipals: map(sp.roles, role => {

    objectId: sp.objectId

    role: role

  })

]

var assignmentArray = flatten(assignments)

 

第三步:使用循环进行roleAssignment的创建

resource roleAssignments 'Microsoft.Authorization/roleAssignments@2020-10-01-preview' = [
  for assignment in assignmentArray: {
    name: guid(storageAccount.id, assignment.objectId, assignment.role)
    scope: storageAccount
    properties: {
      roleDefinitionId: subscriptionResourceId(
        'Microsoft.Authorization/roleDefinitions',
        roleDefinitionMap[assignment.role]
      )
      principalId: assignment.objectId
      principalType: 'ServicePrincipal'
    }
  }
]

 

代码片段截图:

 

参考资料

  1. https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/existing-resource 
  2. https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/outputs?tabs=azure-powershell#get-output-values 

 

[END]

 

posted @   路边两盏灯  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2021-11-26 【Azure 应用服务】Azure Function App Linux环境下的Python Function,安装 psycopg2 模块错误
2020-11-26 【Azure微服务 Service Fabric 】Service Fabric中应用开启外部访问端口及微服务之间通过反向代理端口访问问题
点击右上角即可分享
微信分享提示