متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 50Packer · IaC and automation

Install and set up Packer (golden images)

Packer builds machine images from code: it starts a temporary VM in AWS, Azure, Google Cloud, VMware or Proxmox, installs and hardens everything you need, and saves the result as an image (AMI, managed image, template). Every new server then starts from the same tested golden image — fast, identical and patched. This guide installs Packer from the HashiCorp repository on RHEL and Ubuntu, writes an HCL2 template for AWS, provisions with shell and Ansible, and covers variables, CI builds and the fixes.

  • 9 الدروس
  • 9 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

50.1

Know it before you install

A template has sources (where and from what base to build, for example amazon-ebs from a RHEL AMI), a build block with provisioners (shell scripts, Ansible, files) and post-processors (manifest, upload). Plugins (amazon, azure, googlecompute, vsphere, ansible …) are installed with packer init.

ItemValue
Packagepacker (HashiCorp repository)
Program/usr/bin/packer
Templates*.pkr.hcl, variables in *.pkrvars.hcl
Plugins~/.config/packer/plugins/ (from required_plugins)
LogsPACKER_LOG=1 (debug output)
Cloud loginthe normal cloud credentials (AWS profile/role, Azure login, …)

الأوامر في هذا الدرس

50.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install dnf-plugins-core
dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
dnf -y install packer
/usr/bin/packer version

انتبه

On RHEL, cracklib has its own program called **packer** in /usr/sbin. If packer version prints nothing useful, call /usr/bin/packer or put /usr/bin first in your PATH.

الأوامر في هذا الدرس

50.3

Install on Ubuntu and Debian

curl -fsSL https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" > /etc/apt/sources.list.d/hashicorp.list
apt-get update
apt-get install -y packer

50.4

First configuration: a template for AWS

A golden RHEL 9 image: latest updates, hardening, monitoring agent, then a clean-up so every server gets fresh SSH host keys and cloud-init runs again.

rhel9.pkr.hcl
packer {
  required_plugins {
    amazon = {
      source  = "github.com/hashicorp/amazon"
      version = "~> 1.3"
    }
    ansible = {
      source  = "github.com/hashicorp/ansible"
      version = "~> 1.1"
    }
  }
}
 
variable "region" {
  type    = string
  default = "eu-central-1"
}
 
variable "version" {
  type = string
}
 
locals {
  stamp = formatdate("YYYYMMDD-hhmm", timestamp())
}
 
source "amazon-ebs" "rhel9" {
  region        = var.region
  instance_type = "t3.small"
  ssh_username  = "ec2-user"
  ami_name      = "golden-rhel9-${var.version}-${local.stamp}"
  source_ami_filter {
    filters = {
      name                = "RHEL-9.*_HVM-*-x86_64-*"
      virtualization-type = "hvm"
      root-device-type    = "ebs"
    }
    owners      = ["309956199498"]
    most_recent = true
  }
  encrypt_boot = true
  tags = {
    Name    = "golden-rhel9"
    Version = var.version
    Built   = local.stamp
  }
}
 
build {
  sources = ["source.amazon-ebs.rhel9"]
 
  provisioner "shell" {
    inline = [
      "sudo dnf -y upgrade",
      "sudo dnf -y install chrony audit firewalld",
      "sudo systemctl enable chronyd auditd firewalld"
    ]
  }
 
  provisioner "ansible" {
    playbook_file = "./ansible/harden.yml"
    user          = "ec2-user"
  }
 
  provisioner "shell" {
    inline = [
      "sudo rm -f /etc/ssh/ssh_host_*",
      "sudo cloud-init clean --logs",
      "sudo truncate -s 0 /etc/machine-id"
    ]
  }
 
  post-processor "manifest" {
    output = "manifest.json"
  }
}
PartWhy
required_plugins with versionspacker init installs exactly these plugins.
source_ami_filter + ownersAlways start from the newest official RHEL image (309956199498 = Red Hat).
ami_name with a timestampEvery build has a unique name.
encrypt_boot = trueThe image disk is encrypted.
Clean-up stepNo shared SSH host keys or machine IDs between servers.
manifest post-processorWrites the new AMI ID to manifest.json for the next pipeline step (Terraform).

الأوامر في هذا الدرس

50.5

Build it

Log in to AWS first (for example aws sso login, export AWS_PROFILE=images), then:

packer init .
packer fmt .
packer validate -var 'version=1.4.0' .
packer build -var 'version=1.4.0' .
==> amazon-ebs.rhel9: Creating temporary keypair: packer_6739…
==> amazon-ebs.rhel9: Launching a source AWS instance...
…
==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs.rhel9: AMIs were created:
eu-central-1: ami-0a1b2c3d4e5f67890
jq -r '.builds[-1].artifact_id' manifest.json
PACKER_LOG=1 packer build -var 'version=1.4.1' . 2> packer-debug.log
packer build -on-error=ask -var 'version=1.4.1' .

-on-error=ask keeps the temporary VM when a step fails, so you can log in and look.

Real uses at work:

  • Golden images for every server type (base, web, Kubernetes node), rebuilt monthly with the newest patches.
  • Images tested in CI (scan with Trivy or OpenSCAP) before Terraform uses them.
  • The same template for AWS, Azure and VMware (one build with several source blocks).

50.6

Make it safe

Security checklist:

  • No secrets in templates: use environment variables, sensitive = true variables or a secret manager; never bake passwords or keys into the image.
  • Start from official base images; pin plugin versions.
  • Clean SSH host keys, machine ID, logs, shell history and cloud-init state before the image is saved.
  • Encrypt image disks; share images only with the accounts that need them.
  • Least-privilege IAM role for Packer (create instance, snapshot, AMI — not admin).
  • Deregister old images and delete their snapshots (cost and old vulnerabilities).

50.7

Update and remove

dnf -y upgrade packer
apt-get install --only-upgrade -y packer
packer init -upgrade .
dnf -y remove packer

الأوامر في هذا الدرس

50.8

Fix common problems

ProblemCauseFix
packer version gives a strange message or nothing (RHEL)You ran cracklib's /usr/sbin/packer.Use /usr/bin/packer.
Timeout waiting for SSHNo public IP/route, security group, or wrong ssh_username.Use a subnet with internet/SSH access (or ssh_interface = "session_manager"); check the user (ec2-user, ubuntu).
Missing plugins / unknown source typePlugins not installed.packer init ..
AMI name 'golden-rhel9-…' is already in useThe same name twice.Add a timestamp (local.stamp) to the name.
UnauthorizedOperationThe IAM role misses a permission.Add the needed EC2 actions to the Packer role.
Ansible provisioner fails to connectAnsible not installed on the build machine, or Python missing in the VM.Install Ansible locally; make sure the base image has Python.
Leftover VMs or keys after a crashThe build was killed.Delete instances, key pairs and security groups named packer_*.

الأوامر في هذا الدرس

50.9

Next steps

1Why delete the SSH host keys before the image is saved?

Otherwise every server built from the image has the same host keys.

2Where does the next pipeline step find the new AMI ID?

In manifest.json from the manifest post-processor.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل