Ansible is one of the most popular tools for configuration management and automation. It appears in interviews for DevOps, Linux administration, cloud and network automation roles. These Ansible interview questions and answers cover the essentials, with short playbook examples.

Basics
1. What is Ansible?
Ansible is an open-source automation tool from Red Hat. It configures servers, deploys applications and orchestrates tasks across many machines using simple YAML files.
2. Why is Ansible called agentless?
It does not need software installed on managed nodes. It connects over SSH to Linux hosts and over WinRM or SSH to Windows hosts, and it needs Python on most Linux targets.
3. Describe the architecture.
- Control node: where Ansible runs.
- Managed nodes: the servers or devices you automate.
- Inventory: the list of hosts and groups.
- Modules: units of work, such as installing a package.
- Playbooks: YAML files that describe tasks.
- Plugins and collections: extensions that add modules and features.
4. What is idempotency?
Running the same playbook several times gives the same result. If the system is already in the desired state, Ansible reports “ok” and changes nothing. Most modules are idempotent, but command and shell tasks need care.
Inventory and variables
5. Show a simple inventory.
[web]
web1.example.com
web2.example.com
[db]
db1.example.com ansible_user=adminInventories can also be YAML, or dynamic, for example generated from AWS or Azure.
6. Where can you define variables?
In inventories, group_vars and host_vars directories, playbooks, roles, included files and the command line with -e. Extra variables on the command line have the highest precedence.
7. What are facts?
Facts are details Ansible gathers about each host, such as its OS, IP addresses and memory. You can use them in conditions and templates, for example ansible_facts['os_family'].
Playbooks
8. Write a short playbook that installs and starts Nginx.
- name: Install and start Nginx
hosts: web
become: true
tasks:
- name: Install Nginx
ansible.builtin.package:
name: nginx
state: present
- name: Start and enable Nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true9. What are handlers?
Handlers run only when notified by a task that changed something. For example, restart Nginx only when its configuration file changes.
10. How do you use templates?
The template module renders Jinja2 files with variables, then copies the result to the host. It is ideal for configuration files.
11. How do loops and conditions work?
Use loop to repeat a task over a list, and when to run a task only if a condition is true.
Structure and reuse
12. What is a role?
A role is a reusable bundle of tasks, handlers, templates, files, variables and defaults with a standard directory structure. You can share roles through Ansible Galaxy.
13. What are collections?
Collections package modules, plugins and roles together, for example community.general or amazon.aws. Since Ansible 2.10, most modules live in collections, so best practice is to use fully qualified names such as ansible.builtin.copy.
Security and troubleshooting
14. How do you protect secrets?
Use Ansible Vault to encrypt variables or files: ansible-vault encrypt secrets.yml. Alternatively, fetch secrets at runtime from a manager such as HashiCorp Vault.
15. How do you test a playbook safely?
Run --syntax-check first, then --check --diff for a dry run that shows what would change. Also use ansible-lint, and test roles with Molecule.
16. How do you debug a failing task?
Increase verbosity with -v to -vvvv, use the debug module to print variables, and limit runs with --limit or --start-at-task.
17. Ad hoc command vs playbook?
Ad hoc commands run one module quickly, for example ansible web -m ping. Playbooks, in contrast, are repeatable, version-controlled automation.
18. Ansible vs Terraform?
Terraform excels at provisioning infrastructure, such as cloud resources. Ansible excels at configuring what runs on it. Many teams use both together.
For related topics, see our DevOps interview questions and shell scripting interview questions.
Keep preparing
Practise each answer out loud, and run the commands in a lab rather than memorising them. Interviewers often ask follow-up questions, so make sure you understand why each answer is true.
Sources
Preparing for an interview? Browse more questions in our interview preparation hub. For businesses, we also offer security services such as VAPT, server hardening and ISO 27001 readiness.
