0

我正在尝试创建一个二头肌模板来根据条件部署具有 1 个或 2 个 NIC 的 VM。

任何人都知道是否有办法在属性定义中使用条件语句来部署 VM NIC?似乎资源定义中不允许使用 if 函数,并且由于 ID 无效而导致三元错误。

只是试图避免使用 resource = if (bool) {} 有 2 个重复的 VM 资源定义

networkProfile: {
  networkInterfaces: [
    {
      id: nic_wan.id
      properties: {
        primary: true
      }
    }
    
    {
      id: bool ? nic_lan.id : '' #Trying to deploy this as a conditional if bool = true.
      properties: {
        primary: false
      }
    }

  ]
}

上面的代码出错了,因为一旦你定义了一个 NIC,它就需要一个有效的 ID。

“properties.networkProfile.networkInterfaces[1].id”无效。期望以“/subscriptions/{subscriptionId}”或“/providers/{resourceProviderNamespace}/”开头的完全限定资源 ID。(代码:LinkedInvalidPropertyId)

4

1 回答 1

1

您可以创建一些变量来处理它:

// Define the default nic
var defaultNic = [
  {
    id: nic_wan.id
    properties: {
      primary: true
    }
  }
]

// Add second nic if required
var nics = concat(defaultNic, bool ? [
  {
    id: nic_lan.id
    properties: {
      primary: false
    }
  }
] : [])

// Deploy the VM
resource vm 'Microsoft.Compute/virtualMachines@2020-12-01' = {
  ...
  properties: {
    ...
    networkProfile: {
      networkInterfaces: nics
    }
  }
}
于 2021-08-26T02:50:20.560 回答