Skip to content

How to import

Jonathan Senkerik edited this page Aug 26, 2020 · 2 revisions

How to import

Importing existing infra into terraform doesn't create the configuration files for you! (yet...) According to their web site, there is plans to have terraform build configuration files for you. In the mean time, you can however import to tfstate to help you build your configuration files manually...

Requirements

To import an existing esxi guest, it should be powered off and you need its vmid. To get a list of all vmids on your esxi host, ssh to your esxi host and run the following command.

vim-cmd vmsvc/getallvms

To import an existing resource pool, you need its poolID. To list all poolIDs, ssh to your esxi host and run.

grep -A1 name /etc/vmware/hostd/pools.xml

To import an existing virtual disk, you need its full path. Typically it will be in the format:

/vmfs/volumes/<DiskStore>/<Dir>/<Name>.vmdk

Terraform documentation

Steps to import.

  1. Create a "blank" main.tf
provider "esxi" {
  esxi_hostname  = var.esxi_hostname
  esxi_hostport  = var.esxi_hostport
  esxi_username  = var.esxi_username
  esxi_password  = var.esxi_password
}

resource "esxi_resource_pool" "MyPool" {
  # To be imported
}

resource "esxi_guest" "MyVM" {
  # To be imported
}

resource "esxi_virtual_disk" "vdisk1" {
  # To be imported
}
  1. Create a variables.tf and versions.tf file.
  • See appropriate version examples in this repo as a reference.
  1. Import each resource into tfstate
$ terraform init
$ terraform import esxi_resource_pool.MyPool pool9999
$ terraform import esxi_guest.MyVM 9999
$ terraform import esxi_virtual_disk.vdisk1 /vmfs/volumes/DS_001/MyStorage/MyVM.vmdk
  1. Show results
$ terraform show
  1. Take the output from terraform show to update main.tf. Do no include information that is not needed in your main.tf (for example, the id and ip_address).

  2. Validate your work. Terraform plan show not require any changes.

$ terraform plan

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.

Clone this wiki locally