Terraform provisioner ilegal char with resources list

时间:2018-02-06 09:57:17

标签: amazon-web-services terraform

我使用terraform部署了资源列表:

resource "aws_instance" "masters" {
    count = 2
    ami = "${var.aws_centos_ami}"
    instance_type = "t2.micro"
    ....

    availability_zone = "eu-west-1b"

    tags {
            Name = "master-${count.index}"
        }
}

我正在尝试使用该列表执行本地配置程序:

provisioner "local-exec" {
    command =  "echo \"["${aws_instance.masters.*.private_ip}"]\" >> ../ansible-provision/inventory/hosts.ini"
}

但它抛出:

Error: Error parsing /home/asier/Ik4-Data-Platform/AWS/Pruebas/simplificando/aws-infraestructure/aws-vpc.tf: At 117:26: illegal char

我只是尝试这样使用:

provisioner "local-exec" {
    command =  "echo \"${aws_instance.masters.*.private_ip}\" >> ../ansible-provision/inventory/hosts.ini"
}

但它什么都不写。它抛出:

Error: Error applying plan:

1 error(s) occurred:

* null_resource.ansible-provision: 1 error(s) occurred:

* At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 2 is TypeList) in:

echo ${aws_instance.masters.*.private_ip} >> ../ansible-provision/inventory/hosts.ini

我只是尝试使用一个简单的资源,它工作正常。

1 个答案:

答案 0 :(得分:2)

您需要使用分隔符加入列表以获取如下字符串:

provisioner "local-exec" {
    command =  "echo \"${join(",", aws_instance.masters.*.private_ip)}\" >> ../ansible-provision/inventory/hosts.ini"
}

join(",", aws_instance.masters.*.private_ip)部分使用join功能在每个项目之间添加逗号。您可以找到有关插值here的文档。