IT Career Guide
Search

Ansible Interview Questions and Answers (2026)

Code on two monitors in a dark room, representing Ansible automation

Ansible interview questions usually test four things: how Ansible works without agents, how you write playbooks and roles, how you handle variables and secrets, and how you keep runs safe to repeat. This guide collects 32 Ansible interview questions with short, accurate answers and small YAML examples, grouped from basic to scenario-based, so freshers and career switchers can revise in one sitting before a DevOps or cloud interview.

Key takeaways

  • Ansible is agentless: it connects over SSH (or WinRM for Windows) and pushes small modules that run and then clean up.
  • Idempotency means running the same playbook twice leaves the system unchanged the second time, and interviewers almost always ask about it.
  • Roles give a standard folder layout for tasks, handlers, templates, defaults and variables so playbooks stay reusable.
  • Ansible Vault encrypts secrets such as passwords and keys so they can live safely in Git.
  • Be ready to explain when you would pick Ansible over Terraform: Ansible configures servers, Terraform provisions infrastructure.

Basic Ansible interview questions

1. What is Ansible?

Ansible is an open-source automation tool for configuration management, application deployment and orchestration. You describe the desired state of servers in YAML files called playbooks, and Ansible connects to the machines and makes them match that state. It is maintained by Red Hat, which also sells a supported enterprise platform built around it.

2. What does agentless mean in Ansible?

Agentless means you do not install any permanent software on the managed machines. Ansible connects from a control node over SSH for Linux or WinRM for Windows, copies a small module, runs it with Python on the target and removes it. The only requirements on a Linux target are SSH access and a Python interpreter.

3. What is the control node and what are managed nodes?

The control node is the machine where Ansible is installed and from where you run commands and playbooks. Managed nodes, also called hosts, are the servers Ansible configures. The control node must run Linux, macOS or WSL on Windows; it cannot be a native Windows machine.

4. What is an inventory?

An inventory is the list of hosts Ansible manages, organised into groups. It can be a static INI or YAML file, or a dynamic inventory that pulls hosts from a cloud provider such as AWS or Azure at run time.

[web]
web1.example.com
web2.example.com

[db]
db1.example.com ansible_user=ubuntu

5. What is a module?

A module is a unit of work that Ansible runs on a host, such as installing a package, copying a file or starting a service. Examples are ansible.builtin.apt, ansible.builtin.copy, ansible.builtin.service and ansible.builtin.user. Most modules are idempotent, which means they only make a change when the current state differs from what you asked for.

6. What is a playbook?

A playbook is a YAML file containing one or more plays. Each play maps a group of hosts to a list of tasks. Playbooks are how you write repeatable automation, as opposed to one-off ad hoc commands.

- name: Install and start nginx
  hosts: web
  become: true
  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: true

    - name: Ensure nginx is running
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true

7. What is the difference between a play and a task?

A task is a single call to a module, such as installing nginx. A play is a group of tasks applied to a set of hosts, along with settings such as become and variables. A playbook can hold several plays, for example one for web servers and one for database servers.

8. What are ad hoc commands?

Ad hoc commands run a single module from the command line without writing a playbook. They are useful for quick checks, such as pinging all hosts or restarting a service on one group.

ansible all -i inventory.ini -m ping
ansible web -i inventory.ini -b -m service -a 'name=nginx state=restarted'

9. What is idempotency and why does it matter?

Idempotency means applying the same playbook many times gives the same result as applying it once. If nginx is already installed and running, a second run reports ok rather than changed. It matters because you can safely rerun automation after a failure or on a schedule without breaking working servers.

10. What does become do?

become: true tells Ansible to escalate privileges, usually with sudo, to run tasks as root or another user. You can set it for a whole play or for individual tasks, and choose the user with become_user.

Intermediate Ansible interview questions

11. What are handlers?

Handlers are tasks that run only when another task notifies them and reports a change. A common example is restarting nginx only when its configuration file changes. Handlers run once at the end of the play, even if several tasks notify them.

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

  handlers:
    - name: Restart nginx
      ansible.builtin.service:
        name: nginx
        state: restarted

12. What are roles?

A role is a standard way to package tasks, handlers, templates, files, variables and defaults so they can be reused across playbooks and projects. Running ansible-galaxy role init webserver creates the folder structure with directories such as tasks, handlers, templates, defaults and vars, each with a main.yml.

13. What is Ansible Galaxy?

Ansible Galaxy is a public hub for sharing roles and collections, and ansible-galaxy is the command used to install them. Teams often list required roles and collections in a requirements.yml file so everyone installs the same versions.

14. What are collections?

Collections are the distribution format for Ansible content. A collection bundles modules, plugins and roles under a namespace, such as amazon.aws or community.general. This is why modern playbooks use fully qualified names like ansible.builtin.copy.

15. How do variables work in Ansible?

Variables can be defined in many places: inventory, group_vars and host_vars folders, play vars, role defaults and vars, files loaded with vars_files, and on the command line with -e. You reference them with Jinja2 syntax such as {{ app_port }}.

16. What is variable precedence?

When the same variable is defined in several places, Ansible uses a fixed precedence order to decide which value wins. Role defaults have the lowest precedence and extra vars passed with -e have the highest. In interviews it is enough to say that defaults are meant to be overridden, inventory and group variables sit in the middle, and extra vars always win.

17. What are facts?

Facts are details Ansible gathers about each host at the start of a play, such as the operating system, IP addresses, memory and CPU count. They are available as variables like ansible_facts['distribution']. You can turn gathering off with gather_facts: false to speed up plays that do not need them.

18. What is a template in Ansible?

