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

Importing a vm forces replacement #1128

Open
lug-gh opened this issue Oct 17, 2024 · 1 comment
Open

Importing a vm forces replacement #1128

lug-gh opened this issue Oct 17, 2024 · 1 comment

Comments

@lug-gh
Copy link

lug-gh commented Oct 17, 2024

I have a VM that I created with the terraform provider. However, I no longer have the state file for this VM. I would now like to import it into my existing state file (which contains many other VMs) using terraform import.

Unfortunately, terraform does not want to import the existing VM as a state, but create a new one, as the VM is to be created from a clone with cloud-init. However, the VM has already been created with exactly the clone image with which it is now to be imported.

How can I tell the provider that this VM does not need to be replaced as it is already a clone of the “ubuntu2204-ci” image?

terraform code snippet

main.tf

module "example-vm" {
  source         = "./module"
  vm_name        = "example-vm"
  desc           = "to-do"
  target_node    = "node1"
  clone          = "ubuntu2204-ci"
  cores          = "4"
  memory         = "16384"
  [...shortened...]
}

module/module.tf

variable "vm_name" {}
variable "desc" {}
variable "target_node" {}
variable "clone" {}
variable "cores" {}
variable "memory" {}
[...shortened...]

resource "proxmox_vm_qemu" "vm" {
  name        = var.vm_name
  desc        = var.desc
  onboot      = true
  target_node = var.target_node
  agent       = 1
  clone       = var.clone
  qemu_os     = var.qemu_os
  cores       = var.cores
  sockets     = 1
  cpu         = "x86-64-v2-AES"
  memory      = var.memory
  boot        = "order=scsi0"
  tags        = "ha;terraform"
  scsihw      = "virtio-scsi-pci"
  pool        = var.pool
  bios        = var.bios
  [...shortened...]
}

image

@Tinyblargon
Copy link
Collaborator

@lug-gh you could try the following:

  • create the same vms with new names and ids.
  • create a copy of the state file.
  • tell terraform to remove the newly created vms.
  • replace the statefile with the copy you made earlier.
  • update the ids and names in the state file to your already existing vms.
  • create a backup of the state file.
  • terraform refresh & terraform plan.

You might still have to make minor changes to the state file.

There is probably a better way of doing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants