首页 > 解决方案 > terraform 中一种资源的互斥属性

问题描述

我需要一些帮助,我正在尝试为 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

标签: azureterraformterraform-provider-azure

解决方案


您可以使用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
}

推荐阅读