如何根据addIdentity1参数在Function App模板中条件添加IDENTITY_RESOURCE_ID1
条件性添加用户分配身份的Function App模板修改方案
要实现当addIdentity1参数为true时才包含IDENTITY_RESOURCE_ID1的需求,你可以利用ARM模板的union()函数和条件表达式动态构建userAssignedIdentities对象,具体修改后的模板如下:
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "name": { "type": "string" }, "location": { "type": "string" }, "addIdentity1": { "type": "bool", "defaultValue": false, "metadata": { "description": "是否添加第一个用户分配身份" } }, "identityResourceId1": { "type": "string", "metadata": { "description": "第一个用户分配身份的资源ID" } }, "identityResourceId2": { "type": "string", "metadata": { "description": "第二个用户分配身份的资源ID" } } // 其他现有参数... }, "resources": [ { "apiVersion": "2018-11-01", "name": "[parameters('name')]", "type": "Microsoft.Web/sites", "kind": "functionapp", "location": "[parameters('location')]", "identity": { "type": "SystemAssigned,UserAssigned", "userAssignedIdentities": "[union( if(parameters('addIdentity1'), json(concat('{\"', parameters('identityResourceId1'), '\": {}}')), json('{}')), json(concat('{\"', parameters('identityResourceId2'), '\": {}}')) )]" }, "tags": null, // 其他现有属性... } ] }
关键修改说明
- 新增控制参数:添加
addIdentity1布尔参数,默认关闭,用于控制是否包含第一个用户分配身份。 - 动态构建身份对象:使用
union()函数合并对象,当addIdentity1为true时,将第一个身份的JSON结构和第二个身份合并;为false时,仅保留第二个身份的结构。 - 字符串转JSON对象:通过
json(concat(...))将身份资源ID字符串转换为ARM模板可识别的JSON对象格式,解决引号转义问题。
内容的提问来源于stack exchange,提问作者Punter Vicky
相关产品推荐
相关产品推荐

