What is the difference between ansible and ansible-playbook commands?
Understand the difference between the ansible ad-hoc command and ansible-playbook, when to use each, with examples and common interview questions and answers.
Expected Interview Answer
The 'ansible' command runs a single ad-hoc task or module against hosts directly from the command line, while 'ansible-playbook' executes a full playbook — an ordered YAML file of plays and tasks — enabling complex, repeatable, and idempotent automation.
Use 'ansible' for one-off actions like checking connectivity, restarting a service, or gathering facts, where writing a file would be overkill. Use 'ansible-playbook' for anything reusable or multi-step: it reads a YAML file, runs handlers, variables, roles, loops, conditionals, and reports a play recap. Ad-hoc commands are great for quick diagnostics; playbooks are the backbone of documented, version-controlled infrastructure as code.
- ansible: fast one-off tasks with no file to write
- ansible: ideal for quick checks like ping, uptime, or service status
- ansible-playbook: repeatable, version-controlled automation
- ansible-playbook: supports variables, handlers, roles, loops and conditionals
- ansible-playbook: produces a play recap and enforces idempotency
AI Mentor Explanation
Running 'ansible' ad-hoc is like a captain shouting a single instruction to move one fielder for the next ball — quick and immediate. Running 'ansible-playbook' is like following the whole pre-planned innings strategy written in the team notebook: field placements, bowling changes and batting orders executed in sequence, repeatable match after match.
Step-by-Step Explanation
Step 1
Pick the task scope
One quick action? Use ansible. A multi-step, reusable workflow? Use ansible-playbook.
Step 2
Run an ad-hoc command
Invoke ansible with a host pattern and -m module, e.g. 'ansible web -m ping' to test connectivity.
Step 3
Write a playbook
Author a YAML file with plays, tasks, variables and handlers describing the desired state.
Step 4
Execute the playbook
Run 'ansible-playbook site.yml -i inventory' to apply all tasks in order and see a play recap.
Step 5
Choose based on repeatability
Use ad-hoc for diagnostics; commit playbooks to version control for documented, idempotent automation.
What Interviewer Expects
- Knowing ansible is for ad-hoc single tasks and modules
- Knowing ansible-playbook runs a full YAML playbook
- Understanding when each is appropriate
- Awareness that playbooks support variables, handlers, roles and idempotency
- Ability to give a concrete example of each
Common Mistakes
- Thinking ansible and ansible-playbook are interchangeable
- Believing ad-hoc commands can run a YAML playbook file
- Assuming ansible-playbook needs -m to specify a module
- Forgetting ad-hoc commands still require an inventory or host pattern
- Claiming ad-hoc commands support handlers and roles
Best Answer (HR Friendly)
“The 'ansible' command is for quick one-off jobs, like checking if servers respond. The 'ansible-playbook' command runs a saved script of many steps in order, which is what teams use for reliable, repeatable setups they can reuse and share.”
Code Example
# Ad-hoc: run a single module against hosts
ansible web -i inventory.ini -m ping
ansible web -i inventory.ini -m service -a "name=nginx state=restarted" --become
# Playbook: run an ordered YAML file of tasks
ansible-playbook -i inventory.ini site.yml
# site.yml
# - name: Configure web servers
# hosts: web
# become: true
# tasks:
# - name: Ensure nginx running
# ansible.builtin.service:
# name: nginx
# state: startedFollow-up Questions
- When would you prefer an ad-hoc command over a playbook?
- How do you pass a module and arguments to the ansible command?
- What is a play recap and which command produces it?
- Can ansible-playbook run without an inventory file?
- How do handlers and roles differ from ad-hoc execution?
MCQ Practice
1. Which command runs a single module against hosts without a YAML file?
The 'ansible' command runs ad-hoc tasks using -m to specify a module directly on the CLI.
2. Which command executes an ordered YAML file of plays and tasks?
ansible-playbook reads a playbook YAML file and runs its plays, tasks and handlers in order.
3. Which feature is available with ansible-playbook but NOT with ad-hoc commands?
Handlers and roles are playbook constructs; ad-hoc commands run a single module without them.
Flash Cards
What does the 'ansible' command do? — Runs a single ad-hoc module/task against hosts directly from the command line.
What does 'ansible-playbook' do? — Executes a YAML playbook of ordered plays, tasks and handlers for repeatable automation.
Which flag specifies a module in an ad-hoc command? — -m, e.g. 'ansible web -m ping'.
When should you use ad-hoc commands? — For quick one-off tasks and diagnostics like pings, service restarts, or fact gathering.