Skip to content

Instantly share code, notes, and snippets.

@hegerdes
Last active November 1, 2024 11:56
Show Gist options
  • Save hegerdes/deb361b1383c76e9dabbe030c607ac51 to your computer and use it in GitHub Desktop.
Save hegerdes/deb361b1383c76e9dabbe030c607ac51 to your computer and use it in GitHub Desktop.
Hetzner Packer
#cloud-config
ssh_pwauth: false
disable_root_opts: no-port-forwarding,no-agent-forwarding,no-X11-forwarding
# Install base packages
package_update: true
package_upgrade: true
packages:
- gnupg
- curl
- jq
- unzip
- apparmor
- aptitude
- lsb-release
- ca-certificates
- apt-transport-https
- unattended-upgrades
- apparmor-profiles-extra
- logrotate
- wget
# custom-img-v1.pkr.hcl
source "hcloud" "base-amd64" {
image = "debian-12"
location = "nbg1"
server_type = "cx11"
ssh_keys = []
user_data = ""
ssh_username = "root"
snapshot_name = "custom-img"
snapshot_labels = {
base = "debian-12",
version = "v1.0.0",
name = "custom-img"
}
}
build {
sources = ["source.hcloud.base-amd64"]
provisioner "shell" {
inline = [
"apt-get update",
"apt-get install -y wget fail2ban cowsay",
"/usr/games/cowsay 'Hi Hetzner Cloud' > /etc/motd",
]
env = {
BUILDER = "packer"
}
}
}
# custom-img-v2.pkr.hcl
variable "base_image" {
type = string
default = "debian-12"
}
variable "output_name" {
type = string
default = "snapshot"
}
variable "version" {
type = string
default = "v1.0.0"
}
variable "user_data_path" {
type = string
default = "cloud-init-default.yml"
}
source "hcloud" "base-amd64" {
image = var.base_image
location = "nbg1"
server_type = "cx11"
ssh_keys = []
user_data = file(var.user_data_path)
ssh_username = "root"
snapshot_name = "${var.output_name}-${var.version}"
snapshot_labels = {
base = var.base_image,
version = var.version,
name = "${var.output_name}-${var.version}"
}
}
source "hcloud" "base-arm64" {
image = var.base_image
location = "nbg1"
server_type = "cax11"
ssh_keys = []
user_data = file(var.user_data_path)
ssh_username = "root"
snapshot_name = "${var.output_name}-${var.version}"
snapshot_labels = {
base = var.base_image,
version = var.version,
name = "${var.output_name}-${var.version}"
}
}
build {
sources = ["source.hcloud.base-amd64", "source.hcloud.base-arm64"]
provisioner "shell" {
scripts = [
"os-setup.sh",
]
env = {
BUILDER = "packer"
}
}
}
#!/bin/bash
set -e -o pipefail
echo "waiting for cloud-init to finish..."
cloud-init status --wait
echo "installing packages..."
apt-get update
apt-get install --yes --no-install-recommends wget fail2ban
# My setup...
echo "cleanup..."
cloud-init clean --machine-id --seed --logs
rm -rvf /var/lib/cloud/instances /etc/machine-id /var/lib/dbus/machine-id /var/log/cloud-init*
# packer.pkr.hcl
packer {
required_plugins {
hcloud = {
source = "github.com/hetznercloud/hcloud"
version = ">= 1.2.0"
}
}
}
# talos.pkr.hcl
# NOTE: Based on https://www.talos.dev/v1.5/talos-guides/install/cloud-platforms/hetzner/
variable "talos_version" {
type = string
default = "v1.5.5"
}
locals {
image = "https://github.com/siderolabs/talos/releases/download/${var.talos_version}/hcloud-amd64.raw.xz"
}
source "hcloud" "talos" {
rescue = "linux64"
image = "debian-11"
location = "hel1"
server_type = "cx11"
ssh_username = "root"
snapshot_name = "talos system disk"
snapshot_labels = {
type = "infra",
os = "talos",
version = "${var.talos_version}",
}
}
build {
sources = ["source.hcloud.talos"]
provisioner "shell" {
inline = [
"apt-get install -y wget",
"wget -O /tmp/talos.raw.xz ${local.image}",
"xz -d -c /tmp/talos.raw.xz | dd of=/dev/sda && sync",
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment