Fifteen common Ansible interview questions
- How does Ansible work, and why is it called agentless?
- What is an inventory? Static versus dynamic?
- What are playbooks, plays, tasks and modules?
- What does idempotent mean in Ansible?
- command versus shell versus a dedicated module?
- What are handlers, and when do they run?
- What are roles, and how do you structure them?
- What are collections?
- Explain variable precedence at a high level.
- What are facts, and how do you use them?
- How do you manage secrets?
- How do you test playbooks?
- How do you speed up Ansible on many hosts?
- Ansible versus Terraform?
- How do you handle errors and conditionals?
Core concepts
- Agentless: the control node connects over SSH (or WinRM for Windows), pushes small modules, runs them and removes them — nothing permanent to install on managed hosts beyond Python on Linux.
- Inventory: the hosts and groups to manage; dynamic inventories query cloud APIs so lists stay current.
- Playbook: YAML defining plays that map host groups to tasks; each task calls a module with arguments.
- Idempotency: running a playbook again leaves a correctly configured system unchanged and reports no changes — modules check state before acting.
Writing good playbooks
| Practice | Why |
|---|---|
| Prefer dedicated modules over command/shell | Modules are idempotent and report changes correctly |
| Use handlers for restarts | Services restart only when configuration actually changed |
| Organise into roles | Reusable, testable units with defaults, tasks, handlers, templates |
| Use templates (Jinja2) | Generate configuration files from variables |
| Tag tasks | Run subsets during development or incidents |
| Use check mode and diff | Preview changes before applying |
Variables, facts and secrets
Variables can come from many places — role defaults, inventory group and host variables, playbook vars, extra vars on the command line — with defined precedence: role defaults are lowest and extra vars highest. A practical answer is to put sensible defaults in roles, environment differences in group variables, and avoid scattering the same variable across levels. Facts are host details Ansible gathers automatically, such as OS and IP addresses, used in conditionals and templates. Encrypt secrets with Ansible Vault or fetch them from an external secrets manager at runtime; never commit plaintext secrets.
Testing, scale and errors
- Testing: lint playbooks, run roles against disposable test instances with a testing framework, and use check mode in pipelines.
- Scale: raise the number of forks for parallelism, enable SSH pipelining, cache or disable fact gathering when not needed, and use async tasks for long operations.
- Errors and logic: when for conditionals, loop for iteration, block with rescue and always for error handling, failed_when and changed_when to define outcomes accurately.
Ansible versus Terraform
Terraform declaratively provisions infrastructure — networks, VMs, managed services — and tracks it in state. Ansible configures what runs on that infrastructure — packages, files, services — procedurally but idempotently. Teams commonly use Terraform to create servers and Ansible to configure them, or use images built in advance so less configuration happens at boot. See Terraform interview questions.
A scenario to rehearse
“Roll out a configuration change to 500 web servers without taking the site down.” Use a serial setting to update servers in batches, remove each batch from the load balancer before changing it, run the role, verify health with a check task, and return it to the pool; set max_fail_percentage so a bad change stops the rollout early. Test first in check mode and on a staging group. This combines most of what interviewers want to see: idempotent roles, handlers, rolling updates and safety.
Practising
The fastest way to find gaps is to be asked. Run a spoken mock round on Ansible topics — a free AI interview assistant such as CoPilot Interview builds one from your résumé and the job description and scores every answer — then study whatever you stumbled on. Write a role that installs and configures a web server with a template and a handler, then explain each part aloud. See also DevOps interview questions.
Disclosure: CoPilot Interview is our product, so weigh what we say about it accordingly. Everything else on this page applies whichever tool you use, or none.
Frequently asked questions
What are common Ansible interview questions?
Agentless architecture, inventories, playbooks and modules, idempotency, handlers, roles and collections, variable precedence, facts, Vault, testing, scaling and Ansible versus Terraform.
Why is Ansible agentless?
It connects to managed hosts over SSH or WinRM, transfers and runs modules temporarily, and needs no persistent agent on those hosts.
What is idempotency in Ansible?
Running the same playbook repeatedly produces the same end state; if the system is already correct, tasks report no changes.
When should I use the shell module?
Only when no dedicated module exists. Shell and command aren't idempotent by default, so add creates, removes or changed_when conditions to make them behave correctly.
Practise on real interview questions
Run 145 real interview questions across 10 tracks, answer out loud and see a structured answer to compare with yours. Free, no signup.
Try a practice question