What are Ansible tasks, plays, and modules?
Understand Ansible modules, tasks and plays, how they form the playbook hierarchy, why modules are idempotent, with YAML examples and interview questions.
Expected Interview Answer
In Ansible a module is a self-contained unit of code that performs one action (like installing a package), a task is a single invocation of a module with arguments, and a play is an ordered group of tasks mapped to a set of hosts.
These three form a hierarchy: modules are the reusable building blocks Ansible ships (apt, service, copy, file), tasks call those modules with specific parameters and run in order on each host, and plays bundle tasks together with a hosts target plus settings like become and vars. One or more plays make up a playbook. Modules are designed to be idempotent, so tasks report changed only when they actually alter the system.
- Modules provide reusable, idempotent building blocks
- Tasks give ordered, named, readable steps
- Plays map tasks to the right hosts
- Clear separation makes automation easy to reason about
- Encourages reuse through roles and collections
AI Mentor Explanation
A module is like a single cricketing skill a player has mastered — bowling a yorker or hitting a cover drive. A task is calling for that skill in a specific delivery: bowl a yorker at the stumps now. A play is an over or a phase of the innings, an ordered set of such deliveries aimed at a particular batter (host). Together the phases form the full match plan, just as tasks and plays form a playbook.
Step-by-Step Explanation
Step 1
Start with a module
Pick a module such as ansible.builtin.apt or service that performs the action you need.
Step 2
Write a task
Give it a name and call the module with arguments; the task runs once per targeted host.
Step 3
Order tasks
List tasks in the sequence they must execute — Ansible runs them top to bottom per host.
Step 4
Group into a play
Wrap the tasks in a play with a hosts target and options like become and vars.
Step 5
Combine plays
Add multiple plays to a single YAML file to form a complete playbook targeting different host groups.
Step 6
Rely on idempotency
Because modules check state, re-running only changes what is out of the desired state.
What Interviewer Expects
- Correct hierarchy: module -> task -> play -> playbook
- Modules are the executable units, tasks invoke them
- Plays map tasks to hosts with settings like become
- Awareness that modules are idempotent
- Examples of common modules (apt, service, copy, file)
Common Mistakes
- Using module, task and play as interchangeable terms
- Thinking a play is a single task
- Not knowing modules ship in collections like ansible.builtin
- Assuming tasks run in parallel on a single host
- Ignoring idempotency and expecting tasks to always report changed
Best Answer (HR Friendly)
“In Ansible, a module is a small tool that does one job like installing software, a task is you telling that tool exactly what to do, and a play groups those tasks and points them at the right servers. Stack a few plays together and you get a playbook, which is the full automation script.”
Code Example
---
- name: Set up app servers # this whole block is a PLAY
hosts: appservers
become: true
tasks:
- name: Install git # a TASK
ansible.builtin.apt: # the MODULE
name: git
state: present
- name: Copy config file # another TASK
ansible.builtin.copy: # another MODULE
src: app.conf
dest: /etc/app/app.conf
mode: '0644'Follow-up Questions
- How is a playbook different from a single play?
- What is an Ansible collection and how do modules relate to it?
- What does it mean for a module to be idempotent?
- Name three commonly used built-in modules.
- How do handlers relate to tasks?
MCQ Practice
1. Which is the smallest reusable unit that actually performs an action?
A module is the self-contained code unit that performs one action; tasks invoke modules.
2. A play primarily maps tasks to what?
A play associates an ordered list of tasks with a target set of hosts plus play-level settings.
3. What is the correct hierarchy from smallest to largest?
Modules are invoked by tasks, tasks are grouped into plays, and plays make up a playbook.
Flash Cards
What is an Ansible module? — A self-contained unit of code that performs one action, like apt or service; it is idempotent.
What is a task? — A single invocation of a module with arguments, usually named, run in order on each host.
What is a play? — An ordered group of tasks mapped to a set of hosts with settings like become and vars.
How do these form a playbook? — One or more plays combined in a YAML file make a complete playbook.
Where do modules live? — In collections such as ansible.builtin and community/vendor collections on Ansible Galaxy.