【问题标题】:mutually exclusive attributes to one resource in terraformterraform 中一种资源的互斥属性
【发布时间】:2021-08-23 12:25:02
【问题描述】:

我需要一些帮助,我正在尝试为 azure ACI 容器实例编写一个模块。 我发现两个属性是互斥的,dns_name_label 和 network_profile_id。 如果我将ip_address_type设置为public,我想使用dns_name_label,但是network_profile_id不能来同一个脚本 反之亦然,如果我将ip_address_type设置为Private,我必须定义network_profile_id,但是dns_name_label不能来脚本

如果要同时包含 dns_name_label 和 network_profile_id,根据 ip_address_type 判断?

resource "azurerm_container_group" "this" {
  name                = var.name
  location            = var.location
  resource_group_name = var.resource_group_name
  ip_address_type     = var.ip_address_type
  dns_name_label = var.ip_address_type =="Public"&&length(var.dns_name_label)> 0 ? var.dns_name_label :""
   os_type            = var.os_type
  restart_policy     = var.restart_policy
  network_profile_id = var.ip_address_type == "Private" ? azurerm_network_profile.this[0].id : ""

}

注意上面的代码不起作用,我得到错误

"network_profile_id": conflicts with dns_name_label

【问题讨论】:

    标签: azure terraform terraform-provider-azure


    【解决方案1】:

    您可以使用null 代替"",这将消除资源中的属性,将null 设置为其值:

    resource "azurerm_container_group" "this" {
      name                = var.name
      location            = var.location
      resource_group_name = var.resource_group_name
      ip_address_type     = var.ip_address_type
      dns_name_label = var.ip_address_type =="Public"&&length(var.dns_name_label)> 0 ? var.dns_name_label : null
       os_type            = var.os_type
      restart_policy     = var.restart_policy
      network_profile_id = var.ip_address_type == "Private" ? azurerm_network_profile.this[0].id : null
    }
    

    【讨论】:

    • 谢谢!这样可行!我以为“”和null的效果一样,显然不是lol
    • @RogerChen 没问题。很高兴它解决了:-)
    猜你喜欢
    • 2019-11-20
    • 1970-01-01
    • 2018-11-25
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    相关资源
    最近更新 更多