0 / 13 lessons — 0%
Lesson 04 / 13
Playbooks & YAML basics
A playbook is where ad-hoc commands graduate into something repeatable and version-controllable. It's YAML describing one or more plays — each play targets a group of hosts and runs a list of tasks against them, in order.
# webserver.yml - name: Configure web servers hosts: webservers become: true tasks: - name: Install nginx apt: name: nginx state: present - name: Copy homepage copy: src: files/index.html dest: /var/www/html/index.html - name: Ensure nginx is running service: name: nginx state: started enabled: true
ansible-playbook -i inventory.ini webserver.yml # preview what would change, without changing anything ansible-playbook -i inventory.ini webserver.yml --check --diff
| YAML rule | Why it trips people up |
|---|---|
| Indentation is meaningful | Tabs are invalid — use spaces, consistently, or the file won't parse |
key: value needs a space after the colon | name:web is not the same as name: web |
| Lists use a leading dash | Each task starts with - name: ... |
Always run
--check --diff on anything touching production for the first time. It shows you exactly what would change without changing it — the closest thing Ansible has to a dry run.Try it yourselfSave the playbook above, run it with
--check --diff first, read the proposed diff, then run it for real. Run it a second time right after — notice nothing reports as "changed" the second time. That's idempotency, and it's the whole next lesson.