在Bicep模板中定义的网络访问限制规则之前,添加以下代码:
resource existingNSGRule 'Microsoft.Network/networkSecurityGroups/securityRules@2018-02-01' = {
  name: 'exampleExistingNSGRule'
  properties: {
    protocol: 'tcp'
    sourcePortRange: '*'
    destinationPortRange: '22'
    sourceAddressPrefix: '10.0.0.0/16'
    destinationAddressPrefix: '10.0.0.0/16'
    access: 'Allow'
    priority: 200
    direction: 'Inbound'
  }
}
其中, existingNSGRule 是你定义的规则名称。在此示例中,它是“exampleExistingNSGRule”。在Bicep模板中的每个 networkSecurityGroup 资源之前,将上面的代码添加到模板中,以创建现有的网络安全组规则。
然后,你可以在 networkSecurityGroup 资源中使用 existingNSGRule 定义的规则。例如:
resource nsg 'Microsoft.Network/networkSecurityGroups@2018-02-01' = {
  name: 'exampleNSG'
  properties: {
    securityRules: [
      existingNSGRule
      {
        name: 'exampleRule'
        properties: {
          protocol: 'tcp'
          sourcePortRange: '22'
          destinationPortRange: '*'
          sourceAddressPrefix: '10.0.0.0/24'
          destinationAddressPrefix: 'VirtualNetwork'
          access: 'Allow'
          priority: 300
          direction: 'Inbound'
        }
      }
    ]
  }
}
在上面的示例中,现有的网络安全组规则 existingNSGRule 将首先应用于 networkSecurityGroup 资源。然后,定义具体的安全规则 exampleRule。