Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support importing vsphere_virtual_machine resources #57

Closed
hashibot opened this issue Jun 13, 2017 · 20 comments
Closed
Labels
enhancement Type: Enhancement

Comments

@hashibot
Copy link

This issue was originally opened by @jaredledvina as hashicorp/terraform#9535. It was migrated here as part of the provider split. The original body of the issue is below.


Terraform Version

Terraform v0.7.7

Affected Resource(s)

  • vsphere_virtual_machine

Expected Behavior

The following command would successfully import the resource into the tfstate file.

terraform import vsphere_virtual_machine.name id

Actual Behavior

The command above fails with:

❯ terraform import vsphere_virtual_machine.name id
provider.vsphere.password
  The user password for vSphere API operations.

  Enter a value: <super-secret-password>

provider.vsphere.user
  The user name for vSphere API operations.

  Enter a value: <username>

provider.vsphere.vsphere_server
  The vSphere Server name for vSphere API operations.

  Enter a value: <fqdn>

vsphere_virtual_machine.name: Importing from ID "id"...
Error importing: 1 error(s) occurred:

* import vsphere_virtual_machine.name (id: id): resource vsphere_virtual_machine doesn't support import
@hashibot hashibot added the enhancement Type: Enhancement label Jun 13, 2017
@kalebwalton
Copy link

I also just ran into this as I was reorganizing my modules... I've reorganized them and now Terraform wants to destroy/recreate them all. I'd love if they could be imported.

@marekmrowca
Copy link

Hi,
are there any updates on this issues? I am using v0.10.8 and there is still the same error showing up when trying to import:
Error importing: 1 error(s) occurred:
... resource vsphere_virtual_machine doesn't support import

Best regards

@Ankan-Fraedom
Copy link

Ankan-Fraedom commented Dec 4, 2017

Would love to have this feature as we are trying to adopt terraform in our current organizatio, We wanna use this feature to compare the existing infra as a base line and then basically rewrite IaC as we add more resource. Any timeline around the availability of this? Thanks 👍

@Rybue
Copy link

Rybue commented Dec 6, 2017

Hi

I tried import with newest provider version - 1.0.3 and terraform version 0.11.1:

terraform import vsphere_virtual_machine.name id

Error importing: 1 error(s) occurred:

* vsphere_virtual_machine.name (import id: id): import vsphere_virtual_machine.name (id: id): error fetching virtual machine: please specify a datacenter

Is there a way to specify datacenter? I tried with -var 'datacenter=mydatacenter', but it doesn't work.

Best regards

@vancluever
Copy link
Contributor

Hey all,

This was added in the 1.0.0 release. For more information, please consult the section in the vsphere_virtual_machine resource documentation regarding importing, which can be found here.

Thanks!

@vancluever
Copy link
Contributor

PS @Rybue, you want to specify the full path to the VM, as per the documentation, which should include the datacenter as well.

@Rybue
Copy link

Rybue commented Jan 11, 2018

@vancluever thanks!

@tenchi4u
Copy link

Could one of you elaborate or provide a generic example on what you mean by "full path to the VM....which should include the datacenter as well"? I am new to Terraform and am having the same issue when trying to import an existing VM.

Thank you!

@belonesox
Copy link

belonesox commented Feb 19, 2018

Please, anyone, tell me HOW TO SPECIFY datacenter in "terraform import"?
I have read https://www.terraform.io/docs/providers/vsphere/r/virtual_machine.html but still cannot understand.

I have to create a lot of VMs

resource "vsphere_virtual_machine" "vm" {
  count            = 100
  name             = "vm-name-${count.index}"
...
}

but some of this VMs already created manually and I don want to recreate it.

How to import existing VMs from DC with name "dc-name" and id "dc-id"?

@vancluever
Copy link
Contributor

@tenchi4u and @belonesox - some help on importing:

The "full path" to your object in vSphere inventory is usually composed of a few things:

  • The path to the datacenter, including datacenter folders
  • A specific object classification. These correspond to a specific category you see in the vSphere client. host for Hosts and Clusters, vm for VMs and Templates, datastore for Storage and network for Networking.
  • The path to the object, including folders.

