Aller au contenu

Build Packer multi-OS

Templates Packer HCL et construction des images Ubuntu 24.04 et Rocky Linux 9.


Structure Packer

packer/
├── variables.pkr.hcl          # Variables communes
├── ubuntu-vdi.pkr.hcl         # Source Ubuntu 24.04
├── rocky-vdi.pkr.hcl          # Source Rocky Linux 9
└── vdi.auto.pkrvars.hcl       # Valeurs par defaut

Variables communes

Créez packer/variables.pkr.hcl :

variable "project_id" {
  type        = string
  description = "ID du projet GCP"
}

variable "zone" {
  type        = string
  default     = "europe-west1-b"
  description = "Zone GCE pour le build"
}

variable "machine_type" {
  type        = string
  default     = "e2-standard-4"
  description = "Type de machine pour le build (4 vCPU recommande pour desktop)"
}

variable "universe_domain" {
  type        = string
  default     = "googleapis.com"
  description = "Universe domain GCP (souverain ou standard)"
}

variable "profile" {
  type        = string
  default     = "full"
  description = "Profil Ansible a appliquer (developer, office, full)"
}

variable "disk_size" {
  type        = number
  default     = 30
  description = "Taille du disque en Go"
}

Template Ubuntu

Créez packer/ubuntu-vdi.pkr.hcl :

packer {
  required_plugins {
    googlecompute = {
      version = ">= 1.1.0"
      source  = "github.com/hashicorp/googlecompute"
    }
    ansible = {
      version = ">= 1.1.0"
      source  = "github.com/hashicorp/ansible"
    }
  }
}

source "googlecompute" "ubuntu-vdi" {
  project_id          = var.project_id
  zone                = var.zone
  source_image_family = "ubuntu-2404-lts"
  source_image_project_id = "ubuntu-os-cloud"
  machine_type        = var.machine_type
  ssh_username        = "packer"

  image_name          = "vdi-ubuntu-${var.profile}-{{timestamp}}"
  image_family        = "vdi-ubuntu-${var.profile}"
  image_description   = "VDI Linux Ubuntu 24.04 - profil ${var.profile}"

  universe_domain = var.universe_domain

  disk_size = var.disk_size
  disk_type = "pd-ssd"

  tags = ["packer-build"]
}

build {
  sources = ["source.googlecompute.ubuntu-vdi"]

  provisioner "ansible" {
    playbook_file = "../ansible/playbook.yml"
    user          = "packer"
    extra_arguments = [
      "--scp-extra-args", "'-O'",
      "--extra-vars", "profile=${var.profile}"
    ]
    ansible_env_vars = [
      "ANSIBLE_HOST_KEY_CHECKING=False"
    ]
  }
}

Template Rocky

Créez packer/rocky-vdi.pkr.hcl :

packer {
  required_plugins {
    googlecompute = {
      version = ">= 1.1.0"
      source  = "github.com/hashicorp/googlecompute"
    }
    ansible = {
      version = ">= 1.1.0"
      source  = "github.com/hashicorp/ansible"
    }
  }
}

source "googlecompute" "rocky-vdi" {
  project_id          = var.project_id
  zone                = var.zone
  source_image_family = "rocky-linux-9"
  source_image_project_id = "rocky-linux-cloud"
  machine_type        = var.machine_type
  ssh_username        = "packer"

  image_name          = "vdi-rocky-${var.profile}-{{timestamp}}"
  image_family        = "vdi-rocky-${var.profile}"
  image_description   = "VDI Linux Rocky 9 - profil ${var.profile}"

  universe_domain = var.universe_domain

  disk_size = var.disk_size
  disk_type = "pd-ssd"

  tags = ["packer-build"]
}

build {
  sources = ["source.googlecompute.rocky-vdi"]

  provisioner "ansible" {
    playbook_file = "../ansible/playbook.yml"
    user          = "packer"
    extra_arguments = [
      "--scp-extra-args", "'-O'",
      "--extra-vars", "profile=${var.profile}"
    ]
    ansible_env_vars = [
      "ANSIBLE_HOST_KEY_CHECKING=False"
    ]
  }
}

Valeurs des variables

Créez packer/vdi.auto.pkrvars.hcl :

project_id      = "MON-PROJET-ID"
zone            = "europe-west1-b"
universe_domain = "my-universe.gcp.example.com"
profile         = "developer"

Ne pas commiter vos valeurs

Ajoutez *.auto.pkrvars.hcl a votre .gitignore. Commitez un fichier vdi.auto.pkrvars.hcl.example avec des placeholders.

Initialisation

cd packer
packer init ubuntu-vdi.pkr.hcl
packer init rocky-vdi.pkr.hcl

Lancer le build

Ubuntu — profil developer

packer build -var 'profile=developer' ubuntu-vdi.pkr.hcl

Rocky — profil office

packer build -var 'profile=office' rocky-vdi.pkr.hcl

Override un flag spécifique

packer build -var 'profile=developer' \
  ubuntu-vdi.pkr.hcl

Puis dans le playbook, passez un override via extra_arguments :

extra_arguments = [
  "--extra-vars", "profile=${var.profile} k3s_enabled=false"
]

Matrice de build

OS Profil Image famille Durée estimée
Ubuntu 24.04 developer vdi-ubuntu-developer 20-25 min
Ubuntu 24.04 office vdi-ubuntu-office 15-20 min
Ubuntu 24.04 full vdi-ubuntu-full 25-30 min
Rocky 9 developer vdi-rocky-developer 20-25 min
Rocky 9 office vdi-rocky-office 15-20 min
Rocky 9 full vdi-rocky-full 25-30 min

Troubleshooting

Erreur Cause Solution
apt-get lock Processus concurrent Ajouter un pre-task cloud-init status --wait
xrdp service not found Paquet non installe Vérifier desktop_packages dans group_vars/
K3s timeout Ressources insuffisantes Passer a e2-standard-4 minimum
binfmt_misc not found Kernel sans support Vérifier CONFIG_BINFMT_MISC=y dans le kernel
podman rootless failed subuid/subgid manquants Vérifier la tâche de configuration subuid

Vérifier les images

gcloud compute images list --filter="family:vdi-" --format="table(name, family, status, creationTimestamp)"

Attendu :

NAME                                FAMILY                STATUS  CREATION_TIMESTAMP
vdi-ubuntu-developer-1234567890     vdi-ubuntu-developer  READY   2026-04-09T...
vdi-rocky-office-1234567890         vdi-rocky-office      READY   2026-04-09T...