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

简介: 使用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]




 

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

相关文章
|
6月前
|
C#
【Azure Developer】使用 Azure VM 上的用户分配托管标识访问 Azure Key Vault 中国区代码示例
【Azure Developer】使用 Azure VM 上的用户分配托管标识访问 Azure Key Vault 中国区代码示例
|
4月前
|
内存技术
【Azure Cloud Service】创建Azure云服务时遇见分配VM资源错误: VM(s) with the following constraints cannot be allocated, because the condition is too restrictive
Allocation failed. VM(s) with the following constraints cannot be allocated, because the condition is too restrictive. Please remove some constraints and try again. Constraints applied are:\n - VM Size
|
JavaScript
【IBM Tivoli Identity Manager 学习文档】17 账户分配
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/   1.前续工作: 添加人; 创建组织角色,并将此人加入其中; 创建Service; 创建身份策略; 创建分配策略; 配置provisioning policy entitlements; 2.创建分配策略: 两个部分需要设置-- Membership:谁有允许访问的权限。
1033 0

热门文章

最新文章