Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 7Name in the course: Infrastructure as code

Build infrastructure from code

Infrastructure as code (IaC) means servers, networks, clusters and their settings live in Git and are made by tools — never by hand in a portal. You get reviews, history, and the same result every time. There are two kinds of tools: provisioning (make the resources: Terraform, OpenTofu, Pulumi) and configuration (set up what is inside: Ansible, cloud-init).

  • 7 Lessons
  • 9 Commands

$ run as a normal user# run as root (or with sudo)

Skills in this chapter

  • Infrastructure as code — 8

7.1

Terraform — the standard for provisioning

terraform describes resources in HCL, shows a plan and applies it; the state remembers what exists.

terraform init
terraform fmt -recursive && terraform validate
terraform plan -out tfplan
terraform apply tfplan

Good practice:

  • Remote state with locking (Azure Blob, S3 + DynamoDB); never commit state files.
  • Modules with pinned versions; one state per environment and part.

7.2

OpenTofu — the open-source Terraform

tofu runs the same code as Terraform, under an open licence, with built-in state encryption.

tofu init
tofu plan -out tfplan
tofu apply tfplan

Commands in this lesson

7.3

Terragrunt — many environments without copy-paste

terragrunt keeps one backend config and shared inputs, and runs many modules in the right order.

cd live/prod && terragrunt run-all plan
TERRAGRUNT_TFPATH=tofu terragrunt apply

7.4

Pulumi — IaC in a programming language

pulumi describes resources in TypeScript, Python or Go — loops and tests come for free.

pulumi preview --diff
pulumi up --stack prod --yes

7.5

Ansible — configure servers

ansible connects with SSH and makes servers match a playbook — packages, files, users, services.

ansible all -i inventory.ini -m ping
ansible-playbook -i inventory.ini site.yml --check --diff
ansible-playbook -i inventory.ini site.yml --limit web

Good practice:

  • Every task idempotent: the second run changes nothing.
  • Secrets in Ansible Vault or an external secret store.

7.6

Packer, Vagrant and cloud-init — images and first boot

packer bakes a ready VM image; vagrant makes local lab VMs; cloud-init sets up a cloud VM at first boot.

packer build rocky9.pkr.hcl
vagrant up && vagrant ssh server1
cloud-init status --wait

7.7

Choose the right tool

Use each tool for what it is good at.

JobTool
Cloud resources (VNet, VM, AKS, database)Terraform / OpenTofu / Pulumi
Many environments of the same modulesTerragrunt
What runs inside a VMAnsible (or cloud-init for the first boot)
A golden VM imagePacker
A local labVagrant
What runs in KubernetesHelm / Kustomize + GitOps

Remember

Plan in the pull request, apply only from the pipeline, and check for drift (terraform plan -refresh-only) every week.

All commands in this chapter