danitso / terraform-provider-proxmox

Terraform Provider for Proxmox
Mozilla Public License 2.0
142 stars 32 forks source link

[BUG] Initialization always recrete the VM #30

Open itek09 opened 4 years ago

itek09 commented 4 years ago

Describe the bug If you create a vm resource with initialization block, every terraform apply will recreate the virtual machine

To Reproduce Steps to reproduce the behavior:

  1. Create a simple vm with initialization and execute terraform apply
  2. Without destroy the resources, reexecute terraform apply
  3. You will see that the vm resource should be recreated

Expected behavior If something in the recreation block changes, we should create the VM.

Initialization info first apply initialization.#: "" => "1" \ initialization.0.datastore_id: "" => "shared" \ initialization.0.user_account.#: "" => "1" \ initialization.0.user_account.0.keys.#: "" => "1" \ initialization.0.user_account.0.keys.0: "" => "my-key\n"

Initialization info second apply initialization.#: "1" => "1" \ initialization.0.datastore_id: "shared" => "shared" \ initialization.0.user_account.#: "1" => "1" \ initialization.0.user_account.0.keys.#: "1" => "1" \ initialization.0.user_account.0.keys.0: "my-key" => "my-key\n" (forces new resource) \ initialization.0.user_account.0.username: " " => "" (forces new resource)

As you can see, you are adding a '\n' to the key and proxmox give you " " as username (instead of "" that you apply).

Thank you so much

itek09 commented 4 years ago

Finally I did a workaround:

For the User key, I saw in your documentation the usage of the function "trimspace", and this solve this issue.

For the username, I forced the username to a fixed one.

Other issues related to related to recreate: -If you do not define the vm_id parameter, the resource will recreate the machine. Finally I decided to force the id per machine. -If you are initialising a network adapter, the network device id always change. It will not recreate the machine only modify, but the modification require VM reboot. In my case (latest version of Proxmox), the /reboot endpoint is not implemented, so I changed this function in the provider code to use /shutdown and /start endpoints.

From my side, we can close the issue, but I will let you do it, maybe you want to check or improve these details ;)

danitso-dp commented 4 years ago

@itek09 I'll let it remain open so I can check up on the issues you experienced. We can probably refine it a bit more :)

diijkstra commented 4 years ago

I have similar problem with initialization block. I'm using using custom packer image to create a template VM. I don't want to specify anything in initialization and it seems to be consistent with documentation - marked as being optional. Template is created properly. Unfortunately on subsequent run, terraform will try to replace (destroy/create) template in order to remove empty initialization block.

The workaround in my case is to use lifecycle management and ignore changes in initialization. Unfortunately specifying empty block, adds implicit datastore_id to the block and plan.

Side note, it would be useful to mark the reason for replacement in such case, it took me some time to discover that initialization is a culprit and not a mac-address or any other usual small changes. There were no meaningfull changes in state files after recreating such template.

For the User key, I saw in your documentation the usage of the function "trimspace", and this solve this issue.

Thank you for that! I've had the same issue with multiline description, trimspace saved the day.

danitso-dp commented 3 years ago

@itek09 do you still experience this issue using the latest version?