100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Infrastructure as Code — Terraform & Ansible
30 minintermediate

Playbook Anatomy — Plays, Tasks, Handlers, Tags and Blocks

An Ansible playbook is a YAML file that declares the desired state of one or more groups of hosts. The playbook is structured into plays — each play targets a specific group of hosts from the inventory and contains a list of tasks to execute on those hosts. Tasks call Ansible modules, which are the actual units of work: installing a package, writing a configuration file, starting a service, creating a user, or executing a shell command. This structure gives Ansible its predictability: reading a well-written playbook, an experienced engineer can predict exactly what will happen on the target hosts without running it. The idempotency principle — the cornerstone of reliable automation — means that running the same playbook twice produces the same result, making playbooks safe to re-run after failures or configuration changes.

Handlers and tags are two Ansible features that improve operational efficiency. Handlers are tasks that run only when triggered by other tasks — the canonical use is restarting Nginx only when its configuration file changes, not on every playbook run. Without handlers, a naive automation would restart Nginx after every run even if nothing changed, causing unnecessary service interruptions. With handlers, the restart only happens when the notify directive fires, which only happens when the notifying task makes an actual change (not when it is idempotently skipping because the configuration already matches). Tags allow selective execution of specific tasks within a playbook — 'ansible-playbook deploy.yml --tags nginx' runs only the tasks tagged 'nginx', skipping everything else. This enables partial deployments without maintaining separate playbooks for each component.

Analogy🏏Cricket
🏏 Think of it like cricket: Terraform Cloud is the ICC's centralised match management platform — instead of each national board (team) maintaining its own scoring system, umpire assignment software and results database (self-managed CI/CD + S3 backend), the ICC platform handles all of this centrally. When a board member proposes a rule change (pull request), the platform automatically simulates the match under the new rules (speculative plan on PR), shows the referees the impact (plan output in PR comment), and requires the match committee to approve (policy gates) before the rule takes effect. The audit log records every change, every approval, and who made each decision — providing the governance and traceability that serious tournament operations require.
Lesson 16 of 33
0% complete