【问题标题】:Azure Bicep - Conditional Deployment of VM PropertiesAzure 二头肌 - VM 属性的条件部署
【发布时间】:2021-10-26 01:43:30
【问题描述】:

我正在尝试创建一个二头肌模板,以根据条件部署具有 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}/'。 (代码:LinkedInvalidPropertyId)

【问题讨论】:

    标签: azure azure-resource-manager azure-bicep


    【解决方案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
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2023-02-22
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多