- Devops Diaries
- Posts
- Ansible - Interview Q&A
Ansible - Interview Q&A
Here are some of the most commonly asked Ansible interview questions and answers to help you prepare:

1. What is Ansible?
Answer:
Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It is agentless, meaning it does not require any software to be installed on managed nodes. It uses SSH for communication and YAML-based playbooks for automation.
2. How does Ansible work?
Answer:
Ansible works by:
Connecting to managed nodes via SSH (Linux) or WinRM (Windows).
Executing tasks defined in YAML-based Playbooks.
Using modules to perform actions such as installing packages, copying files, and managing services.
Maintaining an inventory file to list managed nodes.
3. What are the key components of Ansible?
Answer:
Inventory: Defines the list of managed nodes.
Playbooks: YAML files that define automation tasks.
Modules: Predefined functions used to perform tasks (e.g., file, yum, service).
Tasks: Steps defined in playbooks to execute actions.
Handlers: Triggered when specific tasks change the system state.
Roles: Used for organizing playbooks into reusable components.
4. What is an Ansible Playbook?
Answer:
An Ansible Playbook is a YAML file that defines a set of automation tasks to be executed on remote nodes.

5. What is an Ansible Inventory file?
Answer:
An inventory file defines the managed nodes (hosts) and groups

The inventory file can be static (INI format) or dynamic (retrieved from cloud services like AWS).
6. What are Ansible modules?
Answer:
Modules are reusable scripts that Ansible uses to perform automation tasks. Examples include:
System modules:
service
,user
,file
Package modules:
yum
,apt
,pip
Cloud modules:
aws_s3
,gcp_compute_instance
Database modules:
mysql_db
,postgresql_user
Example: Installing Nginx using a module:

7. How is Ansible different from Puppet and Chef?

8. What is become
in Ansible?
Answer:become
allows a user to execute tasks with elevated privileges (sudo/root).
Example: Running a task as root:

9. How do you test Ansible Playbooks?
Answer:
Dry run (Check Mode):
ansible-playbook playbook.yml --check
Syntax check:
ansible-playbook playbook.yml --syntax-check
Run on a test environment before production
10. What is Ansible Galaxy?
Answer:
Ansible Galaxy is a community-driven repository for sharing Ansible roles. You can download pre-built roles using:
ansible-galaxy install geerlingguy.apache
11. What is the difference between copy
and template
modules?

12. How do you encrypt sensitive data in Ansible?
Using Ansible Vault to encrypt secrets like passwords: ansible-vault encrypt secrets.yml
To edit an encrypted file: ansible-vault edit secrets.yml
To run a playbook with encrypted data: ansible-playbook playbook.yml --ask-vault-pass
13. What is an Ansible Role?
An Ansible Role is a way to structure Playbooks into reusable components. A role follows this directory

14. How do you handle errors in Ansible?
Ignore errors:

Use failed_when:

15. What is the async
parameter in Ansible?
Answer:
The async
keyword allows tasks to run asynchronously. Example:

Check the status using ansible-playbook --check
Reply