在terraform中使用count.index吗? [英] using count.index in terraform?

查看:491
本文介绍了在terraform中使用count.index吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从模板生成一堆文件-我需要用count.index替换硬编码的1,但不确定terraform允许使用什么格式.

I am trying to generate a bunch of files from templates - I need to replace the hardcoded 1 with the count.index , not sure what format terraform will allow we to use.

resource "local_file" "foo" {
  count = "${length(var.files)}"

  content  = "${data.template_file.tenant_repo_multi.1.rendered}"
  #TODO: Replace 1 with count index.
  filename = "${element(var.files, count.index)}"
}


data "template_file" "tenant_repo_multi" {

  count = "${length(var.files)}"
  template = "${file("templates/${element(var.files, count.index)}")}"

}

variable "files" {
  type    = "list"
  default = ["filebeat-config_filebeat.yml",...]
}

我正在跑步

Terraform v0.11.7
+ provider.gitlab v1.0.0
+ provider.local v1.1.0
+ provider.template v1.0.0

推荐答案

您可以像这样遍历tenant_repo_multi数据源-

You can iterate through the tenant_repo_multi data source like so -

resource "local_file" "foo" {
  count    = "${length(var.files)}"
  content  = "${element(data.template_file.tenant_repo_multi.*.rendered, count.index)}"
  filename = "${element(var.files, count.index)}"
}

但是,您是否考虑过使用 template_dir 资源Terraform模板提供程序.下面的示例-

However, have you considered using the template_dir resource in the Terraform Template provider. An example below -

resource "template_dir" "config" {
    source_dir      = "./unrendered"
    destination_dir = "./rendered"

    vars = {
        message = "world"
    }
}

这篇关于在terraform中使用count.index吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