Register Login

Top Ansible Interview Questions and Answers

Updated May 20, 2025

What Is Ansible And What Does Ansible Do?

Ansible is an open-source tool used for the complete automation of IT environments. It simplifies software deployment, improves scalability, handles OS and application configuration, and enforces security policies.

Explain Architecture Of Ansible?

  • Modules: Small programs that define the state of systems. Executed via SSH and removed after execution.
  • Plugins: Pieces of code that enhance Ansible's core functionality.
  • SSH Keys: Used for secure communication with remote hosts.
  • Inventory: INI or YAML files listing the systems managed by Ansible.
  • Playbooks: YAML files defining the configuration, deployment, and orchestration of systems.

How Ansible Works?

Ansible connects to nodes using SSH, executes tasks through modules, and manages systems listed in the inventory using Playbooks. It doesn’t require agents or SSL certificates.

What Are Ansible Tags?

Tags in Ansible allow running specific tasks within a playbook from the command line, skipping the rest of the tasks.

What Are Ansible Variables?

Ansible variables allow you to store values and use them dynamically within playbooks. They're useful for loops, configurations, and conditional execution.

What Are Ansible Predefined Variables?

Predefined variables are facts collected by Ansible about the managed hosts. They can be accessed using the command: ansible -m setup hostname.

When Variable Is Defined In Ansible?

Variables in Ansible are defined when you need to store a value for reuse in other tasks or systems, similar to other programming languages.

What Is Ansible Tower?

Ansible Tower is a web-based UI and REST API for Ansible. It helps teams manage complex deployments, monitor jobs, and control access.

What Is Ansible Playbook?

Playbooks are YAML files used to define a series of tasks that Ansible executes. They describe the desired system state and are executed sequentially.

How to Use the Same Playbook For Different Environments?

  • Create environment-specific directory structures.
  • Include all necessary folders (e.g., main.yml, roles).
  • Call roles in the playbook.
  • Use inventory and variable files per environment.

What Is the Ansible Inventory?

The inventory is a file that lists hosts and groups of hosts that Ansible manages. By default, it's located at /etc/ansible/hosts.

How Ansible Is Different From Chef & Puppet?

  • Setup: Ansible is easier to set up than Chef and Puppet.
  • Language: Ansible uses YAML; Chef uses Ruby DSL; Puppet uses its own Puppet DSL.
  • Agentless: Ansible is agentless; Chef and Puppet require agents.
  • Platform Support: Ansible supports Windows and Linux/UNIX; Chef and Puppet primarily support UNIX/Linux.

What Is Ansible Register?

Ansible register stores the output of a task in a variable. This output can be used in subsequent tasks or for conditional execution.

Where Ansible Hosts File?

The Ansible hosts file is located at /etc/ansible/hosts. It defines host groups and subgroups.

Which Ansible Module Is Used To Manage Docker Services And Containers?

The docker_service module is used to manage Docker containers and services in Ansible.

What Are Ad-Hoc Commands?

Ad-Hoc commands are one-liners used to quickly perform tasks on managed hosts without writing a full playbook. Example:

ansible web -m ping

Give An Example Of Ansible Pre_Tasks.


pre_tasks:
  - name: Convert data into uppercase
    command: echo 'hello' | tr 'a-z' 'A-Z'
  

Pre_tasks run before all other roles or tasks in a playbook.

How To Speed Up With_Items In Ansible?

To speed up with_items, use the raw module for simple tasks instead of shell or command. Also, use async with poll: 0 to run tasks in parallel.

What Are Handlers In Ansible?

Handlers are special tasks that run only when notified by another task. They are used to trigger actions like restarting a service when a configuration changes.

What Is Ansible Galaxy?

Ansible Galaxy is a repository for finding, reusing, and sharing Ansible roles. You can install roles using the command: ansible-galaxy install username.rolename.

What Are Facts In Ansible?

Facts are system properties collected by Ansible from the managed hosts. These include IP addresses, OS, memory, etc., and are used to make decisions in playbooks.

What Is The Difference Between A Role And A Playbook In Ansible?

  • Playbook: A file containing tasks executed sequentially.
  • Role: A structured way of organizing playbooks and variables to be reused across different playbooks or projects.

Can You Encrypt Sensitive Data In Ansible?

Yes. Ansible provides ansible-vault to encrypt variables, files, or entire playbooks to protect sensitive information like passwords and API keys.

What Is Local Action In Ansible?

local_action is used to run a task on the control node (localhost) instead of the managed host. Useful for tasks like generating configuration files locally.

How To Handle Errors In Ansible?

Use the ignore_errors: yes directive to continue execution despite an error, or use block, rescue, and always to handle exceptions and perform cleanup.

What Is The Use Of Delegate_To In Ansible?

delegate_to allows you to run a specific task on a different host than the one currently being managed. It's useful for centralized logging or control tasks.

What Are Some Common Use Cases For Ansible?

  • Configuration management
  • Application deployment
  • Provisioning cloud infrastructure
  • Security automation
  • Continuous delivery and integration

How Do You Test Ansible Playbooks?

  • Use ansible-playbook --check for dry-run mode.
  • Use Molecule to test roles and playbooks in isolated environments.
  • Use CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run automated Ansible tests.


×