100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is an Ansible Playbook?

Learn what an Ansible playbook is, how tasks and modules achieve idempotent config management, and why Ansible is agentless.

easyQ96 of 224 in DevOps Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

An Ansible playbook is a YAML file that declares a list of plays, each mapping a set of managed hosts to an ordered sequence of tasks, which Ansible executes over SSH to bring those hosts to a desired configuration state.

Each play targets a host group defined in an inventory file and runs a list of tasks in order, where every task invokes a module β€” such as apt, copy, template, or service β€” that is idempotent, meaning running it repeatedly produces the same end state without duplicating side effects. Because Ansible is agentless, it pushes tasks to managed nodes over SSH using Python (or WinRM for Windows) rather than requiring a persistent daemon on every host, which keeps the control plane simple. Variables, conditionals (when), loops, and handlers (tasks triggered only when a preceding task reports a change, e.g. restarting a service after a config file changes) make playbooks expressive without needing a general-purpose scripting language. Playbooks are typically organized into reusable roles so the same tasks β€” installing nginx, configuring users β€” can be shared across many playbooks and projects.

  • Declarative, human-readable automation of server configuration
  • Agentless execution keeps managed nodes simple, no daemon required
  • Idempotent tasks make repeated runs safe and predictable
  • Reusable roles enable sharing configuration logic across projects

AI Mentor Explanation

An Ansible playbook is like a written match-day checklist a support staff hands to every ground the touring team visits β€” set up the nets in this order, check the pitch, prepare the dressing room, brief the umpires. Each ground (managed host) follows the exact same checklist, and if a ground already has the nets set up correctly, that step is simply skipped rather than redone badly. The staff never needs to be physically stationed permanently at each ground; they just hand over the checklist and it gets carried out. Grouping checklist items into a reusable β€œpre-match setup” bundle means the same bundle can be reused at every venue on the tour.

Step-by-Step Explanation

  1. Step 1

    Define inventory

    List managed hosts and group them (e.g. webservers, dbservers) in an inventory file.

  2. Step 2

    Write the playbook

    Declare plays mapping host groups to an ordered list of tasks using YAML.

  3. Step 3

    Tasks call modules

    Each task invokes an idempotent module (apt, copy, template, service) with parameters.

  4. Step 4

    Run ansible-playbook

    Ansible connects over SSH, executes tasks in order, and triggers handlers on changes.

What Interviewer Expects

  • Understanding that playbooks are declarative YAML, not imperative scripts
  • Knowledge that Ansible is agentless and connects over SSH
  • Awareness of idempotency and why it makes repeated runs safe
  • Understanding of roles as the reusable packaging mechanism

Common Mistakes

  • Thinking Ansible requires an agent installed on every managed node
  • Writing non-idempotent tasks (e.g. raw shell commands) instead of proper modules
  • Confusing a play with a task or a role
  • Forgetting handlers only run when a preceding task reports a change

Best Answer (HR Friendly)

β€œAn Ansible playbook is basically a written recipe of steps we want applied to a group of servers β€” install this package, copy this config file, restart this service. Because Ansible connects over plain SSH and every step is designed to be safely repeatable, we can run the same playbook again and again without worrying about breaking something that is already correctly configured.”

Code Example

A simple playbook installing and starting nginx
- name: Configure web servers
  hosts: webservers
  become: true
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present

    - name: Deploy nginx config
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: Restart nginx

  handlers:
    - name: Restart nginx
      service:
        name: nginx
        state: restarted

Follow-up Questions

  • What makes an Ansible task idempotent, and why does it matter?
  • How does Ansible connect to managed nodes without an agent?
  • What is the difference between a playbook, a play, and a role?
  • When does a handler run compared to a regular task?

MCQ Practice

1. How does Ansible typically connect to managed nodes?

Ansible is agentless, pushing tasks to managed nodes over SSH (or WinRM) rather than requiring a persistent daemon.

2. What does it mean for an Ansible task to be idempotent?

Idempotent tasks converge the host to a desired state and are safe to re-run without duplicating changes.

3. When does an Ansible handler execute?

Handlers are triggered only by a notify from a task that actually reported a change, such as restarting a service after its config changed.

Flash Cards

What is an Ansible playbook? β€” A YAML file declaring plays that map hosts to an ordered list of tasks.

Is Ansible agent-based or agentless? β€” Agentless β€” it connects over SSH/WinRM, no daemon required on managed nodes.

What makes Ansible tasks safe to re-run? β€” Idempotency β€” modules converge to the desired state without duplicating effects.

How are reusable Ansible steps packaged? β€” Roles, which bundle tasks, variables, templates, and handlers for reuse.

1 / 4

Continue Learning