Ansible interview question · Most asked
Explain Ansible's architecture: control node, inventory, modules, playbooks, roles and handlers.
How it works Asked in DevOps Engineer interviews
Summary
Ansible runs from a control node and connects to managed hosts over SSH (WinRM for Windows) with no agent installed. The inventory lists hosts and groups, modules do the work on each host, playbooks map groups of hosts to ordered tasks, roles package tasks, templates and defaults for reuse, and handlers are tasks that run only when something notifies them, typically a service restart after a config change.
Longer Answer
For each play, Ansible gathers facts, then runs each task across all targeted hosts in parallel (5 forks by default) before moving to the next task. A task calls a module with arguments; Ansible copies the module to the host, runs it with Python and reads back JSON saying whether anything changed.
- Inventory: static INI/YAML files or a dynamic inventory plugin (AWS, Azure, GCP) that asks the cloud API.
Group variables live in
group_vars/, host variables inhost_vars/. - Modules:
apt,dnf,copy,template,service,user,lineinfile, cloud modules and thousands more through collections (ansible-galaxy collection install community.general). - Playbooks: YAML; a play sets
hosts,become,varsand a list oftasksorroles. - Roles: a fixed layout (
tasks/,handlers/,templates/,defaults/,vars/,meta/) so the same role works for many playbooks and environments. - Handlers: run once at the end of the play (or at
meta: flush_handlers), however many tasks notified them.
Example
site.yml # playbook: which roles run on which groups
inventories/prod/hosts.yml
inventories/prod/group_vars/web.yml
roles/nginx/
tasks/main.yml
handlers/main.yml
templates/nginx.conf.j2
defaults/main.yml # lowest-precedence, overridable variables
Key Points
- Agentless: SSH plus Python on the host.
- Inventory says where, playbooks say what, modules do the work.
- Roles make tasks reusable across playbooks and environments.
- Handlers run once, only when notified by a task that changed something.
Explanation
What interviewers look for
A clear picture of the moving parts and how a run actually executes (facts, forks, task-by-task across hosts), not just a list of terms.
More Ansible questions
- Write an Ansible playbook that installs Nginx, deploys its configuration, enables the service and restarts it only when the configuration changes.
- What is idempotency in Ansible, and how do you keep your own tasks idempotent?
- How do you structure reusable Ansible roles for several environments?
- Explain serial, delegate_to, when and notify in Ansible.
- How would you automate TLS certificate installation and renewal with Ansible?
- How would you configure 100 Linux servers consistently with Ansible?
- How do Ansible and Terraform work together in one delivery pipeline?
- A playbook run fails halfway through 50 hosts. What do you do?