遍历Terraform本地列表中的资源值列表

时间:2019-03-01 18:22:48

标签: terraform

我正在尝试获取要在Terraform template_file数据字段中使用的数组数组:

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}模板变量中扩展。

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

1 个答案:

答案 0 :(得分:0)

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

您可以将其作为地形计数资源

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.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"
            }
相关问题