在Bicep中,可以使用循环来创建多个资源。在这个问题中,我们需要在循环中分配VM到不同的可用区。
下面是一个示例Bicep代码,使用循环和if语句将VM分配到不同的可用区:
// 定义可用区
var zones = ['1', '2', '3']
// 创建多个VM
resource vm 'Microsoft.Compute/virtualMachines@2021-03-01' = [for i in range(0, 3): {
  name: 'vm-${i}',
  location: resourceGroup().location,
  properties: {
    hardwareProfile: {
      vmSize: 'Standard_D2_v2'
    },
    storageProfile: {
      imageReference: {
          publisher: 'Canonical',
          offer: 'UbuntuServer',
          sku: '16.04-LTS',
          version: 'latest'
      },
      osDisk: {
          createOption: 'FromImage'
      }
    },
    osProfile: {
      computerName: 'vm-${i}',
      adminUsername: 'testuser',
      adminPassword: 'Password1234!'
    },
    networkProfile: {
        networkInterfaces: [
            {
                id: resourceId('Microsoft.Network/networkInterfaces', format('{0}-nic-{1}', 'vm', i))
            }
        ]
    },
    availabilitySet: {
        id: resourceId('Microsoft.Compute/availabilitySets', format('{0}-as-{1}', 'vm', i))
    }
  }
}]
// 分配可用区
resource nic 'Microsoft.Network/networkInterfaces@2020-06-01' = [for i in range(0, 3): {
  name: format('{0}-nic-{1}', 'vm', i)
  location: resourceGroup().location
  properties: {
    ipConfigurations: [
      {
        name: 'ipconfig1'
        properties: {
          subnet: {
            id: subnetRef('default', format('vnet-{0}-subnet-1', 'vm'))
          },
          privateIPAllocationMethod: 'Dynamic'
        }
      }
    ]
  }
  zones: if(i
                    上一篇:BICC提取不正确的值