英文:
How to output a map in inventory file format using templatefile terraform function
问题
I have a map variable given below local.inventory_map
{
"black" = [
"xyz",
]
"blue" = [
"abc",
"xyz",
]
"geen" = [
"abc",
]
"red" = [
"abc",
"xyz",
]
"yellow" = [
"xyz",
]
}
I am trying to use the Terraform templatefile
function to get the following text file, Expected output
[black]
xyz
[blue]
abc
xyz
[green]
abc
[red]
abc
xyz
[yellow]
xyz
I tried
resource "local_file" "host_file" {
content = templatefile(".hosts.yaml.tftpl",
{
color_groups = local.inventory_map
}
)
}
and then in the hosts.yaml.tftpl
file, I just tried to output the map but got an error. Please help.
%{ for key, value in color_groups ~}
${key}:${value}
%{ endfor ~}
Note: The original text includes HTML escape codes for double quotes, which have been converted to regular double quotes for readability.
英文:
I have a map variable given below local.inventory_map
{
"black" = [
"xyz",
]
"blue" = [
"abc",
"xyz",
]
"geen" = [
"abc",
]
"red" = [
"abc",
"xyz",
]
"yellow" = [
"xyz",
]
}
I am trying to use the terrafrom templatefile function to get the following text file, Expected output
[black]
xyz
[blue]
abc
xyz
[green]
abc
[red]
abc
xyz
[yellow]
xyz
I tried
resource "local_file" "host_file" {
content = templatefile(".hosts.yaml.tftpl",
{
color_groups = local.inventory_map
}
)
and then in the hosts.yaml.tftpl file, I just tried to output the map but got an error. Please help.
%{ for key, value in color_groups ~}
${key}:${value}
%{ endfor ~}
答案1
得分: 2
这应该可以:
locals {
stuff = {
"black" = [
"xyz",
]
"blue" = [
"abc",
"xyz",
]
"green" = [
"abc",
]
"red" = [
"abc",
"xyz",
]
"yellow" = [
"xyz",
]
}
out_stuff = [
for k, v in local.stuff :
format("[%s]\n%s",
k,
join("",
[
for s in v : "${s}\n"
]
))
]
}
output "stuff" {
value = local.out_stuff
}
resource "local_file" "host_file" {
filename = "${path.module}/foo.yml"
content = join("", local.out_stuff)
}
这是我的输出文件的样子:
英文:
This should do it:
locals {
stuff = {
"black" = [
"xyz",
]
"blue" = [
"abc",
"xyz",
]
"geen" = [
"abc",
]
"red" = [
"abc",
"xyz",
]
"yellow" = [
"xyz",
]
}
out_stuff = [
for k, v in local.stuff :
format("[${k}]\n%s",
join("",
[
for s in v : "${s}\n"
]
))
]
}
output stuff {
value = local.out_stuff
}
resource "local_file" "host_file" {
filename = "${path.module}/foo.yml"
content = join("", local.out_stuff)
}
this is what my output file looks like:
答案2
得分: 0
我正在寻找这个答案,以获得我所期望的输出
%{ for key, value in color_groups ~}
[${ key }]
%{ for v in value ~}
${ v }
%{ endfor ~}
%{ endfor ~}
英文:
I was looking for this answer to get my desired output
%{ for key, value in color_groups ~}
[${ key }]
%{ for v in value ~}
${ v }
%{ endfor ~}
%{ endfor ~}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论