hashicorp / terraform-provider-vsphere

Terraform Provider for VMware vSphere
https://registry.terraform.io/providers/hashicorp/vsphere/
Mozilla Public License 2.0
619 stars 452 forks source link

Unpredictable disk type creation when cloning VM from the template with thin disk type #2178

Closed micropbl4 closed 6 months ago

micropbl4 commented 6 months ago

Community Guidelines

Terraform

1.6.0

Terraform Provider

2.5.1

VMware vSphere

8.0.2

Description

When attempting to create a disk with the "Thick Provision Lazy Zeroed" type, Terraform randomly creates another type. Upon rerunning the same code, I receive the following output:

Error: disk.0: cannot change the value of "thin_provisioned" - (old: true newValue: false)

This occurs within the module.provision_base_vms["hostname"].module.server.vsphere_virtual_machine.server resource in the .terraform/modules/provision_base_vms/server/server.tf file, at line 20, within the resource "vsphere_virtual_machine" "server" block.

In my case 1 of 3 VM was affected.

Affected Resources or Data Sources

"resource/vsphere_virtual_machine"

Terraform Configuration

server.tf

resource "vsphere_virtual_machine" "server" {
....
dynamic "disk" {
    for_each = var.disks
    iterator = each
    content {
      size             = each.value.size
      label            = "disk${each.key}"
      unit_number      = each.key
      attach           = each.value.attach
      path             = each.value.path
      datastore_id     = data.vsphere_datastore.datastore.id
      thin_provisioned = each.value.attach ? null : each.value.thin_provisioned
      eagerly_scrub    = each.value.attach ? null : each.value.eagerly_scrub
    }
clone {
    template_uuid = var.template_uuid
    customize {
      linux_options {
        host_name = var.host_name
        domain    = var.domain
      }

      network_interface {
        ipv4_address = var.ip_v4_address
        ipv4_netmask = var.ip_v4_netmask
      }

      ipv4_gateway    = var.ip_v4_gateway
      dns_server_list = var.dns_server_list
      timeout         = var.timeout
    }
  }
}

variables.tf

variable "disks" {
  type = list(
    object({
      attach           = optional(bool, false)
      size             = optional(number)
      path             = optional(string)
      thin_provisioned = optional(bool, false)
      eagerly_scrub    = optional(bool, false)
    })
  )

Debug Output

terraform plan 1st attempt:

disk { 
 attach            = false 
 controller_type   = "scsi" 
 datastore_id      = "datastore-33009" 
 device_address    = (known after apply) 
 disk_mode         = "persistent" 
 disk_sharing      = "sharingNone" 
 eagerly_scrub     = false 
 io_limit          = -1 
 io_reservation    = 0 
 io_share_count    = 0 
 io_share_level    = "normal" 
 keep_on_remove    = false 
 key               = 0 
 label             = "disk0" 
 path              = (known after apply) 
 size              = 40 
 storage_policy_id = (known after apply) 
 thin_provisioned  = false 
 unit_number       = 0 
 uuid              = (known after apply) 
 write_through     = false 
} 

2nd attempt:

Terraform will perform the following actions: 

  # module.provision_base_vms["hostname"].module.server.vsphere_virtual_machine.server will be updated in-place 

  ~ resource "vsphere_virtual_machine" "server" { 

        id  = "42143a93-de7a-b80c-ab1e-adf3404853e8" 

        name= "hostname" 
      ~ disk { 
          ~ thin_provisioned = true -> false 

} 

Panic Output

No response

Expected Behavior

When attempting to create a disk with the "Thick Provision Lazy Zeroed" type, Terraform inconsistently selects a different type. Upon rerunning the same code, there should be no changes in the output. However, the error message indicates that there is an attempt to change the "thin_provisioned" value from true to false, which should not occur.

Actual Behavior

Provider change infrastructure without actual changes on the code side

Steps to Reproduce

1) Create a few VMs with the following options:

  dynamic "disk" {
      .....
      thin_provisioned = false
      eagerly_scrub      = false

2) Execute terraform deploy without any changes

Environment Details

No response

Screenshots

No response

References

No response

github-actions[bot] commented 6 months ago

Hello, micropbl4! 🖐

Thank you for submitting an issue for this provider. The issue will now enter into the issue lifecycle.

If you want to contribute to this project, please review the contributing guidelines and information on submitting pull requests.

spacegospod commented 6 months ago

Looks like a duplicate of #2116

There are other duplicates out there too https://github.com/hashicorp/terraform-provider-vsphere/issues/2140

@micropbl4 don't worry I'll try to fix this Let's move the conversation to #2116

spacegospod commented 6 months ago

Duplicate of #2116

github-actions[bot] commented 5 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.