azure 我试图用if条件在terraform上实现一个true值,但是我的代码不能正常工作

4ktjp1zp  于 2023-08-07  发布在  其他
关注(0)|答案(1)|浏览(81)

如果我的主机名是admin,环境是uat,我想创建一个私有服务链接和私有DNS区域,但我的if语句总是返回true。下面是一段代码

resource "your_resource_type" "example_resource" {
  for_each = merge([
    for i in setproduct(keys(local.static_web_app_names), keys(var.environments)) : {
      join("-", i) = {
        environment = i[1]
        hostname    = local.static_web_app_names[i[0]]
        prefixes    = i
      }
    }
  ]...)

  private_endpoints_subnet_resource = (each.value["environment"] != "uat" && each.value["hostname"] != "admin") || each.value["environment"] != "prod" ? local.private_endpoints_subnet_resource : null
  private_dns_zone_resource        = (each.value["environment"] != "uat" && each.value["hostname"] != "admin") || each.value["environment"] != "prod" ? lookup(var.dns_zones_resources[each.value["environment"]][each.value["hostname"]], "private", null) : null
  private_dns_zones_ids            = (each.value["environment"] != "uat" && each.value["hostname"] != "admin") || each.value["environment"] != "prod" ? var.private_dns_zones_ids[each.value["environment"]] : []
}

字符串
每当我使用单个测试条件时,这都能按预期工作

resource "your_resource_type" "example_resource" {
  for_each = merge([
    for i in setproduct(keys(local.static_web_app_names), keys(var.environments)) : {
      join("-", i) = {
        environment = i[1]
        hostname    = local.static_web_app_names[i[0]]
        prefixes    = i
      }
    }
  ]...)

 
  private_endpoints_subnet_resource = each.value["environment"] != "uat"  ? local.private_endpoints_subnet_resource : null
  private_dns_zone_resource        = each.value["environment"] != "uat"  ? lookup(var.dns_zones_resources[each.value["environment"]][each.value["hostname"]], "private", null) : null
  private_dns_zones_ids            = each.value["environment"] != "uat"  ? var.private_dns_zones_ids[each.value["environment"]] : []

}


它从环境为uat的配置中删除所有专用DNS和专用服务链接。

1l5u6lss

1l5u6lss1#

从您的语句“如果我的主机名是admin,环境是uat,我想创建一个私有服务链接和私有DNS区域”。不需要“prod”检查,您应该检查它是否是==,如下所示。

private_endpoints_subnet_resource = (each.value["environment"] == "uat" && each.value["hostname"] == "admin") ? local.private_endpoints_subnet_resource : null
private_dns_zone_resource         = (each.value["environment"] == "uat" && each.value["hostname"] == "admin") ? lookup(var.dns_zones_resources[each.value["environment"]][each.value["hostname"]], "private", null) : null
private_dns_zones_ids             = (each.value["environment"] == "uat" && each.value["hostname"] == "admin") ? var.private_dns_zones_ids[each.value["environment"]] : []

字符串

相关问题