Play → task → module
| Concept | Meaning |
|---|---|
| Play | One block: which hosts + which tasks |
| Task | One named step |
| Module | The unit of work (apt, service, copy, …) |
Playbook
└── Play (hosts: web)
├── Task → Module (apt)
├── Task → Module (service)
└── Task → Module (copy)
What is a play?
- name: Configure web servers
hosts: web
become: true
tasks:
- ...One playbook can contain multiple plays (for example web then db).
What is a task?
- name: Install nginx
apt:
name: nginx
state: presentname:— human-readable label in outputapt:— module- arguments under the module key
What is a module?
Modules are Ansible’s building blocks — the same ones you used in Day 1 ad-hoc commands.
Common modules
- name: Test connectivity
ping:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx
service:
name: nginx
state: started
- name: Copy file
copy:
src: index.html
dest: /tmp/index.html
- name: Create directory
file:
path: /tmp/devops
state: directory
- name: Create user
user:
name: devops
state: presentMultiple tasks example
---
- name: Nginx setup
hosts: all
become: true
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx
service:
name: nginx
state: started
enabled: true
- name: Ensure site dir exists
file:
path: /var/www/html
state: directoryExecution flow
ansible-playbook site.yml
↓
Load inventory + play
↓
For each task: run module over SSH
↓
Report ok / changed / failed
Mini project ideas
- Install
gitandcurlwithapt - Create
/opt/appwithfile - Create a
deployuser withuser - Combine into one play
Prefer modules over shell/command when a module exists — better idempotency and clearer intent.