【问题标题】:Iterating through a list of resource values inside a list in a Terraform local遍历 Terraform 本地列表中的资源值列表
【发布时间】:2019-07-23 19:39:40
【问题描述】:

我正在尝试获取要在 Terraform 模板文件数据字段中使用的数组数组:

data "template_file" "dashboard" {
  template = "${file("${path.module}/files/dashboard.json")}"

  vars {
    metrics = "${jsonencode(local.metrics)}"
  }
}

但我没有找到正确的方法来获得我想要的东西。我有一个计数为 3 的 aws_instance 资源,我试图根据每个资源计数在本地生成 3 个数组。到目前为止,我唯一想到的是:

locals {
  metrics = [
    "collectd", "GenericJMX.gauge.50thPercentile", "Host", "${aws_instance.instance.*.id}", "PluginInstance", "cassandra_client_request-latency"
  ]
}

显然,这样做是将所有实例一个接一个地放在同一个数组中。我想要实现的是一个结果数组,看起来像:

["collectd", "GenericJMX.gauge.50thPercentile", "Host", "the id of instance 0", PluginInstance", "cassandra_client_request-latency"], 
["collectd", "GenericJMX.gauge.50thPercentile", "Host", "the id of instance 1", PluginInstance", "cassandra_client_request-latency"], 
["collectd", "GenericJMX.gauge.50thPercentile", "Host", "the id of instance 3", PluginInstance", "cassandra_client_request-latency"]

这将在模板 ${metrics} 变量中展开。

有什么方法可以在本地实现我想要的,并使其在模板中可用?

【问题讨论】:

标签: terraform


【解决方案1】:

terraform 数据源也支持count

这是一项隐藏功能,永远不会被记录在案 (https://github.com/hashicorp/terraform/pull/8635)

对你的dashboard.json做一些调整,然后使用下面的代码来生成一些template_file数据源资源。

data "template_file" "dashboard" {
  count = "${length(aws_instance.instance.*.id)}"
  template = "${file("${path.module}/files/dashboard.json")}"

  vars {
    metrics = "${element(aws_instance.instance.*.id, count.index)}"
  }
}

您可以将其作为 terraform 计数资源引用

count = "${length(aws_instance.instance.*.id)}"

${data.template_file.dashboard.*.rendered[count.index]}"

这是完整的测试数据。

$ cat main.tf
data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "instance" {
  count         = 2
  ami           = "${data.aws_ami.ubuntu.id}"
  instance_type = "t2.micro"

  tags = {
    Name = "HelloWorld"
  }
}


data "template_file" "dashboard" {
  count    = "${length(aws_instance.instance.*.id)}"
  template = "${file("${path.module}/files/dashboard.json")}"

  vars {
    metric = "${element(aws_instance.instance.*.id, count.index)}"
  }
}

output "aws_instances" {
  value = "${length(aws_instance.instance.*.id)}"
}

$ cat files/dashboard.json
["collectd", "GenericJMX.gauge.50thPercentile", "Host", "${metric}", PluginInstance", "cassandra_client_request-latency"]

应用更改后,检查 tfstate 文件,数据源是

data.template_file.dashboard.0
data.template_file.dashboard.1

示例 tfstate:

            "data.template_file.dashboard.1": {
                "type": "template_file",
                "depends_on": [
                    "aws_instance.instance.*"
                ],
                "primary": {
                    "id": "8e05e7c115a8d482b9622a1eddf5ee1701b8cc4695da5ab9591899df5aeb703d",
                    "attributes": {
                        "id": "8e05e7c115a8d482b9622a1eddf5ee1701b8cc4695da5ab9591899df5aeb703d",
 # the date is here ==> "rendered": "[\"collectd\", \"GenericJMX.gauge.50thPercentile\", \"Host\", \"i-015961b744ff55da4\", PluginInstance\", \"cassandra_client_request-latency\"]\n",
                        "template": "[\"collectd\", \"GenericJMX.gauge.50thPercentile\", \"Host\", \"${metric}\", PluginInstance\", \"cassandra_client_request-latency\"]\n",
                        "vars.%": "1",
                        "vars.metric": "i-015961b744ff55da4"
                    },
                    "meta": {},
                    "tainted": false
                },
                "deposed": [],
                "provider": "provider.template"
            }

【讨论】:

    猜你喜欢
    • 2019-08-24
    • 2017-07-23
    • 2019-03-21
    • 2020-11-27
    • 1970-01-01
    • 2022-01-04
    • 2019-08-08
    • 2020-05-18
    • 2021-11-27
    相关资源
    最近更新 更多