65 lines
2.2 KiB
HCL
65 lines
2.2 KiB
HCL
variable "proxmox_host" { type = string }
|
|
variable "proxmox_user" { type = string }
|
|
variable "cloud_image_url" { type = string }
|
|
variable "image_path" { type = string }
|
|
variable "template_id" {
|
|
type = number
|
|
default = 8000
|
|
}
|
|
variable "template_name" { type = string }
|
|
variable "snippet_name" { type = string }
|
|
variable "user_passwd" { type = string } # hashed pw
|
|
|
|
# SSH connection to Proxmox
|
|
resource "null_resource" "create_template_remote" {
|
|
connection {
|
|
type = "ssh"
|
|
user = var.proxmox_user
|
|
host = var.proxmox_host
|
|
private_key = file("~/.ssh/id_ed25519")
|
|
}
|
|
|
|
# Commands executed *on Proxmox host*
|
|
provisioner "remote-exec" {
|
|
inline = [
|
|
"set -e",
|
|
# download the cloud image if missing
|
|
"if [ ! -f ${var.image_path} ]; then wget -O ${var.image_path} ${var.cloud_image_url}; fi",
|
|
# create template only if not existing
|
|
"if ! qm status ${var.template_id} >/dev/null 2>&1; then",
|
|
" echo 'Creating cloud-init template...';",
|
|
" qm create ${var.template_id} --name ${var.template_name} --memory 8192 --cores 8 --net0 virtio,bridge=vmbr0;",
|
|
" qm importdisk ${var.template_id} ${var.image_path} local-lvm;",
|
|
" qm set ${var.template_id} --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-${var.template_id}-disk-0;",
|
|
" qm set ${var.template_id} --ide2 local-lvm:cloudinit;",
|
|
" qm set ${var.template_id} --boot c --bootdisk scsi0;",
|
|
" qm set ${var.template_id} --serial0 socket --vga serial0;",
|
|
" qm template ${var.template_id};",
|
|
"else",
|
|
" echo 'Template ${var.template_id} already exists — skipping.';",
|
|
"fi"
|
|
]
|
|
}
|
|
}
|
|
|
|
resource "null_resource" "upload_cloud_init" {
|
|
connection {
|
|
type = "ssh"
|
|
host = var.proxmox_host
|
|
user = var.proxmox_user
|
|
private_key = file("~/.ssh/id_ed25519")
|
|
}
|
|
|
|
provisioner "remote-exec" {
|
|
inline = ["mkdir -p /var/lib/vz/snippets"]
|
|
}
|
|
|
|
provisioner "file" {
|
|
destination = "/var/lib/vz/snippets/${var.snippet_name}"
|
|
content = templatefile("${path.module}/cloud_init.yaml", { authorized_ssh_key = file("~/.ssh/id_ed25519.pub"), passwd = var.user_passwd })
|
|
}
|
|
|
|
triggers = {
|
|
file_hash = filesha256("${path.module}/cloud_init.yaml")
|
|
}
|
|
}
|