如何使用`templatefile` Terraform函数输出一个符合清单文件格式的地图。

huangapple go评论57阅读模式
英文:

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)
}

这是我的输出文件的样子:

如何使用`templatefile` Terraform函数输出一个符合清单文件格式的地图。

英文:

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:

如何使用`templatefile` Terraform函数输出一个符合清单文件格式的地图。

答案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 ~}

huangapple
  • 本文由 发表于 2023年3月31日 22:45:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定