So, viewing the sample path as shown in https://www.terraform.io/docs/providers/vsphere/r/virtual_machine.html#importing, this is broken down as

/dc1/vm/srv1
 ^   ^  ^
 |   |  +----- Virtual machine "srv1"
 |   |
 |   +-------- "Virtual Machines" inventory category
 |   
 +------------ Datacenter "dc1"

Now, say you have both objects in folders - maybe your datacenter is in a folder named "foo" and your VM is in a folder named "blue". The path is then, along with breakdowns:

/foo/dc1/vm/blue/srv1
     ^   ^       ^
     |   |       +----- Virtual machine "srv1" in folder "blue"
     |   |
     |   +-------- "Virtual Machines" inventory category
     |
     +------------ Datacenter "dc1" in folder "foo"

Hope this helps!

@belonesox
Copy link

belonesox commented Feb 20, 2018

@vancluever, thank you very much.
But I still failed with it. I tried to import using "names" and "ids" of DC, or any combination...

terraform import vsphere_virtual_machine.vm /Office/vm/big-tms-00
terraform import vsphere_virtual_machine.vm /datacenter-2/vm/420273ab-321b-3632-5a27-71d8e6a541dd

and even

terraform import vsphere_virtual_machine.vm /datacenter-2/vm/big-tms-00
etc

but still get
"error fetching virtual machine: vm '***' not found"

All data - *.tf files, logs, terraform.tfstate with "big-tms-00" VM (created by TF, which I tried to import back) on https://kopy.io/M2tz9

Any ideas welcomed!

@belonesox
Copy link

Dear @vancluever, may be you can look at previous comment? Please!

@vancluever
Copy link
Contributor

@belonesox can you try to locate your vm using govc ls or govc find? Once you have the full path that way, give that a go.

You can get info on govc here.

Thanks!

@belonesox
Copy link

Thank you very much!
I find right names-paths-....

But I immediately got new puzzle!
When I tried to import
exising VMs, that are not already in tf.state:

>terraform import vsphere_virtual_machine.vm /Office/vm/VMs/big-tms-01                                                 
vsphere_virtual_machine.vm: Importing from ID "/Office/vm/VMs/big-tms-01"...                                                                                                 
vsphere_virtual_machine.vm: Import complete!                                                                                                                                 
  Imported vsphere_virtual_machine (ID: 4202d774-cf4a-3c60-9388-cc89b3982e77)                                                                                                
                                                                                                                                                                             
Error: vsphere_virtual_machine.vm (import id: /Office/vm/VMs/big-tms-01): Can't import vsphere_virtual_machine.vm, would collide with an existing resource.                  
                                                                                                                                                                             
Please remove or rename this resource before continuing.                                                                                                                     
                                                                                                                                                                             
                                                                                                                                                                             
                                                                                                                                                                             
>terraform import vsphere_virtual_machine.vm /Office/vm/VMs/big-tms-02                                                 
vsphere_virtual_machine.vm: Importing from ID "/Office/vm/VMs/big-tms-02"...                                                                                                 
vsphere_virtual_machine.vm: Import complete!                                                                                                                                 
  Imported vsphere_virtual_machine (ID: 4202da6e-aa51-c454-f757-5de0b27b1d47)                                                                                                
                                                                                                                                                                             
Error: vsphere_virtual_machine.vm (import id: /Office/vm/VMs/big-tms-02): Can't import vsphere_virtual_machine.vm, would collide with an existing resource.                  
                                                                                                                                                                             
Please remove or rename this resource before continuing.               

https://kopy.io/JmbhG

???

@belonesox
Copy link

Dear @vancluever, may be you can look at previous comment? Please!

@vancluever
Copy link
Contributor

@belonesox it sounds like you have an existing state for the specific resource address (vsphere_virtual_machine.vm) already. You will need to import the resources at a different resource address or start with a new state.

@ideologysec
Copy link

