Skip to content

Instantly share code, notes, and snippets.

@mike1237
Last active March 9, 2024 20:16
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mike1237/cce83a74f898b11c2cec911204568cf9 to your computer and use it in GitHub Desktop.
Save mike1237/cce83a74f898b11c2cec911204568cf9 to your computer and use it in GitHub Desktop.
variable "proxmox_host_node" {
type = string
}
variable "proxmox_password" {
type = string
sensitive = true
}
variable "proxmox_source_template" {
type = string
}
variable "proxmox_template_name" {
type = string
}
variable "proxmox_url" {
type = string
}
variable "proxmox_username" {
type = string
default = "root@pam"
}
source "proxmox-clone" "test-cloud-init" {
insecure_skip_tls_verify = true
full_clone = false
template_name = "${var.proxmox_template_name}"
clone_vm = "${var.proxmox_source_template}"
os = "l26"
cores = "1"
memory = "512"
scsi_controller = "virtio-scsi-pci"
ssh_username = "ubuntu"
qemu_agent = true
network_adapters {
bridge = "vmbr0"
model = "virtio"
}
node = "${var.proxmox_host_node}"
username = "${var.proxmox_username}"
password = "${var.proxmox_password}"
proxmox_url = "${var.proxmox_url}"
}
build {
sources = ["source.proxmox-clone.test-cloud-init"]
provisioner "shell" {
inline = ["sudo cloud-init clean"]
}
}
#!/bin/bash
# Must run on Proxmox VE 7 server
# Not sure how to handle a cluster - either run on each node or copy template after creating on one?
# e.g. $ ssh root@proxmox.server < proxmox-create-cloud-template.sh
SRC_IMG="https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-disk-kvm.img"
IMG_NAME="focal-server-cloudimg-amd64-disk-kvm.qcow2"
TEMPL_NAME="ubuntu2004-cloud"
VMID="9000"
MEM="512"
DISK_SIZE="32G"
DISK_STOR="zfs-mirror-pool"
NET_BRIDGE="vmbr0"
# Install libguesetfs-tools to modify cloud image
apt update
apt install -y libguestfs-tools
# Download kvm image and rename
# Ubuntu img is actually qcow2 format and Proxmox doesn't like wrong extensions
wget -O $IMG_NAME $SRC_IMG
# Ubuntu cloud img doesn't include qemu-guest-agent required for packer to get IP details from proxmox
# Add any additional packages you want installed in the template
virt-customize --install qemu-guest-agent -a $IMG_NAME
# Create cloud-init enabled Proxmox VM with DHCP addressing
qm create $VMID --name $TEMPL_NAME --memory $MEM --net0 virtio,bridge=$NET_BRIDGE
qm importdisk $VMID $IMG_NAME $DISK_STOR
qm set $VMID --scsihw virtio-scsi-pci --scsi0 $DISK_STOR:vm-$VMID-disk-0
qm set $VMID --ide2 $DISK_STOR:cloudinit
qm set $VMID --boot c --bootdisk scsi0
qm set $VMID --serial0 socket --vga serial0
qm set $VMID --ipconfig0 ip=dhcp
qm resize $VMID scsi0 $DISK_SIZE
# Convert to template
qm template $VMID
# Remove downloaded image
rm $IMG_NAME
# Next, use packer to clone this template and customize it!
# References
# https://gist.github.com/chriswayg/43fbea910e024cbe608d7dcb12cb8466
# https://whattheserver.com/proxmox-cloud-init-os-template-creation/
# https://norocketscience.at/deploy-proxmox-virtual-machines-using-cloud-init/
# https://pve.proxmox.com/wiki/Cloud-Init_Support
# https://blog.dustinrue.com/2020/05/going-deeper-with-proxmox-cloud-init/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment