server_init/packer/ubuntu-server.pkr.hcl

174 lines
3.9 KiB
HCL
Raw Normal View History

packer {
required_plugins {
qemu = {
version = "~> 1"
source = "github.com/hashicorp/qemu"
}
ansible = {
version = ">= 1.1.2"
source = "github.com/hashicorp/ansible"
}
}
}
# --- VM settings ---
variable "cpu" {
type = string
default = "2"
}
variable "ram" {
type = string
default = "4096"
}
variable "disk_size" {
type = string
default = "50000"
}
variable "headless" {
type = bool
default = true
}
# --- Ubuntu ISO ---
variable "iso_url" {
type = string
default = "https://releases.ubuntu.com/24.04/ubuntu-24.04.2-live-server-amd64.iso"
}
variable "iso_checksum" {
type = string
default = "file:https://releases.ubuntu.com/24.04/SHA256SUMS"
}
# --- OS user (created by autoinstall) ---
variable "ssh_username" {
type = string
default = "ubuntu"
}
variable "ssh_password" {
type = string
default = "ubuntu"
sensitive = true
}
# --- Server config (passed to Ansible) ---
variable "base_domain" {
type = string
}
variable "ssh_pubkey" {
type = string
default = ""
}
variable "juicefs_s3_endpoint" {
type = string
}
variable "juicefs_s3_bucket" {
type = string
}
variable "juicefs_s3_access_key" {
type = string
sensitive = true
}
variable "juicefs_s3_secret_key" {
type = string
sensitive = true
}
variable "juicefs_cache_size" {
type = string
default = "50G"
}
source "qemu" "ubuntu-server" {
accelerator = "kvm"
boot_command = [
"c<wait>",
"linux /casper/vmlinuz --- autoinstall ds=\"nocloud;s=http://{{ .HTTPIP }}:{{ .HTTPPort }}/\"<enter><wait>",
"initrd /casper/initrd<enter><wait>",
"boot<enter><wait>"
]
boot_wait = "10s"
disk_cache = "none"
disk_compression = true
disk_discard = "unmap"
disk_interface = "virtio"
disk_size = var.disk_size
format = "qcow2"
headless = var.headless
http_directory = "http"
iso_checksum = var.iso_checksum
iso_url = var.iso_url
net_device = "virtio-net"
output_directory = "output"
qemu_binary = "/usr/bin/qemu-system-x86_64"
qemuargs = [
["-m", "${var.ram}M"],
["-smp", var.cpu],
["-cpu", "host"]
]
shutdown_command = "echo '${var.ssh_password}' | sudo -S shutdown -P now"
ssh_password = var.ssh_password
ssh_username = var.ssh_username
ssh_handshake_attempts = 500
ssh_timeout = "45m"
ssh_wait_timeout = "45m"
}
build {
sources = ["source.qemu.ubuntu-server"]
provisioner "shell" {
execute_command = "echo '${var.ssh_password}' | sudo -S bash -c '{{ .Vars }} {{ .Path }}'"
inline = [
"apt-get update",
"apt-get install -y ansible-core python3-pip"
]
}
provisioner "file" {
source = "../playbook.yml"
destination = "/tmp/playbook.yml"
}
provisioner "file" {
source = "../resources"
destination = "/tmp/resources"
}
provisioner "shell" {
execute_command = "echo '${var.ssh_password}' | sudo -S bash -c '{{ .Vars }} {{ .Path }}'"
environment_vars = [
"ANSIBLE_FORCE_COLOR=1"
]
inline = [
"ansible-playbook -i localhost, -c local /tmp/playbook.yml -e 'base_domain=${var.base_domain} ssh_pubkey=\"${var.ssh_pubkey}\" juicefs_s3_endpoint=${var.juicefs_s3_endpoint} juicefs_s3_bucket=${var.juicefs_s3_bucket} juicefs_s3_access_key=${var.juicefs_s3_access_key} juicefs_s3_secret_key=${var.juicefs_s3_secret_key} juicefs_cache_size=${var.juicefs_cache_size}'"
]
}
provisioner "shell" {
execute_command = "echo '${var.ssh_password}' | sudo -S bash -c '{{ .Vars }} {{ .Path }}'"
inline = [
"rm -rf /tmp/playbook.yml /tmp/resources",
"apt-get clean",
"rm -rf /var/lib/apt/lists/*",
"cloud-init clean --logs",
"truncate -s 0 /etc/machine-id",
"rm -f /var/lib/dbus/machine-id",
"fstrim -av || true"
]
}
}