@belonesox I ran into this when I was using the Terraform vSphere examples - it's not the clearest of documentation, but the important bit is that the part after the period is the name of the vm.

For example, what you would want in your Terraform file, and in the import step, is to call those vms something like "vsphere_virtual_machine.big-tms-01" and "vsphere_virtual_machine.big-tms-02" (defined in your instance.tf file like this:

resource "vsphere_virtual_machine" "big-tms-01" {

@Koleon
Copy link

Koleon commented Aug 6, 2018

Hello guys,
I'm dealing with very similar issue. Could you help me out with this situation please?
My resource looks like this:

resource "vsphere_virtual_machine" "dns" {
  count            = "${terraform.workspace == "core" ? "${local.core_dns_count}" : 0}"
  name             = "${element(local.core_dns_vm_names, count.index)}"
  datastore_id     = "${data.vsphere_datastore.datastore.id}"
  num_cpus         = 1
  memory           = 1024
  folder           = "${local.core_folder}"
  depends_on       = ["vsphere_folder.core-folder"]
  guest_id         = "${data.vsphere_virtual_machine.centos.guest_id}"
  scsi_type        = "${data.vsphere_virtual_machine.centos.scsi_type}"
  resource_pool_id = "${data.vsphere_compute_cluster.lab_cluster.resource_pool_id}"
  network_interface {
    network_id     = "${data.vsphere_network.core-nic.id}"
  }
  disk {
    label            = "disk0"
    size             = "${data.vsphere_virtual_machine.centos.disks.0.size}"
    eagerly_scrub    = "${data.vsphere_virtual_machine.centos.disks.0.eagerly_scrub}"
    thin_provisioned = "${data.vsphere_virtual_machine.centos.disks.0.thin_provisioned}"
  }
  clone {
    template_uuid = "${data.vsphere_virtual_machine.centos.id}"
    customize {
      timeout = "${var.timeout}"
      linux_options {
        host_name = "${element(local.core_dns_vm_names, count.index)}"
        domain    = "domain.test"
      }
      network_interface {
        ipv4_address = "${element(local.core_dns_vm_ips, count.index)}"
        ipv4_netmask = 24
      }
      ipv4_gateway   = "${local.core_gtw}"
    }
  }
  provisioner "remote-exec" {
    script = "after-deploy/centos.sh"

    connection {
      password = "${var.root_password}"
    }
  }
}

This suppose to create 2 dns servers - dns1 and dns2. Unfortunately when I run
tf import vsphere_virtual_machine.dns /BM-LAB/vm/00_Core/00_dns1 and after that
tf import vsphere_virtual_machine.dns /BM-LAB/vm/00_Core/00_dns2 then I get:

vsphere_virtual_machine.dns: Importing from ID "/BM-LAB/vm/00_Core/00_dns2"...
vsphere_virtual_machine.dns: Import complete!
  Imported vsphere_virtual_machine (ID: 564da04d-5ed8-c688-20a7-32eb89257a8c)

Error: vsphere_virtual_machine.dns (import id: /BM-LAB/vm/00_Core/00_dns2): Can't import vsphere_virtual_machine.dns, would collide with an existing resource.

Please remove or rename this resource before continuing.

@vancluever could you tell me how to handle this situation and push all previously created items from $ terraform state list to Consul as the terraform backend please?

@Koleon
Copy link

Koleon commented Aug 20, 2018

@vancluever bump, any idea please?

@dannyk81
Copy link

@Koleon I had some trouble with this, but finally this worked:

/<DC NAME>/vm/<Folder(s) path to the VM>/<VM Name>
e.g.: /DC1/vm/PROD/LB/LB-01

if course, if the DC is in a folder you need to prefix the folder name.
e.g.: /DCs/DC1/vm/PROD/LB/LB-01

Once I figured out how to properly construct the full path it worked great.

Note: I believe it's case-sensitive 😉

@ghost ghost locked and limited conversation to collaborators Apr 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Type: Enhancement
Projects
None yet
Development

No branches or pull requests