Today I’ve got a short post. It is especially dedicated to the new Terraform users, who use this tool for automation vSphere virtual machines. It is simply a matter of time till you face this problem. I mean, Terraform spec.identity.hostName error when you try to deploy your vSphere infrastructure. Why? Let me explain.
What is the problem?
At first I’ll show you the problem. After applying Terraform build, virtual machine is cloning and when a customization operation should start, you see something like this:
When does Terraform spec.identity.hostName error appear?
I met this problem once and it was caused by my habit and – shame on me – ignorance. Let’s take a closer look on the resource example from the Terraform Docs:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
resource "vsphere_virtual_machine" "web" { name = "terraform_web" vcpu = 2 memory = 4096 network_interface { label = "VM Network" } disk { template = "centos-7" } } |
This is version visible today, 19.02.2017. I want to make fix and add information from this post, but I do not know when (and whether) it will be merge. It is a huge project, so they have lots of work. 🙂
Anyway, the problem is in the name. It is a variable used in the process of cloning AND customization a Virtual Machine. If you look at the Terraform sources, you see in the “resource_vsphere_virtual_machine.go” that:
1 2 3 4 5 6 7 8 |
identity_options = &types.CustomizationLinuxPrep{ HostName: &types.CustomizationFixedName{ Name: strings.Split(vm.name, ".")[0], }, Domain: vm.domain, TimeZone: vm.timeZone, HwClockUTC: types.NewBool(true), } |
Everything above is the Go implementation of the VMware vSphere API. CustomizationLinuxPrep is a Data Object which has, among others, domain and hostName properties. Enough theory, now it’s time for practice. When I was looking for resolution, I found an issue #5448 made by Supernomad on the Terraform Github. He noticed that there is a VMware KB (2009820) which has a resolution for “\nspec.identity.userData.computerName” error. We can see there that:
To resolve this issue:
1) Reconfigure the parent virtual machine or template and ensure that:
- The name is less than 15 characters
- There are no underscores in the name
And of course it is the resolution for Terraform spec.identity.hostName error when you use vSphere provider. You should avoid any underscores in the name variable and be sure that the name is less than 15 characters. That’s all.
TL;DR version
If you get spec.identity.hostName error while using the vSphere provider, ensure yourself that:
- The “name” variable in the resource section does not contain underscores.
- The “name” variable in the resource section is less than 15 characters.
I hope you will find this post helpful and have fun with Terraform and vSphere!
Bibliography:
Thanks man for this post! You’ve saved me a loads of googling 🙂
Nice to hear it, you’re welcome!
Thanks for this
You’re welcome! 🙂
Thanks, gotta love Google for pointing me right to this page.
I’m glad you found my blog helpful. 🙂