Telmate / terraform-provider-proxmox

Terraform provider plugin for proxmox
MIT License
2k stars 502 forks source link

Trying to use proxmox_cloud_init_disk #1020

Open Ikariusrb opened 1 month ago

Ikariusrb commented 1 month ago

I'm currently fighting with 3.0.1-rc2 and trying to convince it to use a custom-generated proxmox_cloud_init_disk. I'm able to get it to generate a cloud init iso image on the proxmox node without too much difficulty:

resource "proxmox_cloud_init_disk" "ci" {
  name     = local.vm_name
  pve_node = local.pm_node
  storage  = local.pm_iso_storage

  meta_data = yamlencode({
    instance_id = sha1(local.vm_name)
  })
  user_data = yamlencode({
    hostname            = local.vm_name
    fqdn                = "${local.vm_name}.${local.local_domain}"
    manage_etc_hosts    = true
    users               = ["default"]
    user                = local.cloudinit_user
    ssh_authorized_keys = [local.ssh_key]
    package_upgrade     = true
    chpasswd            = {
        expire = false
    }
  })
  network_config = yamlencode({
    version = 1
    config = [{
      type = "physical"
      name = "eth0"
      subnets = [{
        type    = "static"
        address = "192.168.5.100/24"
        gateway = local.local_ip_gateway
      }]
      },
      {
        type      = "nameserver"
        addresses = [local.local_dns_server_ip]
        search    = [local.local_domain]
      }
    ]
  })
  vendor_data = ""
}

I can verify it's successfully generating an ISO on the proxmox node with this resource, and I've temp mounted that .iso and validated its contents look correct. I've got a couple of templates; one with a cloudinit device as a cdrom on ide2, and one without.

The trouble comes in when I attempt to actually use that iso for cloudinit. I can get ide2 as a cdrom with the iso attached, but I can't seem to get it to recognize it as a cloudinit source. I suspect I'm missing something in my disks config. Here's where I'm at currently for the VM disk config:

  disks {
    scsi {
      scsi0 {
        disk {
          storage = local.pm_storage
          size    = local.master_node_disk_gb
        }
      }
    }
    ide {
      ide2 {
        cdrom {
          iso = "${local.pm_iso_storage}:${proxmox_cloud_init_disk.ci.id}"
        }
        # cloudinit {
        #     storage = local.pm_iso_storage
        #     iso = "${local.pm_iso_storage}:${proxmox_cloud_init_disk.ci.id}"
        # }
      }
    }
  }

I haven't found a combination of config that lets me specify a cloudinit drive AND the ISO image. The only parameter the cloudinit block seems to support is the storage, which only tells it which storage pool to use.

I've followed as much as I could in the docs and read a number of the bugs, but most of the examples are for using the proxmox built-in cloud-init config, and I want to add some things which aren't supported through proxmox' cloudinit config (packages, runcmd).

maksimsamt commented 1 week ago

@Ikariusrb,

maksimsamt commented 1 week ago

Also, I think this is the wrong approach for user_data:

...
  user_data = yamlencode({
    hostname            = local.vm_name
    fqdn                = "${local.vm_name}.${local.local_domain}"
    manage_etc_hosts    = true
    users               = ["default"]
    user                = local.cloudinit_user
    ssh_authorized_keys = [local.ssh_key]
    package_upgrade     = true
    chpasswd            = {
        expire = false
    }
  })
...

You should use here Heredoc Strings, like:

...
  user_data = <<-EOT
  #cloud-config
  users:
    - default
  ssh_authorized_keys:
    - ssh-rsa AAAAB3N......
  EOT
...

Because user_data must begin with #cloud-config. https://cloudinit.readthedocs.io/en/latest/explanation/format.html