You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将test.bicep中的对象导入主Bicep模板的ruleCollections属性

Bicep 导入外部对象到资源属性的实现方案

方案1:使用Bicep原生import/export语法(推荐,需Bicep版本≥v0.4.1008)

该方式属于编译期引用,不会产生额外的部署资源,性能最优。

第一步:修改test.bicep,导出规则对象

将你定义的规则声明为变量后导出:

// test.bicep 完整内容
var testRafaelRule = {
  name: 'testRafaelRule'
  priority: 1001
  ruleCollectionType: 'FirewallPolicyFilterRuleCollection'
  action: {
    type: 'Allow'
  }
  rules: [
    {
      name: 'deleteme-1'
      ipProtocols: [
        'Any'
      ]
      destinationPorts: [
        '*'
      ]
      sourceAddresses: [
        '192.168.0.0/16'
      ]
      sourceIpGroups: []
      destinationIpGroups: []
      destinationAddresses: [
        'AzureCloud.EastUS'
      ]            
      ruleType: 'NetworkRule'
      destinationFqdns: []
    }
  ]
}

// 导出变量供其他文件引用
export var testRule = testRafaelRule

第二步:主Bicep文件导入对象并填充属性

在包含fwll资源的文件中,导入变量后直接填入ruleCollections数组即可:

// 按实际相对路径引入test.bicep中导出的变量
import { testRule } from './test.bicep'

resource fwll 'Microsoft.Network/firewallPolicies/ruleCollectionGroups@2020-11-01' = {  
  name: 'netrules'  
  properties: {
    priority: 200
    ruleCollections: [
      testRule // 直接插入导入的规则对象,如需追加其他规则用逗号分隔即可
    ]
  }
}

方案2:使用无资源Module输出(兼容旧版Bicep)

如果你的Bicep版本不支持import/export语法,可以用空Module输出变量的方式实现,仅会产生一条无实际资源部署的module记录。

第一步:修改test.bicep,将规则作为output输出

test.bicep不需要定义任何资源,直接输出规则对象即可:

// test.bicep 完整内容
var testRafaelRule = {
  name: 'testRafaelRule'
  priority: 1001
  ruleCollectionType: 'FirewallPolicyFilterRuleCollection'
  action: {
    type: 'Allow'
  }
  rules: [
    {
      name: 'deleteme-1'
      ipProtocols: [
        'Any'
      ]
      destinationPorts: [
        '*'
      ]
      sourceAddresses: [
        '192.168.0.0/16'
      ]
      sourceIpGroups: []
      destinationIpGroups: []
      destinationAddresses: [
        'AzureCloud.EastUS'
      ]            
      ruleType: 'NetworkRule'
      destinationFqdns: []
    }
  ]
}

output testRule object = testRafaelRule

第二步:主Bicep文件引用Module输出

// 引入test.bicep作为module,无需传入任何参数
module testRuleModule './test.bicep' = {
  name: 'fetchTestRule'
}

resource fwll 'Microsoft.Network/firewallPolicies/ruleCollectionGroups@2020-11-01' = {  
  name: 'netrules'  
  properties: {
    priority: 200
    ruleCollections: [
      testRuleModule.outputs.testRule // 直接引用module的输出对象
    ]
  }
}

内容的提问来源于stack exchange,提问作者user3696106

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 18:06:02