尝试在多个vpc中设置多个子网过滤关键字的出现次数并将其设置为地形中的计数的插值语法是什么
tfvars文件格式为
"subnets"
"0:10.3.0.0/24:private",
"0:10.3.1.0/24:private",
"1:10.3.2.0/24:public",
"1:10.3.3.0/24:private"
- VPC 0中有2个私有子网
- VPC 1中1个公共,1个私有
计划是将一个模块用于私有子网,另一个用于公共子网。
VPC是由另一个模块创建的。
在public_subnet/main.tf
文件中,计划是在tfvars文件中为公共var.subnets
启动一个计数器到grep,然后启动计数器(在我们的案例1中)并循环通过VPC模块生成的vpc_id
(另一个挑战是如何关联哪个ID是哪个VPC)。
在private_subnet/main.tf
中,为var.subnets
中的private关键字Grep,启动计数器并循环通过vpc_id
。
如何为这种需要格式化插值序列?
感谢
这里有一种使用terraform 0.12.x 的方法
variable "subnets" {
default = [
"0:10.3.0.0/24:private",
"0:10.3.1.0/24:private",
"1:10.3.2.0/24:public",
"1:10.3.3.0/24:private"
]
}
locals {
# return the second element in the array which is the IP/CIDR if the vpc is "0"
subnets_zero = [
for subnet in var.subnets:
split(":", subnet)[1]
if split(":", subnet)[0] == "0"
]
# return the second element in the array which is the IP/CIDR if the vpc is "1"
subnets_one = [
for subnet in var.subnets:
split(":", subnet)[1]
if split(":", subnet)[0] == "1"
]
}
output "subnets_zero" {
value = local.subnets_zero
}
output "subnets_one" {
value = local.subnets_one
}
$ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
subnets_one = [
"10.3.2.0/24",
"10.3.3.0/24",
]
subnets_zero = [
"10.3.0.0/24",
"10.3.1.0/24",
]