Provisioner IP address access with count > 1

1,388 views
Skip to first unread message

Kale Franz

unread,
May 13, 2015, 3:01:47 PM5/13/15
to terrafo...@googlegroups.com
Following the local-exec provisioner documentation, I'd like to create three ec2 instances.  Here's the modified appropriate syntax as far as I can tell:

resource "aws_instance" "web" {
    ...
    count = 3
    provisioner "local-exec" {
        command = "echo ${element(aws_instance.web.*.private_ip, count.index)} >> private_ips.txt"
    }
}

When planning or applying, I get the following error:


* aws_instance.web: connection info cannot contain splat variable referencing itself
Any ideas on how to make this example work?

More fundamentally, I'm trying to work out using ansible (executed locally) as a provisioner. Any general advice or pointers would be much appreciated!

Paul Hinze

unread,
May 13, 2015, 3:12:41 PM5/13/15
to Kale Franz, terrafo...@googlegroups.com
Hey Kale,

From a provisioner you can reference an attribute from the current resource by using ${self.attr} [1].

Give this a try:

    provisioner "local-exec" {
        command = "echo ${self.private_ip} >> private_ips.txt"
    }

Hope this helps!

Paul

--
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To post to this group, send email to terrafo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/6534f14a-6fb5-4813-93de-7459ae4f1dc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kale Franz

unread,
May 13, 2015, 3:48:34 PM5/13/15
to terrafo...@googlegroups.com, eet...@gmail.com
Perfect. Exactly what I needed. Thanks!

Ioannis Polyzos

unread,
Jun 5, 2015, 6:45:42 AM6/5/15
to terrafo...@googlegroups.com, eet...@gmail.com
Hi Paul, 

  I have similar case with OpenStack provider. I would like to list ALL the private IP addresses of my instances and pass them to a remote script.  

  How you would advice aproach such scenario?

Regards, 
Ioannis

Paul Hinze

unread,
Jun 5, 2015, 10:16:40 AM6/5/15
to Ioannis Polyzos, terrafo...@googlegroups.com
Ioannis,

We're planning better core support for this scenario down the road, but for now there's a workaround with "null_resource" - a resource that does nothing but allow you to place provisioners at certain points in the dependency graph.

Here's the idea:

resource "openstack_compute_instance_v2" "foo" {
  count = 5
  // ...
}

resource "null_resource" "foo_provisioning" {
  provisioner "local-exec" {
    command = "provision-all-instances.sh ${join(" ", openstack_compute_instance_v2.foo.*.access_ip_v4)}"
  }
}

Hopefully that points you in the right direction!

Paul



Reply all
Reply to author
Forward
0 new messages