0
votes

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 ?

1
What should your hostnames look like? Are they all different or do they follow a pattern such as host-1.example.com? - ydaetskcoR
the hoste name is the name of resource i update my question host_name = "${format("%s-proxy-%02d", var.prefix_name, count.index+1)}" - Inforedaster
So what's wrong with what you currently have? That looks like it will work fine for up to 99 machines - ydaetskcoR
i don't like to duplicate the (data template for 99 times) 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}" } } - Inforedaster
i'd like to have the name of instance = hostname for evry machine - Inforedaster

1 Answers

0
votes

If you set count to the same value on multiple resources then you can use count.index to create correspondences between the instances of one block and the instances of another, like this:

data "template_file" "hostname_servers" {
  count = "${var.count_proxy}"

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

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

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 = [
      # use count.index to match the template instance corresponding
      # to this compute instance instance.
      "${data.template_file.hostname_servers.*.rendered[count.index]}"
    ]
  }
}