$ run as a normal user# run as root (or with sudo)
Skills in this chapter
- Infrastructure as code — 8
Terraform — the standard for provisioning
terraform describes resources in HCL, shows a plan and applies it; the state remembers what exists.
OpenTofu — the open-source Terraform
tofu runs the same code as Terraform, under an open licence, with built-in state encryption.
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 applyPulumi — IaC in a programming language
pulumi describes resources in TypeScript, Python or Go — loops and tests come for free.
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 webGood practice:
- Every task idempotent: the second run changes nothing.
- Secrets in Ansible Vault or an external secret store.
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 --waitChoose the right tool
Use each tool for what it is good at.
| Job | Tool |
|---|---|
| Cloud resources (VNet, VM, AKS, database) | Terraform / OpenTofu / Pulumi |
| Many environments of the same modules | Terragrunt |
| What runs inside a VM | Ansible (or cloud-init for the first boot) |
| A golden VM image | Packer |
| A local lab | Vagrant |
| What runs in Kubernetes | Helm / Kustomize + GitOps |
Remember
Plan in the pull request, apply only from the pipeline, and check for drift (terraform plan -refresh-only) every week.