Playbook Structure
A playbook is a YAML file consisting of one or more plays. Each play maps a group of hosts to a set of tasks.
- name: Configure web servers
hosts: web
become: true
tasks:
- name: Install packages
apt:
name: nginx
state: present
Tasks and Modules
Tasks invoke modules to perform actions on managed hosts. Modules are idempotent and designed to ensure system consistency.
Handlers
Handlers are special tasks triggered by notifications. They run only when explicitly notified by other tasks.
handlers:
- name: restart nginx
service:
name: nginx
state: restarted
Variables
Variables allow playbooks to be flexible and reusable. They can be defined in inventories, playbooks, or external files.
Execution
ansible-playbook site.yml