vatesfr / terraform-provider-xenorchestra

Xen Orchestra provider for Terraform
MIT License
150 stars 32 forks source link

Set hostname when creating VMs #87

Closed fabiocruzcoelho closed 3 years ago

fabiocruzcoelho commented 3 years ago

Hi,

I am testing the provider to manage my VMS, it already creates and updates my VMS, however it does not define the hostname according to the name of the VM.

image

The VM created from the template has the hostname defined in the template.

Is there an option for the hostname to be changed when creating the VM? I have cases where I need to create several VMs at the same time, and I need to access VM by VM to change the hostname or use a configuration management tool for that.

I tried via cloud-init but it doesn't accept the count object to count the VMs.

Could you give me any suggestions on how I can resolve this config in the provider?

Thanks

ddelnano commented 3 years ago

@fabiocruzcoelho the name_label of the VM is just metadata. It won't change anything related to the hostname.

You will need to do this with cloud-init but I'm not sure what you mean by "but it doesn't accept the count object to count the VMs.". Could you give me an example?

fabiocruzcoelho commented 3 years ago

@fabiocruzcoelho the name_label of the VM is just metadata. It won't change anything related to the hostname.

You will need to do this with cloud-init but I'm not sure what you mean by "but it doesn't accept the count object to count the VMs.". Could you give me an example?

Follow my test cloud-init.

resource "xenorchestra_cloud_config" "vm" {
  name     = "bootstrap"
  template = <<EOF
#cloud-config
hostname: "${local.hostname}-${format("%02d", count.index + 1)}"
manage_etc_hosts: true
ssh_authorized_keys:
  - ssh-rsa myky
packages:
  - htop
EOF
}

resource "xenorchestra_vm" "vm" {
  provider         = xenorchestra
  count            = var.vms_count
  name_label       = "${local.hostname}-${format("%02d", count.index + 1)}"
  name_description = var.vm_description
  memory_max       = var.memory_max
  cpus             = var.num_cpus
  cloud_config     = xenorchestra_cloud_config.vm.template
  template         = data.xenorchestra_template.template.id
  network {
    network_id = data.xenorchestra_network.net.id
  }

  disk {
    sr_id      = data.xenorchestra_sr.local_storage.id
    name_label = "${local.hostname}-${format("%02d", count.index + 1)}"
    // name_label = var.hostname
    size = var.disk_size
  }
}

when I execute terraform plan returns the error, below I understood that count can only be used on data, resources.

Error: Reference to "count" in non-counted context

  on ../../main.tf line 39, in resource "xenorchestra_cloud_config" "vm":
  39: hostname: "${local.hostname}-${format("%02d", count.index + 1)}"

The "count" object can only be used in "module", "resource", and "data"
blocks, and only when the "count" argument is set.

would you like to pass this count using a cloud-init template with tpl?

Thank

ddelnano commented 3 years ago

You need to you count on the xenorchestra_cloud_config resource as well. Also your xenorchestra_cloud_config resource will need to parameterize the name otherwise the additional cloud configs will overwrite each other.

I'm going to close this since this is not a bug or feature request for the provider