gpt4 book ai didi

terraform - 从 Terraform map 变量中提取键/值

转载 作者:行者123 更新时间:2023-12-04 10:41:56 28 4
gpt4 key购买 nike

我正在尝试从 terraform map 变量中提取左右值,但无法提取左侧值。以下是我的代码:-

variables.tf
variable "notebook" {
type = "map"
default = {
"01" = "a@a.com"
"02" = "b@a.com"
"03" = "c@a.com"
"04" = "d@a.com"
......
}
}

下面是我在 main.tf 中的模块
 module "instance" {
instance_ip = ["1.1.1.x", "1.1.2.y", "1.1.1.z","1.1.2.p"]
dns = ["x", "y", "z","p"]
name = ["a", "b", "c", "d"]
}

输出应如下所示:-
 module "instance" {
instance_ip = ["1.1.1.01", "1.1.2.02", "1.1.1.03","1.1.2.04" and so on]
dns = ["01", "02", "03","04" and so on]
name = ["a@a.com", "b@a.com", "c@a.com", "d@a.com and so on]
}

有什么建议么。我尝试查找变量,但只得到 a@a.com 而不是 key 。

最佳答案

您可以使用 keysvalues为此的功能:

# terraform 0.12
module "instance" {
instance_ip = ["1.1.1.${keys(var.notebook)[0]}", "1.1.1.${keys(var.notebook)[1]}", "1.1.1.${keys(var.notebook)[2]}"]
dns = keys(var.notebook)
name = values(var.notebook)
}

# terraform 0.11
module "instance" {
instance_ip = ["1.1.1.${keys(var.notebook)[0]}", "1.1.1.${keys(var.notebook)[1]}", "1.1.1.${keys(var.notebook)[2]}"]
dns = ["${keys(var.notebook)}"]
name = ["${values(var.notebook)}"]
}

关于terraform - 从 Terraform map 变量中提取键/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59890267/

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