hashicorp / terraform-provider-template

Terraform template provider
https://www.terraform.io/docs/providers/template/
Mozilla Public License 2.0
131 stars 89 forks source link

parametrized terraform template #7

Open hashibot opened 7 years ago

hashibot commented 7 years ago

This issue was originally opened by @reda-dw as hashicorp/terraform#12956. It was migrated here as part of the provider split. The original body of the issue is below.


I have a terraform project to create a 99 virtual machines in Openstack i can not use cloud-init and i must modify the hostname of every machine

hostname.tplt :

sudo sed -i -e "s/debian[7-9]/${host_name}/g" /etc/hostname
sudo invoke-rc.d hostname.sh start

sudo sed -i -e "s/127\.0\.1\.1.*/127.0.1.1\t${host_name}.${domain_name} ${host_name}/g" /etc/hosts
sudo apt-get update && sudo apt-get -y install dbus && sudo hostnamectl set-hostname ${host_name}

part of main.tf :

data "template_file" "hostname_servers" {
  template = "${file("templates/hostname.tplt")}"

  vars {
    host_name   = "${format("%s-proxy-%02d", var.prefix_name, count.index+1)}"
    domain_name = "${var.domain_name}"
  }
}

Ressource

resource "openstack_compute_instance_v2" "proxy-instance" {
  count       = "${var.count_proxy}"
  name        = "${format("%s-proxy-%02d", var.prefix_name, count.index+1)}"
  image_name  = "${var.image}"
  flavor_name = "${var.flavor_proxy}"

  network {
    name = "${format("%s-%s", var.prefix_name, var.network_name)}"
  }

  connection {
    user = "${var.user}"
  }

  provisioner "remote-exec" {
    inline = [
      "${data.template_file.hostname_servers.rendered}"
    ]
  }
}

the use case : when i start a terraform plan it works for the proxy-instance resource but i need to do that for the 99 machines, i don't like to duplicate the templates data 99 times, and i don't know how to parammetrize the template to be able to apply for all the machines any idea please?