terraform-provider-proxmox: `terraform import` for LXC is missing some attributes which forces replacement

Description

I’m trying to migrate my existing guests to get them managed by Terraform. However after importing, terraform tries to replace the container. Obviously this is not an “acceptable” (for the lack of a better word) solution since that would mean I would lose my data and/or would have to reinstall all my services. I’ve found that this is because some attributes don’t get imported. See below for details.

Steps taken

  1. Clone an existing container (to not impact my “real” containers)
  2. terraform import proxmox_lxc.terraform-test <host>lxc/102 was (seemingly) successful
  3. Take note of terraform.tfstate at this stage for later comparison
  4. Copy output of terrform state show proxmox_lxc.terraform-test to the resource it was imported to
  5. Adjust to fit requirements (add storage, remove id, unused and volume, add ostemplate)
  6. Execute terraform apply (let it recreate the container)

Investigation

I then compared the two terraform.tfstate versions (post import and post apply; I’ve also pasted the relevant sections at the end) and found that they differ in the following attributes:

  • cmode ("" instead of "tty")
  • ostemplate (null instead of template path)
  • storage ("" instead of storage path)
  • start (null instead of false) Of those cmode and start seem irrelevant for my issue - however ostemplate and storage seemed interesting.

Workaround/Solution

I then started over (deleting terraform.tfstate) and executed steps 0-4 from above again. After that I manually set ostemplate and storage in terraform.tfstate. Et voilà! Now terraform plan no longer wants to replace the container. It does still want to set start = false and cmode = "tty", but I assume those are defaults when these properties are unset - fine to me.

Conclusion

The issue that forces the replacement of the container seems to be that ostemplate and storage don’t get picked up. Once those properties are defined the container does no longer nees to be replaced. HOWEVER I think that the issue is (slightly) bigger than only those two properties since e.g. network and vmid do not get imported either (and potentially more - that’s just what I’ve noticed).

How to fix

I think one should take a look at the logic that handles importing and adjust it so it properly adds all properties. I don’t know if this is possible for all of them though since I don’t know what the API provides.

Attachments

post import
{
  "schema_version": 0,
  "attributes": {
    "arch": "amd64",
    "bwlimit": 0,
    "clone": null,
    "clone_storage": null,
    "cmode": "",
    "console": true,
    "cores": 8,
    "cpulimit": 0,
    "cpuunits": 1024,
    "description": "",
    "features": [],
    "force": false,
    "full": null,
    "hagroup": "",
    "hastate": "",
    "hookscript": "",
    "hostname": "terraform-test",
    "id": "proxmox/lxc/102",
    "ignore_unpack_errors": false,
    "lock": "",
    "memory": 1024,
    "mountpoint": [],
    "nameserver": "",
    "network": [],
    "onboot": true,
    "ostemplate": null,
    "ostype": "ubuntu",
    "password": null,
    "pool": null,
    "protection": false,
    "restore": false,
    "rootfs": [
      {
        "acl": false,
        "quota": false,
        "replicate": false,
        "ro": false,
        "shared": false,
        "size": "5G",
        "storage": "",
        "volume": "guestdata-fast:subvol-102-disk-0"
      }
    ],
    "searchdomain": "",
    "ssh_public_keys": null,
    "start": null,
    "startup": "",
    "swap": 512,
    "tags": "",
    "target_node": "proxmox",
    "template": false,
    "timeouts": null,
    "tty": 2,
    "unique": false,
    "unprivileged": false,
    "unused": [],
    "vmid": null
  }
}
post apply
{
  "schema_version": 0,
  "attributes": {
    "arch": "amd64",
    "bwlimit": 0,
    "clone": null,
    "clone_storage": null,
    "cmode": "tty",
    "console": true,
    "cores": 8,
    "cpulimit": 0,
    "cpuunits": 1024,
    "description": "",
    "features": [],
    "force": false,
    "full": null,
    "hagroup": "",
    "hastate": "",
    "hookscript": "",
    "hostname": "terraform-test",
    "id": "proxmox/lxc/102",
    "ignore_unpack_errors": false,
    "lock": "",
    "memory": 1024,
    "mountpoint": [],
    "nameserver": "",
    "network": [],
    "onboot": true,
    "ostemplate": "hostdata-slow:vztmpl/ubuntu-22.04-standard_22.04-1_amd64.tar.zst",
    "ostype": "ubuntu",
    "password": null,
    "pool": null,
    "protection": false,
    "restore": false,
    "rootfs": [
      {
        "acl": false,
        "quota": false,
        "replicate": false,
        "ro": false,
        "shared": false,
        "size": "5G",
        "storage": "guestdata-fast",
        "volume": "guestdata-fast:subvol-102-disk-0"
      }
    ],
    "searchdomain": "",
    "ssh_public_keys": null,
    "start": false,
    "startup": "",
    "swap": 512,
    "tags": "",
    "target_node": "proxmox",
    "template": false,
    "timeouts": null,
    "tty": 2,
    "unique": false,
    "unprivileged": false,
    "unused": [],
    "vmid": null
  }
}

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 8
  • Comments: 23 (7 by maintainers)

Most upvoted comments

The LXC related issues will be addressed once the Qemu issues are fixed. There are plans for a LXC rework in the upstream library we use.

Still exists in 3.0.1-rc6

I’ve never imported existing infrastructure, but I wanted to help test this. Just wanted to jot down some notes here for when I come back to this (or so someone else can pick it up if so inclined).

issue_738.tf:

resource "proxmox_vm_qemu" "terraform-test" {
  # (resource arguments)
}

provider.tf:

terraform {
  required_providers {
    proxmox = {
      source = "Telmate/proxmox"
      version = "=3.0.1-rc1"
    }
  }
  required_version = ">= 0.14"
}

Example commands to pull in a VM (instead of a container):

terraform init
terraform import proxmox_vm_qemu.terraform-test larry/vm_qemu/107
terraform state show proxmox_vm_qemu.terraform-test > issue_738.tf

And then to demonstrate this issue:

vim issue_738.tf
terraform apply

Core issue is that "vmid" is set to null. It should be the VM ID number (107 in the example above). When I come back to this, I’ll look for the code which is responsible for filling in this value and see if we can just do the same thing that @reiserfs is doing here. It seems like this might be a pretty straight-forward change once the appropriate code is located.

Latest version still is the one the issue was created with. Details are above. Still relevant.

Still exists in 3.0.1-rc9 ☹️