我在我的 terraform 模块中定义了多个输出,并希望在一行中包含类似
name + public_ip + private_ip
之类的内容时合并它们。这可能吗?
我的模块输出:
output "name" {
description = "List with names of the servers"
value = hcloud_server.this[*].name
}
output "public_ip" {
description = "List with public IPv4 IPs of the instances"
value = hcloud_server.this[*].ipv4_address
}
output "private_ip" {
description = "List with public IPv4 IPs of the instances"
value = flatten(hcloud_server.this[*].network)[*].ip
}
output "id" {
description = "List with IDs of the servers"
value = hcloud_server.this[*].id
}
output "test" {
description = "List with public IPv4 IPs of the instances"
value = hcloud_server.this[*]
}
我尝试了
merge()
,但没有成功
我创建了一个变量
hcloud_server
来模拟您的列表...
以下是示例代码
variable "hcloud_server" {
type = object({
this = list(object({
name = string
ipv4 = string
}))
})
default = {
this: [
{name :"a", ipv4: "10.0.0.1"},
{name :"b", ipv4: "10.0.0.2"},
{name :"c", ipv4: "10.0.0.3"},
]
}
}
output "name" {
description = "List with names of the servers"
value = var.hcloud_server.this[*].name
}
output "public_ip" {
description = "List with public IPv4 IPs of the instances"
value = var.hcloud_server.this[*].ipv4
}
output "test" {
value = [for x in var.hcloud_server.this: "${x.name} + ${x.ipv4}"]
}
重要的部分是:
[for x in var.hcloud_server.this: "${x.name} + ${x.ipv4}"]
for
循环和一个字符串插值来添加我们需要的内容