A template is a Jinja2 file, usually ending in .j2, that Ansible renders with variables and copies to the host using the template module. It lets one configuration file work for many servers with different ports, hostnames or settings.

19. How do you use conditionals?

Use the when keyword with an expression. The task runs only when the expression is true.

- name: Install Apache on Debian family
  ansible.builtin.apt:
    name: apache2
    state: present
  when: ansible_facts['os_family'] == 'Debian'

20. How do you write loops?

The recommended keyword is loop, which repeats a task for each item in a list. Inside the task the current value is available as item.

- name: Create users
  ansible.builtin.user:
    name: '{{ item }}'
    state: present
  loop:
    - asha
    - rahul
    - meera

21. What is register used for?

register saves the output of a task into a variable so later tasks can use it, for example to check a command’s return code or stdout before deciding what to do next.

22. What are tags?

Tags label tasks or roles so you can run or skip only part of a playbook. For example, ansible-playbook site.yml --tags config runs only tasks tagged config, and --skip-tags does the opposite.

23. What is Ansible Vault?

Ansible Vault encrypts sensitive data such as passwords, API keys and certificates. You can encrypt whole files with ansible-vault encrypt secrets.yml or single values with ansible-vault encrypt_string, then supply the password at run time with --ask-vault-pass or a password file kept outside the repository.

Advanced and scenario-based Ansible interview questions

24. How do you test a playbook without changing servers?

Run it in check mode with --check, which reports what would change without making changes. Add --diff to see file differences. Note that some modules, such as command and shell, cannot predict their result in check mode.

25. Why should you prefer modules over command and shell?

Modules understand state, so they are idempotent and report changed only when something really changed. The command and shell modules simply run a command every time. If you must use them, add creates, removes or changed_when so the task behaves predictably.

26. How do you handle errors in a playbook?

You can use ignore_errors: true for tasks that are allowed to fail, failed_when to define your own failure condition, and block with rescue and always sections, which work like try, catch and finally.

- block:
    - name: Deploy new release
      ansible.builtin.command: /opt/app/deploy.sh
  rescue:
    - name: Roll back
      ansible.builtin.command: /opt/app/rollback.sh
  always:
    - name: Notify team
      ansible.builtin.debug:
        msg: Deployment step finished

27. How do you do a rolling update across many servers?

Set serial on the play so Ansible updates a few hosts at a time, for example serial: 2 or serial: '25%'. Combine it with max_fail_percentage to stop the rollout if too many hosts fail, and take each server out of the load balancer before updating it.

28. What are forks and strategies?

Forks control how many hosts Ansible works on in parallel; the default is 5, and you can raise it with -f or in ansible.cfg. The strategy decides how tasks move across hosts. The default linear strategy waits for every host to finish a task before the next one, while the free strategy lets each host run as fast as it can.

29. What is delegate_to?

delegate_to runs a task on a different host from the one being processed. A typical use is removing a web server from a load balancer by running the command on the load balancer while the play targets the web server.

30. How would you speed up a slow playbook?

Increase forks, turn off fact gathering when it is not needed, enable SSH pipelining in ansible.cfg, use fact caching, and replace loops of single package installs with one task that passes a list of packages. Also check for slow shell tasks that could be replaced with modules.

31. What is the difference between Ansible and Terraform?

Terraform is mainly used to provision infrastructure such as networks, virtual machines and managed databases, and it keeps a state file of what it created. Ansible is mainly used to configure what runs on those servers and to deploy applications. Many teams use both: Terraform creates the servers and Ansible configures them. Our Terraform interview questions cover the other half of that workflow.

32. How does Ansible compare with Puppet and Chef?

Puppet and Chef traditionally use an agent on each server that pulls configuration from a central server, and Chef recipes are written in a Ruby-based language. Ansible pushes changes over SSH with no agent and uses YAML, which most beginners find easier to read. Interviewers want to hear that you understand push versus pull, not that one tool is always better.

How to prepare for an Ansible interview

  • Build a small lab with two or three Linux virtual machines or containers and practise ad hoc commands and playbooks on them.
  • Write one role from scratch, for example a web server role with a template, a handler and defaults.
  • Encrypt a secret with Vault and use it in a playbook.
  • Be able to explain a real scenario end to end: inventory, playbook, role, variables, handlers and how you verified the result.
  • Revise containers too, since many DevOps roles combine Ansible with Docker. Our Docker tutorial for beginners is a good starting point.

For broader preparation, go through our DevOps interview questions, because Ansible questions are often mixed with CI/CD, Linux and cloud questions in the same round.

Frequently asked questions

Is Ansible easy to learn for freshers?

Yes. Ansible is one of the easier DevOps tools to start with because playbooks are written in readable YAML and there is no agent to install. Basic Linux and SSH knowledge is enough to begin, and most beginners can write useful playbooks within a couple of weeks of practice.

Do I need to know Python to use Ansible?

No. You can write playbooks and roles without writing Python. Python knowledge only becomes useful if you want to write custom modules or plugins, or debug unusual module errors.

Which Ansible topics are asked most in interviews?

The most common topics are agentless architecture, inventory, playbooks, idempotency, handlers, roles, variables and precedence, Vault, and the difference between Ansible and Terraform. Scenario questions about rolling updates and error handling are common for experienced roles.

Is Ansible still worth learning?

Yes. Ansible is widely used for server configuration, patching and application deployment, and it appears in many DevOps and cloud job descriptions. It also pairs well with Terraform and Kubernetes skills.

Explore more guides in our DevOps articles.

Other articles

Radium4 — IT training blog

Practical, plain-English guides for DevOps, cloud, data, Python, testing and security learners.