josenk / terraform-provider-esxi

Terraform-provider-esxi plugin
GNU General Public License v3.0
543 stars 154 forks source link

Some guest resources are planned to be recreated because of guestinfo #193

Closed zeerayne closed 1 year ago

zeerayne commented 1 year ago

Describe the bug

While working with windows guests some guests are planned to be recreated with no actual requirements to do it.

  # esxi_guest.example_vm["11"] must be replaced
-/+ resource "esxi_guest" "###" {
      - guestinfo              = {
          - "driver.pvscsi.version"  = "1.3.25.0"
          - "driver.vmci.version"    = "9.8.18.0"
          - "driver.vmxnet3.version" = "1.9.9.0"
          - "driver.vsock.version"   = "9.8.19.0"
          - "driver.wddm.version"    = "9.17.01.0002"
          - "vmtools.buildNumber"    = "20104755"
          - "vmtools.description"    = "VMware Tools 12.0.6 build 20104755"
          - "vmtools.versionNumber"  = "12294"
          - "vmtools.versionString"  = "12.0.6"
        } -> null # forces replacement
      ~ id                     = "###" -> (known after apply)
      ~ ip_address             = "192.168.###.###" -> (known after apply)
      + ovf_properties_timer   = (known after apply)
      ~ power                  = "on" -> (known after apply)
        # (13 unchanged attributes hidden)

      ~ network_interfaces {
          + mac_address     = (known after apply)
            # (2 unchanged attributes hidden)
        }
    }

I guess it is windows-specific issuse, but not all windows guests are affected. Even in provided resource group only 2 of 16 guests are affected.

To Reproduce

  1. terraform apply
  2. Install guests OS (in this example win10 32bit was used)
  3. Install vmtools
  4. terraform plan

Expected behavior

Clearly vmtools version information which is contained in guestinfo should not cause resource destroy/add. I assume it should be ignored silently.

Terraform files

resource "esxi_guest" "example_vm" {
  for_each       = { for idx, val in var.example_vms: idx => val }
  guest_name     = each.value.name
  boot_disk_type = var.virtual_disk_type
  boot_disk_size = 30
  guestos        = "windows9"
  boot_firmware  = "bios"
  disk_store     = var.disk_store
  memsize        = 1024
  numvcpus       = 2
  virthwver      = var.virthwver
  network_interfaces {
    virtual_network = esxi_portgroup.default.name
    nic_type        = var.nic_type
  }
  guest_startup_timeout  = var.guest_startup_timeout
  guest_shutdown_timeout = var.guest_shutdown_timeout
  notes                  = each.value.notes
}
variable "virtual_disk_type" {
  type    = string
  default = "zeroedthick"
}

variable "virthwver" {
  type    = number
  default = 15
}

variable "nic_type" {
  type    = string
  default = "vmxnet3"
}

variable "guest_startup_timeout" {
  type    = number
  default = 120
}

variable "guest_shutdown_timeout" {
  type    = number
  default = 120
}

variable "example_vms" {
  type = list(any)
  default = [
    { name = "e01", notes = "" },
    { name = "e02", notes = "" },
    { name = "e05", notes = "" },
    { name = "e06", notes = "" },
    { name = "e07", notes = "" },
    { name = "e08", notes = "" },
    { name = "e10", notes = "" },
    { name = "e11", notes = "" },
    { name = "e13", notes = "" },
    { name = "e15", notes = "" },
    { name = "e21", notes = "" },
    { name = "e22", notes = "" },
    { name = "e23", notes = "" },
    { name = "e25", notes = "" },
    { name = "e26", notes = "" },
    { name = "e27", notes = "" },
    { name = "e28", notes = "" },
  ]
}

Desktop

josenk commented 1 year ago

Guestinfo holds the cloudinit information. Sorry, but I don't agree. If you change cloudinit (ie, guestinfo), then the VM needs to be replaced.

https://github.com/josenk/terraform-provider-esxi/blob/master/examples/05%20CloudInit%20and%20Templates/main.tf#L51

If you do not want your VM's replaced when guestinfo is changed by an external manager (you, or some other tool), then you can use the built-in terraform way of ignoring those changes. (lifecycle mangement, ignore_changes)

https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle#ignore_changes

zeerayne commented 1 year ago

That's true, but for windows guests guestinfo looks like filled automatically with some useless data after installing vmware-tools in some cases, and after that contains information which wasn't declared in guest vm resource.

josenk commented 1 year ago

terraform doesn't care who changed the information. That fact that it was changed means that it needs to be corrected. In this case userdata is used for cloudinit, so the vm must be replaced.

There is a simple work-around.

Is it possible for you to confirm with certainly which tool created this problem?

zeerayne commented 1 year ago

Make a lot of tries to stably reproduce this, and this problem stop appearing after installing 2022-10-06 update for ESXi 6.7 and using latest vmware tools version.