gpt4 book ai didi

terraform - 过滤 terraform 列表中的特定值

转载 作者:行者123 更新时间:2023-12-05 01:31:27 25 4
gpt4 key购买 nike

我有一个生成 gcp 区域列表的 terraform 代码块

data "google_compute_regions" "available" {
project = var.project
}

output "name" {
value = data.google_compute_regions.available.names
}
  ~ name = [
+ "asia-east1",
+ "asia-east2",
+ "asia-northeast1",
+ "asia-northeast2",
+ "asia-northeast3",
+ "asia-south1",
+ "asia-southeast1",
+ "asia-southeast2",
+ "australia-southeast1",
+ "europe-north1",
+ "europe-west1",
+ "europe-west2",
+ "europe-west3",
+ "europe-west4",
+ "europe-west6",
+ "northamerica-northeast1",
+ "southamerica-east1",
+ "us-central1",
+ "us-east1",
+ "us-east4",
+ "us-west1",
+ "us-west2",
+ "us-west3",
+ "us-west4",
]

但是,我只想过滤掉欧洲地区。

这样做

output "names" {
value = [for s in data.google_compute_regions.available.names : s if s == "europe-west1"]
}

只给我一个区域,这不是我想要的。

+ names = [
+ "europe-west1",
]

我不确定是否支持通配符或如何为其编写逻辑。

我试过按照 here 所述使用文件管理器但我似乎没有做对。

data "google_compute_regions" "available" {
project = var.project

filter {
name = "name"
values = ["europe-*"]
}
}

但是我遇到了错误此处不应使用“filter”类型的 block 。

最佳答案

由于某些原因,该数据源似乎不支持过滤器,因此您需要在输出中执行正则表达式,如下所示:

output "names" {
value = [for s in data.google_compute_regions.available.names : s if length(regexall("europe.*", s)) > 0]
}

关于terraform - 过滤 terraform 列表中的特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66279791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com