Terraform Vsphere provider - getting the VM's UUID or MOID

1,700 views
Skip to first unread message

SurferL

unread,
Mar 8, 2018, 7:41:32 AM3/8/18
to Terraform
This is a repost of this issue: https://github.com/terraform-providers/terraform-provider-vsphere/issues/423

I'm slightly confused with the syntax of how to get the UUID or MOID of the newly generated VM, referenced in the documentation.


I thought it would be: ${data.vsphere_virtual_machine.id}, but data requires four parts...so trying with something along the lines of ${data.vsphere_virtual_machine.vm.id} failed as well.


Can you point me in the right direction for the syntax to get the UUID or MOID?


Thanks!


Idea behind requiring the MOID or UUID of the newly generated VM is to pass it into a remote-exec call, with which I can use in a script to have a unique name for the VM.

Thomas Michel

unread,
Mar 8, 2018, 7:46:35 AM3/8/18
to terrafo...@googlegroups.com
Hi, 

How did you call your data sphere_virtual_machine resource?

If you went like this :

data "vsphere_virtual_machine" "template" {
  name          = "test-vm-template"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
Then you will get the UUID with this variable: "${data.sphere_virtual_machine.template.id}".

The third part of the variable name is the name you gave to the "data" declaration.

Best regards,
Thomas

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/35543b5d-866b-4352-a837-221989a3b97b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

SurferL

unread,
Mar 8, 2018, 7:55:58 AM3/8/18
to Terraform
Hi,

Thanks for the response,

I am calling the data vsphere_virtual_machine resource as you do above.

However when running, I get the following error:

Error: resource 'vsphere_virtual_machine.vpshere_build_machine' provisioner remote-exec (#3): unknown resource 'data.vpshere_virtual_machine.template' referenced in variable data.vpshere_virtual_machine.template.id




Error: resource 'vsphere_virtual_machine.vpshere_build_machine' provisioner remote-exec (#3): unknown resource 'data.vpshere_virtual_machine.template' referenced in variable data.vpshere_virtual_machine.template.moid


Running vsphere provider version 1.1.0

Thomas Michel

unread,
Mar 8, 2018, 8:01:50 AM3/8/18
to terrafo...@googlegroups.com
Can you show me the whole code?

It seems that terraform can't find ${data.vsphere_virtual_machine.template}. That would means that you didn't write the "data" "sphere_virtual_machine" "template" {} block.

Regards,
Thomas

SurferL

unread,
Mar 8, 2018, 8:32:07 AM3/8/18
to Terraform
Sure:

provider "vsphere" {
version = "1.1.0"
vsphere_server = "${var.vsphere_vcenter}"
user = "${var.vsphere_user}"
password = "${var.vsphere_password}"
allow_unverified_ssl = true
}

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

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

data "vsphere_resource_pool" "pool" {
name = "${var.vsphere_cluster}/Resources"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_network" "network" {
name = "VM Network"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

data "vsphere_virtual_machine" "template" {
name = "${var.vm_template}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}


# Create virtual machine and provision deployment with gitlab runner
resource "vsphere_virtual_machine" "vpshere_build_machine" {
name = "${var.vm_name}"
folder = "${var.vm_folder}"
resource_pool_id = "${data.vsphere_resource_pool.pool.id}"

num_cpus = "${var.vm_cpu}"
memory = "${var.vm_ram}"
guest_id = "${data.vsphere_virtual_machine.template.guest_id}"
scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}"

network_interface {
adapter_type = "${data.vsphere_virtual_machine.template.network_interface_types[0]}"
}

disk {
name = "${var.vm_name}.vmdk"
size = "${data.vsphere_virtual_machine.template.disks.0.size}"
}

clone {
}

provisioner "remote-exec" {
connection {
type = "${var.connection_type}"
insecure = true
user = "${var.user}"
password = "${var.pwd}"
}

inline = [
"python ${var.working_dir}/script.py ${data.vpshere_virtual_machine.template.id} ${data.vpshere_virtual_machine.template.moid}"
]
}

Thomas Michel

unread,
Mar 8, 2018, 8:35:23 AM3/8/18
to terrafo...@googlegroups.com
You got a typo "${data.vpshere_" 

Didn't see it earlier 

Regards,
Thomas

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

SurferL

unread,
Mar 8, 2018, 9:04:15 AM3/8/18
to Terraform
Well this is embarrassing... Sorry about that! 

And thanks for pointing me in the right direction :) 

Is getting the moid the same? (getting this error, without spelling mistake this time!)

* Resource 'data.vsphere_virtual_machine.template' does not have attribute 'moid' for variable 'data.vsphere_virtual_machine.template.moid'

Thomas Michel

unread,
Mar 8, 2018, 9:07:59 AM3/8/18
to terrafo...@googlegroups.com
No worries, it happened to me so many times :D

About the moid, I think it will depends on the vsphere provider version, in the latest, there is no moid in the attribute list :

Regards,
Thomas

-- 
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
--- 
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

SurferL

unread,
Mar 8, 2018, 9:11:43 AM3/8/18
to Terraform

Thomas Michel

unread,
Mar 8, 2018, 9:17:10 AM3/8/18
to terrafo...@googlegroups.com
It's an attribute of the "resource" "vsphere_virtual_machine" configuration. When you instantiate a "data" configuration, this attribute does not exist. 
With a "data" configuration, you just fetch some informations about an existing resource in your infrastructure, but by no means you manage it with terraform.

Regards,
Thomas

SurferL

unread,
Mar 8, 2018, 9:21:58 AM3/8/18
to Terraform
Ah okay - thank you for your help :) 

Thomas Michel

unread,
Mar 8, 2018, 4:25:50 PM3/8/18
to terrafo...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages