过滤 terraform 列表中的特定值
Filter specific values in terraform list
我有一个生成 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-*"]
}
}
但是我运行进入了错误
Blocks of type "filter" are not expected here.
由于某些原因,该数据源似乎不支持过滤器,因此您需要在输出中执行正则表达式,如下所示:
output "names" {
value = [for s in data.google_compute_regions.available.names : s if length(regexall("europe.*", s)) > 0]
}
我有一个生成 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-*"]
}
}
但是我运行进入了错误
Blocks of type "filter" are not expected here.
由于某些原因,该数据源似乎不支持过滤器,因此您需要在输出中执行正则表达式,如下所示:
output "names" {
value = [for s in data.google_compute_regions.available.names : s if length(regexall("europe.*", s)) > 0]
}