What Is a Playbook?
A playbook is a YAML file describing automation tasks that Ansible executes on target hosts. Playbooks are reusable, version-controlled, and idempotent
Simple Playbook Example
Create:
---
- name: Install Nginx
hosts: webservers
become: yes
tasks:
- name: Install nginx package
apt:
name: nginx
state: present
Save as:
nginx.yml
``
Run:
ansible-playbook -i hosts nginx.yml
Playbook Structure
---
- name: Play Name
hosts: target_hosts
become: yes
tasks:
- name: Task Name
module:
parameter: value
Multiple Tasks Example
---
- name: Configure Web Server
hosts: webservers
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx
service:
name: nginx
state: started
enabled: yes
Check Mode
Preview execution:
ansible-playbook nginx.yml --check
Best Practices
- One purpose per playbook.
- Use descriptive task names.
- Store playbooks in Git.
- Test using check mode first







