هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
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.
| Item | Value |
|---|---|
| Package | packer (HashiCorp repository) |
| Program | /usr/bin/packer |
| Templates | *.pkr.hcl, variables in *.pkrvars.hcl |
| Plugins | ~/.config/packer/plugins/ (from required_plugins) |
| Logs | PACKER_LOG=1 (debug output) |
| Cloud login | the normal cloud credentials (AWS profile/role, Azure login, …) |
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.
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 packerFirst 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.
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"
}
}| Part | Why |
|---|---|
required_plugins with versions | packer init installs exactly these plugins. |
source_ami_filter + owners | Always start from the newest official RHEL image (309956199498 = Red Hat). |
ami_name with a timestamp | Every build has a unique name. |
encrypt_boot = true | The image disk is encrypted. |
| Clean-up step | No shared SSH host keys or machine IDs between servers. |
manifest post-processor | Writes the new AMI ID to manifest.json for the next pipeline step (Terraform). |
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
buildwith severalsourceblocks).
Make it safe
Security checklist:
- No secrets in templates: use environment variables,
sensitive = truevariables 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).
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
packer version gives a strange message or nothing (RHEL) | You ran cracklib's /usr/sbin/packer. | Use /usr/bin/packer. |
Timeout waiting for SSH | No 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 type | Plugins not installed. | packer init .. |
AMI name 'golden-rhel9-…' is already in use | The same name twice. | Add a timestamp (local.stamp) to the name. |
UnauthorizedOperation | The IAM role misses a permission. | Add the needed EC2 actions to the Packer role. |
| Ansible provisioner fails to connect | Ansible 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 crash | The build was killed. | Delete instances, key pairs and security groups named packer_*. |
Next steps
- Quick how-to: Build a VM image with Packer.
- Command page: packer.
- At work: Golden images, Images and first boot.
- Use the image: Terraform. Configure inside it: Ansible. Log in to AWS: AWS CLI.
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.