0
votes

We have provisioned a VM in vSphere using terraform and it returns the VM details in the state file. But it does not return the VM Instance UUID that is used to uniquely identify the VM within the vCenter.

It only gives the VM uuid which cannot be used to make vSphere API calls. The vSphere API expects the instance UUID to identify a VM.

Any help/pointers would be appreciated.

Below is the main terraform file to provision a VM in vSphere:

###############################
#Description: to provide linux vm using Vsphere
#Owner: Iautomate
#Created On: 29th Oct, 2020
#Output: Provides a Linux Vm using a template
####################################
#VMWARE PROVIDER
provider "vsphere" {
  version        = "1.12"
  user           = var.vsphere_user
  password       = var.vsphere_password
  vsphere_server = var.vsphere_server

  # If you have a self-signed cert
  allow_unverified_ssl = true
}

data "vsphere_datacenter" "dc" {
  name = var.datacenter_name
}

data "vsphere_datastore" "datastore" {
  name          = var.datastore_name
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_resource_pool" "pool" {
  name          = var.resource_pool
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_network" "network" {
  name          = var.network_name
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_virtual_machine" "template" {
  name          = var.template_name
  datacenter_id = data.vsphere_datacenter.dc.id
}
resource "vsphere_virtual_machine" "vm" {
  name             = var.vm_name
  resource_pool_id = data.vsphere_resource_pool.pool.id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = var.cpus
  memory   = var.mem
  guest_id = var.guest

  network_interface {
    network_id = data.vsphere_network.network.id
  }

  disk {
    label = var.disk_label
    size  = var.disk_size
  }

  clone {
    template_uuid = data.vsphere_virtual_machine.template.id

    customize {
      linux_options {
        host_name = var.vm_name
        domain    = var.domain_name
      }
      network_interface {
        ipv4_address = var.ipaddress
        ipv4_netmask = var.netmask
      }

      ipv4_gateway    = var.gateway
    }
  }
}

=================================================================

It takes in another variable file that supplies all the parameters.

VM is getting provisioned but the unique id- Instance UUID, used to identify a VM in vCenter is not returned by terraform. Is there any way that I can get this Instance UUID as response in terraform?

1
Please can you share your Terraform code, ideally as a minimal reproducible example and then explain a little more depth what you're trying to achieve and mention how what you have doesn't quite achieve that?ydaetskcoR

1 Answers

0
votes

You can use the concept of output blocks to return some information about the VM that was created.

The attributes being returned by the vsphere_virtual_machine resource block can be found here: attribute reference

Information about output blocks can be found here: output command

Couple examples which could be added to your code to produce the UUID and MoRef:

output "vm_uuid" {
  value = vsphere_virtual_machine.vm.uuid
}

output "vm_moref" {
  value = vsphere_virtual_machine.vm.moid
}