更新template_file中的计数不会影响输出

时间:2018-11-29 13:42:17

标签: terraform

我们拥有一个使用大量已计数资源的系统。到目前为止,它们已经被硬编码为2。我刚刚引入了一个新变量pairs,其默认值为1,因此我们可以开始增加它。如果它保持默认值1,则结果plan不会显示任何更改。以下是其中两个正在发挥作用的项目:

data "template_file" "bootstrapuserdata" {
    count   = "${var.pairs * 2}"
    template = "${file("${path.module}/startup.ps1")}"
    vars {
        domain = "${var.domain}"
        region = "${var.region}"
        publicIp = "${element(var.public_ips, count.index)}"
        stack = "${var.stack}"
    }
}


resource "aws_launch_configuration" "lc" {
    count = "${var.pairs * 2}"
    name_prefix = "lc-${element(var.private_subnet_ids, count.index)}-"
    image_id = "${var.ami_id}"
    instance_type = "${var.instance_type}"
    iam_instance_profile = "${aws_iam_instance_profile.webserver_instance_profile.id}"
    key_name = "${var.key_name}"
    security_groups = ["${var.sg_id}", "${var.sg2_id}"]
    user_data = "${data.template_file.bootstrapuserdata.*.rendered[count.index]}"
    lifecycle {
        create_before_destroy = true
    }
}

如果我将pairs的值更改为2(两个项目的结果均为4),我会从plan得到以下错误:

Error: Error refreshing state: 1 error(s) occurred:


* module.servers.aws_launch_configuration.lc: 2 error(s) occurred:

* module.servers.aws_launch_configuration.lc[3]: index 3 out of range for list data.template_file.bootstrapuserdata.*.rendered (max 2) in:

${data.template_file.bootstrapuserdata.*.rendered[count.index]}
* module.servers.aws_launch_configuration.lc[2]: index 2 out of range for list data.template_file.bootstrapuserdata.*.rendered (max 2) in:

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

这好像没有用更新的计数重新呈现template_file。如果我故意破坏template_file,则可以确认这一点:

data "template_file" "bootstrapuserdata" {
    count   = "${var.pairs * 2}"
    template = "${file("${path.module}/startup.ps1")}"
    vars {
        #domain = "${var.domain}"
        region = "${var.region}"
        publicIp = "${element(var.public_ips, count.index)}"
        stack = "${var.stack}"
    }
}

输出:

Error: Error refreshing state: 1 error(s) occurred:

* module.servers.data.template_file.bootstrapuserdata: 2 error(s) occurred:

* module.servers.data.template_file.bootstrapuserdata[1]: data.template_file.bootstrapuserdata.1: failed to render : 28:66: unknown variable accessed: domain
* module.servers.data.template_file.bootstrapuserdata[0]: data.template_file.bootstrapuserdata.0: failed to render : 28:66: unknown variable accessed: domain

但是,如果我真的打破template_file

data "template_file" "bootstrapuserdata" {
    count   = "${var.pairs * 2}"
    template = "${file("${path.module}/startup.ps1")}"
    /*vars {
        domain = "${var.domain}"
        region = "${var.region}"
        publicIp = "${element(var.public_ips, count.index)}"
        stack = "${var.stack}"
    }*/
}

它现在知道全部4个:

Error: Error refreshing state: 1 error(s) occurred:

* module.servers.data.template_file.bootstrapuserdata: 4 error(s) occurred:

* module.servers.data.template_file.bootstrapuserdata[3]: data.template_file.bootstrapuserdata.3: failed to render : 15:106: unknown variable accessed: domain
* module.servers.data.template_file.bootstrapuserdata[1]: data.template_file.bootstrapuserdata.1: failed to render : 15:106: unknown variable accessed: domain
* module.servers.data.template_file.bootstrapuserdata[2]: data.template_file.bootstrapuserdata.2: failed to render : 15:106: unknown variable accessed: domain
* module.servers.data.template_file.bootstrapuserdata[0]: data.template_file.bootstrapuserdata.0: failed to render : 15:106: unknown variable accessed: domain

这是怎么回事?我偶然发现了Terraform错误吗?

我从Terraform 0.10.6开始,但是看到这个问题后又升级到0.11.10-两者之间没有区别。

1 个答案:

答案 0 :(得分:0)

经过进一步的研究,finding this issue我尝试了一些实验:

如果我改用resource "template_file"而不是data "template_file" 并更改模板名称,它可以解决我的问题。将其保留在data上并更改名称不起作用,并且在不更改名称的情况下切换到resource也不起作用。

总的来说,这在某种程度上确实是Terraform内部的一个问题,无论我是否链接到上面,我都不确定。