Skip to content

Instantly share code, notes, and snippets.

@nquangit
Created December 8, 2025 13:01
Show Gist options
  • Select an option

  • Save nquangit/ebbe4104f8b6761c6f53aa39aa665e40 to your computer and use it in GitHub Desktop.

Select an option

Save nquangit/ebbe4104f8b6761c6f53aa39aa665e40 to your computer and use it in GitHub Desktop.
Proxmox Packer
packer {
required_plugins {
proxmox = {
version = ">= 1.1.3"
source = "github.com/hashicorp/proxmox"
}
}
}
# --- BIẾN (VARIABLES) ---
# Nên đưa vào file .pkrvars.hcl để bảo mật, ở đây để chung cho dễ hiểu
variable "proxmox_url" {
default = "https://10.10.100.1:8006/api2/json"
}
# Thay đổi: Dùng Token ID và Secret thay vì User/Pass
variable "proxmox_token_id" {
default = "<UPDATE>" # Định dạng: user@realm!token_name
}
variable "proxmox_token_secret" {
# Lời khuyên bảo mật: KHÔNG NÊN hardcode secret ở đây.
# Hãy truyền qua biến môi trường hoặc file .pkrvars.hcl
# Nhưng để test thì bạn điền secret vừa copy vào đây.
default = "<UPDATE>"
sensitive = true
}
variable "node" {
default = "homelab" # Tên node Proxmox
}
variable "vm_id" {
default = "9000" # ID của Template sẽ tạo
}
# --- SOURCE (Định nghĩa máy ảo sẽ build) ---
source "proxmox-iso" "ubuntu-server" {
# Kết nối Proxmox
proxmox_url = var.proxmox_url
# Cấu hình xác thực bằng Token
username = var.proxmox_token_id
token = var.proxmox_token_secret
node = var.node
# Bỏ qua xác thực SSL (nếu dùng IP local)
insecure_skip_tls_verify = true
# Cấu hình VM cơ bản
vm_id = var.vm_id
vm_name = "ubuntu-22.04-template"
cores = 2
memory = 2048
sockets = 1
# Cấu hình Mạng & Ổ cứng
network_adapters {
bridge = "vmbr10"
model = "virtio"
}
scsi_controller = "virtio-scsi-pci"
disks {
disk_size = "10G"
storage_pool = "local-lvm"
type = "scsi"
}
# File ISO (Packer sẽ tải về Proxmox nếu chưa có, hoặc bạn tự upload trước)
iso_url = "https://releases.ubuntu.com/22.04/ubuntu-22.04.5-live-server-amd64.iso"
iso_checksum = "file:https://releases.ubuntu.com/22.04/SHA256SUMS"
iso_storage_pool = "local"
unmount_iso = true
qemu_agent = true
# QUAN TRỌNG: Tạo ổ Cloud-Init cho Template đầu ra
# Để sau này Terraform có thể inject IP/User vào
cloud_init = true
cloud_init_storage_pool = "local-lvm"
# Cấu hình SSH để Packer login vào cài script sau khi OS lên
ssh_username = "packer"
ssh_password = "packerpassword"
ssh_timeout = "20m"
# --- MAGIC PART: Tự động gõ lệnh boot (Autoinstall) ---
# Dòng lệnh này thay thế việc bạn ngồi gõ phím lúc boot
boot_key_interval = "80ms"
boot_command = [
"<esc><wait>",
"c<wait>",
"linux /casper/vmlinuz --- autoinstall 'ds=nocloud-net;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/'",
"<enter><wait>",
"initrd /casper/initrd",
"<enter><wait>",
"boot<enter>"
]
# Packer dựng một Web Server tạm thời để phục vụ file user-data
http_directory = "http"
}
# --- BUILD (Các bước thực thi) ---
build {
sources = ["source.proxmox-iso.ubuntu-server"]
# Bước 1: Đợi máy cài xong và reboot, Packer login vào
# Bước 2: Cài đặt các gói cần thiết (Cloud-Init, Qemu-Agent)
provisioner "shell" {
inline = [
"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done",
"sudo apt-get update",
"sudo apt-get install -y cloud-init",
"sudo systemctl enable qemu-guest-agent",
"sudo systemctl start qemu-guest-agent"
]
}
# Bước 3: Dọn dẹp để Template sạch sẽ (xóa SSH keys cũ, machine-id)
provisioner "shell" {
inline = [
"sudo truncate -s 0 /etc/machine-id",
"sudo rm /var/lib/dbus/machine-id",
"sudo ln -s /etc/machine-id /var/lib/dbus/machine-id",
"sudo cloud-init clean",
"sudo rm -f /etc/ssh/ssh_host_*",
"sudo rm -f /etc/netplan/00-installer-config.yaml"
]
}
}
#cloud-config
autoinstall:
version: 1
locale: en_US.UTF-8
keyboard:
layout: en
variant: us
source:
id: ubuntu-server-minimal
identity:
hostname: ubuntu-template
password: "$6$D8mbcLMJ8voGDtdY$94I/rv9hbg1BLHFG0l7DpKUXNWw6.RPIpxFBfpwn8j1VBE1wEWzPzm5lD3q8wmOIuB/2.3JmHDto1ilFuHMnr/"
username: packer
ssh:
install-server: true
allow-pw: true
storage:
layout:
name: direct
user-data:
disable_root: false
packages:
- qemu-guest-agent
- cloud-init
- net-tools
# Update user priviledge
late-commands:
- "echo 'packer ALL=(ALL) NOPASSWD:ALL' > /target/etc/sudoers.d/packer"
- "chmod 440 /target/etc/sudoers.d/packer"
@nquangit
Copy link
Author

nquangit commented Dec 8, 2025

Download the Proxmox plugin for Packer at Github release

Unzip, then install the plugin

unzip packer-plugin-proxmox_v1.2.3_x5.0_linux_amd64.zip

packer plugins install --path /path/to/packer-plugin-proxmox_v1.2.3_x5.0_linux_amd64 github.com/hashicorp/proxmox

Set up the script with the structure as below

.
├── ubuntu-server.pkr.hcl  # File cấu hình chính
└── http                   # Thư mục chứa config cài đặt tự động
    ├── user-data          # Config trả lời câu hỏi cài đặt (Autoinstall)
    └── meta-data          # File rỗng

Note: Be sure that the VM can get the IP from the DHCP Server, and it can make an HTTP request to the Packer host.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment