Ansible interview questions and answers for DevOps and Linux roles
Ansible interview questions test whether you can automate server configuration that is safe to run again and again: an agentless push model over SSH, inventories and groups, playbooks and roles, idempotent tasks, handlers, variables and their precedence, and Ansible Vault for secrets. Senior rounds ask about rolling updates, large fleets, testing roles, and when Ansible is the right tool compared with Terraform. Interviewers listen for the word idempotent used correctly and backed by an example.
By Evin JohnPublished 7 min read
Where Ansible questions come up
Ansible is a common choice for configuring servers, network devices and Windows hosts, and for scripted operations such as patching and certificate rotation. It appears in DevOps, Linux administration, systems engineering, network automation and SRE interviews. Questions often follow ones about provisioning, so this page also covers how Ansible and Terraform divide the work.
- ConceptsAgentless architecture, inventory, playbooks, modules and idempotence.
- Write or review a playbookInstall and configure a service, template a config file, restart only when it changed.
- Variables and secretsWhere a value comes from, why it was overridden, and how secrets are encrypted.
- Operations at scaleRolling updates without downtime, slow runs over hundreds of hosts, and testing roles before production.
Ansible fundamentals questions
| Question | What it tests | What a strong answer shows |
|---|---|---|
| What does agentless mean in Ansible? | Architecture | Nothing runs permanently on managed hosts. The control node connects over SSH (or WinRM or SSH for Windows), copies small module programs, runs them and removes them. Most modules need Python on the target. |
| What is an inventory? | Inventory | The list of hosts and groups Ansible manages, written in INI or YAML, or produced by a dynamic inventory plugin that queries a cloud provider. Groups can contain other groups. |
| Playbook, play, task and module: how do they relate? | Structure | A playbook is a YAML file with one or more plays. Each play maps a group of hosts to an ordered list of tasks, and each task calls one module with arguments. |
| What is idempotence? | Core principle | Running the same playbook twice leaves the system in the same state, and the second run reports no changes. Modules check the current state before acting. |
| What is a handler? | Change-driven actions | A task that runs only when another task notifies it and reports a change, such as reloading nginx after its config changed. Handlers run once, after the play's tasks finish by default, however many tasks notified them. meta: flush_handlers runs them earlier. |
| Ad hoc command versus playbook? | Usage | An ad hoc command runs one module against hosts from the command line, useful for quick checks. Anything repeatable belongs in a playbook under version control. |
What does become do? | Privilege | Runs tasks with escalated privileges, by default with sudo as root. Set it per play or per task instead of running everything as root. |
| What are facts? | Discovery | Information gathered from each host at the start of a play: OS, IP addresses, memory and more, available as variables. Turn gathering off when you do not need it to save time. |
| What are roles and collections? | Reuse | A role packages tasks, handlers, templates, files, defaults and variables in a standard folder layout. A collection is a distributable bundle of roles, modules and plugins, installed with ansible-galaxy. |
Playbook questions
- name: Run the database migration once, before the rollout
hosts: web[0] # first host of the group only
become: true
tasks:
- name: Migrate
ansible.builtin.command: /opt/app/bin/migrate
args:
creates: /opt/app/.migrated # skipped if the migrate script left this marker
- name: Configure web servers
hosts: web
become: true
serial: 2 # rolling: two hosts at a time
vars:
nginx_port: 8080
tasks:
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Deploy nginx config
ansible.builtin.template:
src: nginx.conf.j2 # uses {{ nginx_port }}
dest: /etc/nginx/nginx.conf
mode: "0644"
validate: nginx -t -c %s # check the rendered file before it replaces the old one
notify: Reload nginx
handlers:
- name: Reload nginx
ansible.builtin.service:
name: nginx
state: reloaded- Why are `command` and `shell` tasks not idempotent by default? Ansible cannot know what an arbitrary command changes, so it reports changed every time. Use a proper module when one exists, or add
creates,removesorchanged_whenso the task reports the truth. - `command` versus `shell`?
commandruns a program directly without a shell, so pipes, redirects and variables like$HOMEdo not work, which is safer.shellruns through a shell and needs careful quoting. - Why is the migration in its own play instead of a task with `run_once`? With
serial,run_onceruns once per batch, not once per play, so the migration would run again for every pair of hosts. A separate play aimed at one host runs it exactly once, before the rollout starts. - What does `validate` on the template task do? It runs a command against the rendered temporary file and only replaces the real file if the command succeeds, so a broken config never reaches nginx. It suits complete files; a fragment such as a single include usually cannot be checked on its own.
- How do you run a playbook without changing anything?
--checkfor a dry run, with--diffto see file changes. Modules that do not support check mode are skipped or report what they would do. - How do you run only part of a playbook?
--tagsand--skip-tagsselect tasks,--limitnarrows hosts, and--start-at-taskresumes after a failure. - What do `register`, `when` and `failed_when` do?
registersaves a task's result,whenmakes a later task conditional on it, andfailed_whendefines failure by your own rule rather than the exit code. - `import_tasks` versus `include_tasks`? Imports are static: processed when the playbook is parsed, so tags and conditions apply to each imported task. Includes are dynamic: evaluated at run time, so the file name can come from a variable or a loop, but tags on the include do not reach the tasks inside unless you pass them through.
- How do you repeat a task over a list?
loopwith the list, referring toiteminside the task. For modules that accept a list directly, such as package names, pass the list once instead, which is much faster. - What is `delegate_to` for? Running a task on a different host on behalf of the current one, such as removing a server from a load balancer before patching it.
Variables, precedence and Vault questions
| Question | What it tests | What a strong answer shows |
|---|---|---|
| Where can variables be defined? | Variables | Role defaults, inventory, group_vars and host_vars folders, play vars, vars_files, role vars, registered results, set_fact and extra vars on the command line. |
| How does variable precedence work, at a high level? | Precedence | Role defaults are the weakest and meant to be overridden. Inventory and group variables sit in the middle, more specific hosts beat groups, and play and role vars beat inventory. Extra vars passed with -e always win. When unsure, check with the debug module. |
| Why put tunable values in role defaults? | Role design | Defaults have the lowest precedence, so users of the role can override them from inventory or the play without editing the role. |
| How do you keep secrets in Ansible? | Vault | Encrypt files or single values with ansible-vault, commit the encrypted text, and supply the password at run time from a file, a prompt or a script that reads a secrets manager. Vault IDs allow different passwords per environment. |
| How do you stop secrets from showing in output? | Logging | Set no_log: true on tasks that handle them, and avoid printing them with debug. Encrypted variables are decrypted in memory during the run. |
| What is Jinja2 used for in Ansible? | Templating | Rendering templates and expressions: {{ variable }}, filters such as default and to_json, loops and conditionals in config files. |
ansible-vault create group_vars/prod/vault.yml # new encrypted file
ansible-vault edit group_vars/prod/vault.yml # edit in place
ansible-vault encrypt_string 'example-value' --name 'db_password' # one value for a vars file
ansible-playbook site.yml --vault-id prod@prompt --check --diff --limit webSenior Ansible and comparison questions
| Question | What it tests | What a strong answer shows |
|---|---|---|
| How do you update a web fleet without downtime? | Rolling updates | serial to update a few hosts at a time, max_fail_percentage to stop early, delegate_to to drain each host from the load balancer first, and a health check before it goes back in. |
| A playbook takes an hour across 500 hosts. How do you speed it up? | Performance | Raise forks, turn on SSH pipelining, skip or cache fact gathering, replace loops of single package installs with one task, and consider the free strategy when hosts do not need to stay in step. |
| How do you test roles? | Quality | ansible-lint for style and common mistakes, Molecule to run the role against containers or VMs, an idempotence check (a second run with zero changes), and a check-mode run against staging. |
| Terraform or Ansible: which would you use, and for what? | Tool choice | Terraform for provisioning cloud resources, because it keeps state and plans changes to the infrastructure graph. Ansible for configuring what runs inside machines and for procedural operations. Many teams use Terraform to create servers and Ansible to configure them. |
| Can Ansible provision cloud resources too? | Nuance | Yes, through cloud collections, but it has no state file, so it cannot easily notice resources removed from your code or show a full plan. That is why it is usually paired with Terraform for this. |
| Push or pull: how does Ansible compare with agent-based tools? | Architecture | Ansible pushes from a control node when you run it; agent-based tools such as Puppet or Chef pull configuration on a schedule. Ansible can pull with ansible-pull, and teams often schedule runs from CI or a controller such as AWX. |
| How do you manage Windows hosts? | Platforms | Over WinRM or SSH with the Windows collections, which use PowerShell on the target instead of Python. Modules have their own names, such as those for services and features. |
Fictional example
- Interviewer
- A colleague's playbook shows several tasks as changed on every run even when nothing should change. How would you find the cause?
- Candidate
- I would run it twice with
--diffand look at which tasks report changes on the second run. Usually it is one of three things. Acommandorshelltask with nocreatesorchanged_when, so Ansible always says changed; I would replace it with a module or add a real check. A template with something that differs every run, such as a timestamp. Or a file whose mode, owner or line endings do not match what the task asks for, so it gets rewritten each time. The goal is a second run with zero changes, and I would add that check to the role's tests so it stays that way.
A one-week Ansible practice plan
- Days 1 and 2: create two or three local VMs or containers, write an inventory with groups and run ad hoc commands against them.
- Day 3: write a playbook that installs and configures a web server with a template and a handler; run it twice and get zero changes on the second run.
- Day 4: turn it into a role with defaults, override a value from
group_vars, and trace precedence with the debug module. - Day 5: move a password into Vault and run the playbook with a vault ID.
- Days 6 and 7: practice the rolling update, performance and Terraform comparison answers out loud.
For the rest of the loop, see DevOps engineer interview questions. Ansible runs are often triggered from a pipeline, which is covered in Jenkins interview questions and CI/CD interview questions.
When you explain a rolling update, the details you skip are the ones interviewers probe. Natively, an AI interview assistant for Mac and Windows, transcribes your mock automation interviews on your computer so you can spot missing steps such as draining a host first. During practice it can read a playbook or a failed task's output from a screenshot and suggest a fix, and with Pro or the free trial it can use the résumé and job description you add. Afterwards you have the transcript and notes to go through.
Questions people ask
What Ansible questions are asked in interviews?
The agentless SSH model, inventory and groups, playbooks, tasks and modules, roles and collections, idempotence, handlers, variable precedence, Ansible Vault, rolling updates with serial, testing roles, and Ansible versus Terraform.
What does idempotent mean in Ansible?
Running the same playbook again leaves the system unchanged and reports no changes, because each module checks the current state before acting. command and shell tasks need creates, removes or changed_when to behave that way.
What is the difference between Ansible and Terraform?
Terraform provisions infrastructure and keeps a state file so it can plan changes. Ansible configures systems and runs procedures without keeping state. Many teams create servers with Terraform and configure them with Ansible.
How are secrets stored in Ansible?
With Ansible Vault: encrypt whole files or single values, commit the encrypted text, and provide the vault password at run time from a prompt, a file or a script. Use no_log on tasks that handle secrets.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
