aukjan / terraform-playground

Random code from working with Terraform (http://terraform.io)
8 stars 7 forks source link

Update docs with example of using IPs from resource #1

Open ababra opened 7 years ago

ababra commented 7 years ago

UPDATE: just realized that your README does not mention the post on your website; can it be updated to link to the original post?

The example is currently hardcoded with a string of fake IPs. Could a real-world example also be provided, i.e. using data from an actual resource (DigitalOcean/AWS, etc.)?

I have been trying to modify the outputs.tf file as follows, however it only outputs the first generated IP from the resource:

data "template_file" "ansible_hosts" {
  template = "${file("ansible_hosts.tpl")}"
  vars {
    # This works for first IP
    do_hosts = "Instances: ${element(digitalocean_droplet.host.*.ipv4_address, count.index)}"
  }
}

output "ansible_hosts" {
  value = "${data.template_file.ansible_hosts.rendered}"
}

For reference, here is my resource:

# Create DigitalOcean nodes
resource "digitalocean_droplet" "host" {
  count = "${var.do_count}"
  image  = "${var.do_image}"
  name   = "host-${format("%02d", count.index+1)}"
  region = "${var.do_region}"
  size   = "${var.do_size}"
  ssh_keys = ["${digitalocean_ssh_key.ssh-key.id}"]
}
seanorama commented 7 years ago

^ Same request as above for an example of how to get the IP from each created hosts rather than the static list example.