Asier Gomez
Asier Gomez

Reputation: 6578

Terraform provisioner ilegal char with resources lists

I have deployed a resource list using 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}"
        }
}

I am trying to do a local provisioner with that list:

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

But it throws:

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

I just try to use like this to:

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

But it doesn't write nothing. And it throws:

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

I just try using with one simple resource and it works fine.

Upvotes: 0

Views: 164

Answers (1)

George Richardson
George Richardson

Reputation: 1268

You will need to join the list with a delimeter to get a string like this:

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

The join(",", aws_instance.masters.*.private_ip) part uses the join function to add commas between each item. You can find docs on interpolation here.

Upvotes: 2

Related Questions