diff --git a/vsphere/resource_vsphere_virtual_machine.go b/vsphere/resource_vsphere_virtual_machine.go index 9464d1c58..50d1a09be 100644 --- a/vsphere/resource_vsphere_virtual_machine.go +++ b/vsphere/resource_vsphere_virtual_machine.go @@ -266,6 +266,31 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{ } } + // The host attribute of CreateVM_Task seems to be ignored in vCenter 6.7. + // Ensure that VMs are on the correct host and relocate if necessary. Do this + // near the end of the VM creation since it involves updating the + // ResourceData. + vprops, err := virtualmachine.Properties(vm) + if err != nil { + return err + } + if d.Get("host_system_id").(string) != vprops.Runtime.Host.Reference().Value { + hid := d.Get("host_system_id").(string) + err = resourceVSphereVirtualMachineRead(d, meta) + if err != nil { + return err + } + // Restore the old host_system_id so we can still tell if a relocation is + // necessary. + err = d.Set("host_system_id", hid) + if err != nil { + return err + } + if err = resourceVSphereVirtualMachineUpdateLocation(d, meta); err != nil { + return err + } + } + // Wait for a routable address if we have been set to wait for one err = virtualmachine.WaitForGuestNet( client,