Your first playbook
Plays, tasks, and YAML structure.
A playbook is a YAML file that describes automation: one or more plays, each mapping a group of hosts to an ordered list of tasks. It is the repeatable, version-controlled form of what you would otherwise run ad-hoc — and because it is declarative and idempotent, it is the artifact you actually keep and evolve. You run it with ansible-playbook.
- name: Configure web servershosts: webbecome: true # run tasks with sudotasks:- name: Install nginxansible.builtin.apt:name: nginxstate: present- name: Ensure nginx is running and enabledansible.builtin.service:name: nginxstate: startedenabled: true
Running and reading results
ansible-playbook runs each task in order across all matching hosts, printing ok (already correct), changed (it acted), or failed for each. The PLAY RECAP at the end summarizes per host. A key habit: run with --check first (check mode) to preview what would change without changing anything, then run for real once the plan looks right — Ansible’s equivalent of a dry run.
$ ansible-playbook -i inventory.ini site.yml --check # dry run: predict changes$ ansible-playbook -i inventory.ini site.yml # applyPLAY RECAPweb1 : ok=3 changed=1 unreachable=0 failed=0web2 : ok=3 changed=1 unreachable=0 failed=0