迭代Terraform 0.12中的地图的地图

问题描述 投票:0回答:1

我需要建立一个templatefile这样的列表:

templatefile("${path.module}/assets/files_eth0.nmconnection.yaml", {
  interface-name = "eth0",
  addresses = element(values(var.virtual_machines), count.index),
  gateway = element(var.gateway, count.index % length(var.gateway)),
  dns = join(";", var.dns_servers),
  dns-search = var.domain,
  }),
templatefile("${path.module}/assets/files_etc_hostname.yaml", {
  hostname = element(keys(var.virtual_machines), count.index),
  }),

通过遍历如下所示的地图:

variable templatefiles {
  default = {
    "files_eth0.nmconnection.yaml" = {
      "interface-name" = "eth0",
      "addresses" = "element(values(var.virtual_machines), count.index)",
      "gateway" = "element(var.gateway, count.index % length(var.gateway))",
      "dns" = "join(";", var.dns_servers)",
      "dns-search" = "var.domain",
    },
    "files_etc_hostname.yaml" = {
      "hostname" = "host1"
    }
  }
}

我对文件列表做了类似的操作:

file("${path.module}/assets/files_90-disable-console-logs.yaml"),
file("${path.module}/assets/files_90-disable-auto-updates.yaml"),

...但想将此扩展为templatefiles(上方)。

这是我为files列表所做的代码:

main.tf

variable files {
  default = [
    "files_90-disable-auto-updates.yaml",
    "files_90-disable-console-logs.yaml",
  ]
}

output "snippets" {
  value = flatten(module.ingition_snippets.files)
}

modules / main.tf

variable files {}

resource "null_resource" "files" {
  for_each = toset(var.files)
  triggers = {
    snippet = file("${path.module}/assets/${each.value}")
  }
}

output "files" {
  value = [for s in null_resource.files: s.triggers.*.snippet]
}

感谢任何帮助!

terraform nested-loops
1个回答
0
投票

这两个用例都可以满足,而根本不使用任何resource块,因为Terraform语言内置了必要的功能。

这是使用静态文件编写示例的较短方法:

variable "files" {
  type = set(string)
}

output "files" {
  value = tomap({
    for fn in var.files : fn => file("${path.module}/assets/${fn}")
  })
}

以上将产生从文件名到文件内容的映射,因此调用模块可以更轻松地访问各个文件内容。

我们可以像这样将其适应于templatefile

variable "template_files" {
  # We can't write down a type constraint for this case
  # because each file might have a different set of
  # template variables, but our later code will expect
  # this to be a mapping type, like the default value
  # you shared in your comment, and will fail if not.
  type = any
}

output "files" {
  value = tomap({
    for fn, vars in var.template_files : fn => templatefile("${path.module}/assets/${fn}", vars)
  })
}

同样,结果将是从文件名到使用给定变量渲染模板的结果的映射。


如果您的目标是构建一个模块来从源目录渲染模板以发布到某个地方,则可能会发现模块hashicorp/dir/template很有用。它将hashicorp/dir/templatefilesetfile组合在一起,希望可以方便地进行静态网站发布和类似的用例。 (在我撰写本文时,该模块正在从我的个人GitHub帐户过渡为HashiCorp组织的身份,因此如果您很快查看它,可能会在文档更新后看到一些动荡,等等。)

© www.soinside.com 2019 - 2024. All rights reserved.