bpg / terraform-provider-proxmox

Terraform Provider for Proxmox
https://registry.terraform.io/providers/bpg/proxmox
Mozilla Public License 2.0
785 stars 128 forks source link

Add support for `proxmox_virtual_environment_file` data source #1396

Open VoidAndAny opened 3 months ago

VoidAndAny commented 3 months ago

Not a bug, just need help to use the provider, maybe it's more terraform usage than provider help, feel free to close this issue if it's not appropriate.

In my usage, I separate each VM in directory but I want to use the same iso/vztmpl.

For now, I have a common directory with terraform configuration to download iso/vztmpl with proxmox_virtual_environment_download_file resource:

resource "proxmox_virtual_environment_download_file" "ubuntu_2204_cloud_image" {
  content_type = "iso"
  datastore_id = var.image_datastorestore_id
  node_name    = var.node_name
  overwrite    = false
  url          = "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img"
}

Then in each VM directories I import them like this:

import {
  to = proxmox_virtual_environment_file.ubuntu_2204_vm_image
  id = "${var.node_name}/${var.image_datastorestore_id}:iso/jammy-server-cloudimg-amd64.img"
}
resource "proxmox_virtual_environment_file" "ubuntu_2204_vm_image" {
  content_type = "iso"
  datastore_id = var.image_datastorestore_id
  node_name    = var.node_name
  overwrite    = false
  lifecycle {
    prevent_destroy = true
  }
}

I had to configure lifecycle with prevent_destroy to preserve my image if I destroy VM (so I need to destroy with -targetoption)

Is there a better way (I tought use template but I prefer not) ? In my terraform comprehension this had to be done with data source, but there is no data source for file ?

Thanks for your help

bpg commented 3 months ago

Hi @VoidAndAny 👋🏼

Yeah, the file datasource would make it easier. But you probably don't need to define resource "proxmox_virtual_environment_file" "ubuntu_2204_vm_image" within each VM folder either. You can use "${var.image_datastorestore_id}:iso/jammy-server-cloudimg-amd64.img" directly as a disk.file_id value in the VM.

VoidAndAny commented 3 months ago

Whaouh, so simple You can close this issue if you want, thanks a lot

bpg commented 3 months ago

I'll leave it, would be a useful feature.

CRASH-Tech commented 2 months ago

+1, I need this feature