Bicep中如何获取迭代生成的模块集合的全部输出?
解决Bicep中迭代模块集合的输出引用问题
你遇到的错误是因为ModuleCollection是模块集合(数组类型),而非单个模块实例,Bicep不允许直接对数组类型的模块集合调用.outputs,必须通过数组遍历或索引操作来提取每个模块的输出。
解决方案1:提取所有输出为字符串数组
在allCon.bicep中,直接通过数组遍历生成包含所有模块someOut的数组,并作为文件输出:
param bList array = ['t1', 'ALG-t2', 'ALG-t3'] module ModuleCollection 'conModule.bicep' = [for bName in bList: if(startsWith(bName , 'ALG')) { params: { ENV: 'dev' } }] // 遍历模块集合,提取每个模块的someOut输出 output allSomeOuts array = [for module in ModuleCollection: module.outputs.someOut]
这样在main.bicep中引用allCon模块时,就能通过allCon.outputs.allSomeOuts拿到所有迭代模块的输出值(本例中会是['test1', 'test1'])。
解决方案2:输出关联模块名称的对象数组
如果需要将输出与原始的bName关联,方便识别每个输出对应的模块,可以先过滤出符合条件的名称列表,再通过索引对应模块输出:
param bList array = ['t1', 'ALG-t2', 'ALG-t3'] // 先过滤出以ALG开头的名称列表 var filteredBNames = [for name in bList: if(startsWith(name, 'ALG')) name] module ModuleCollection 'conModule.bicep' = [for bName in filteredBNames: { params: { ENV: 'dev' } }] // 输出包含模块名称和对应输出的对象数组 output allModuleOutputs array = [for (name, idx) in filteredBNames: { moduleName: name someOut: ModuleCollection[idx].outputs.someOut }]
这种方式的输出会是:
[ { "moduleName": "ALG-t2", "someOut": "test1" }, { "moduleName": "ALG-t3", "someOut": "test1" } ]
关键说明
- 迭代创建的模块本质是数组结构,必须通过
[索引]或者for遍历的方式访问单个模块的outputs属性,不能直接对整个集合调用.outputs。 - 两种方案可以根据需求选择:如果只需要输出值的集合,选方案1;如果需要关联模块标识信息,选方案2。
内容的提问来源于stack exchange,提问作者ibda
相关产品推荐
相关产品推荐

