Skip to main content

Command Palette

Search for a command to run...

Overview of ANSIBLE

Updated
8 min read
Overview of  ANSIBLE
A

I'm a DevOps magician, conjuring automation spells and banishing manual headaches. With Jenkins, Docker, and Kubernetes in my toolkit, I turn deployment chaos into a comedy show. Let's sprinkle some DevOps magic and watch the sparks fly!

What is Configuration Management?

Configuration management in terms of Ansible means that it maintains the configuration of the product performance by keeping a record and updating detailed information that describes an enterprise’s hardware and software.

Which domain do they use Ansible?

In IT companies, infrastructure domain support teams, Linux systems

What kind of configuration management support does this tool provide?

This tool is useful for the configuration of servers, application deployment, and continuous testing of installed applications

What is Ansible?

Ansible is an open-source IT configuration management, deployment, and orchestration tool. You can provide a variety of automation challenges with this. You can manage 10,000 to 20,000 servers. This tool is useful for managing complex multi-tier IT applications, automating tasks, and managing multiple servers

You need to have 3 basic knowledge to know about Ansible

  1. Linux commands

  2. How to work with Ansible

  3. Python basics

Interview Perspective: A DevOps Engineer can automate all the repetitive tasks that we do every day using Ansible

Ansible is an open-source Configuration management tool used to automate infrastructure management and configuration. Using Ansible, we can configure hundreds or thousands of servers with a single command.

How Does Ansible Work?

Ansible uses something called a Playbook, which is written in YAML syntax, where you can define all your tasks and configurations that you want to be applied to all the servers.

Along with the playbook, you also need a host file or inventory file, which will contain the IP Addresses of all the servers on which you want to run the playbook

After you have the playbook and defined your servers in the host file, you can run the Ansible command, which will run the playbook on all the servers defined in the host file using SSH.

This is how Ansible can be used to manage and configure multiple servers at once

𝐖𝐡𝐚𝐭 𝐢𝐬 𝐀𝐧𝐬𝐢𝐛𝐥𝐞 𝐫𝐨𝐥𝐞𝐬?
Ansible Roles provides a clear framework and structure for configuring tasks, variables, handlers, metadata, templates, and other files. They allow us to reuse and share our Ansible code efficiently.

Ansible is written in which language?

Mostly Python and PowerShell

Why Ansible?

  • It is a free, open-source application

  • Agent-less need for agent installation and management

  • Python /YAML-based

  • High flexibility and configuration management of systems.

  • A large number of ready-to-use modules for system management

  • Configuration rollback in case of error

  • Very simple to set up and use (No special coding skills are necessary to use Ansible’s playbooks.)

  • Powerful (Ansible lets you model even highly complex IT workflows)

  • Efficient( Because you don’t need to install any extra software, there’s more room for application resources on your server)

  • SSH(Very simple password-less network authentication protocol that is secure. So, your responsibility is to copy this key to the client)

Advantage

  1. Agentless (No special software required on remote machines)

  2. Security

  3. Simplicity (Playbooks written in human-readable YAML)

  4. Modules and plugins

  5. Comprehensive tool network automation

  6. cloud and local infrastructure automation

Architecture of Ansible

The architecture of Ansible consists of several key components that work together to automate and manage IT infrastructure. Here are the components

Control Node: Where Ansible is installed and from which automation tasks are executed.

Inventory: Lists managed hosts (nodes) categorized into groups.

Playbooks: Written in YAML to define tasks and configurations.

Modules: Small programs executed on managed hosts for tasks.

Roles: organize and reuse playbooks and associated files.

Handlers: Special tasks executed when notified by other tasks.

Facts: Variables gathered from managed hosts

Ad-Hoc Commands: For quick one-time tasks from the command line.

Ansible Tower (Optional): A web-based automation orchestration tool for managing Ansible at scale.

Ansible Supported Modules

There are n number of modules, some of which are

  1. Cloud Modules (AWS, Azure, GCP)

  2. Notification Modules (Slack, Email, Hipchat, Webhooks)

  3. Network Modules

  4. Database Modules

  5. System Modules

Basic Ansible-Commands

• ansible - Ansible ad-hoc commands

• ansible-playbook - Run an Ansible playbook

• ansible-vault - Manage encrypted Ansible vars files

• ansible-galaxy - Manage roles using galaxy.ansible.com

• ansible-doc - Show documentation on Ansible commands

• ansible-pull - Pull playbooks from the server

How is Ansible different from other automation tools? (e.g. Chef, Puppet, etc.)

  • Agentless

  • Minimal run requirements (Python & SSH) and simple to use

  • The default mode is "push" (it supports also pull)

  • Focus on simplicity and ease of use

Basic Questions

What is Ansible, and what makes it different from Puppet or Chef?
Ansible is an open‑source automation and configuration management tool that’s agent‑less, using SSH or WinRM to communicate with nodes. Its playbooks are written in YAML, making them more human‑readable.

What are Inventories, and how are they used in Ansible?
Inventories define the hosts and groups of hosts that Ansible manages. They can be static (INI/YAML files) or dynamic (scripts pulling from external sources).

Explain Playbooks, Roles, and Tasks.
Playbooks are YAML files mapping hosts to tasks. Tasks are atomic units of work (e.g., install a package). Roles structure playbooks into organized folders with tasks, variables, templates, etc.

What are Facts in Ansible?
Facts are system metadata collected from managed nodes (like OS, IP, memory), used within playbooks for decision‑making.

How does idempotency work in Ansible?
Ansible modules are designed to be idempotent—running the same playbook multiple times yields the same result without unintended changes.

What is Ansible Vault used for?
Ansible Vault encrypts sensitive data like passwords and API keys within playbooks or variable files.

What are Ansible Modules, and how do copy vs. template differ?
Modules perform specific tasks. The copy module transfers files as‑is, while the template uses Jinja2 templating for dynamic file generation.

Explain ad‑hoc commands.
These are one‑off commands executed directly without playbooks, useful for quick tasks.


Intermediate Questions

How do you set up a jump host in Ansible to reach servers without direct access?
Use ansible_ssh_common_args in your inventory or playbook, for example:

ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p user@jumphost"'

This ensures Ansible routes SSH traffic through the designated jump server.


What are ad‑hoc commands, and when would you use them?
Ad-hoc commands are quick, and one-line commands are executed without a playbook. They're ideal for tasks like checking uptime or restarting services:

ansible all -m shell -a "uptime"

How do you dynamically access a variable or host information within a playbook?
Use Jinja2 templating with hostvars or vars. For example:

{{ hostvars[inventory_hostname]['ansible_eth0']['ipv4']['address'] }}

This retrieves the IP address of the current host’s interface.


Explain what handlers are and when they execute.
Handlers are special tasks in Ansible that only run when notified by other tasks. They're typically used for actions that should only happen if there's a change, like restarting a service after a configuration file is updated


What’s the difference between dot notation and bracket notation for accessing variables?

  • Dot notation (var.key) is concise but can't handle special characters.

  • Bracket notation (var['key-name']) is safer and supports dynamic keys, making it better for looping and nested data structures.


How do you implement loops in Ansible playbooks?
You can use loop, with_items, or even Jinja2 loops in templates. For example:

- name: Install packages
  apt:
    name: "{{ item }}"
    state: present
  loop:
    - git
    - curl

In templates, you might write:

{% for host in groups['web'] %}
  ...
{% endfor %}

How is set_fact different from static variable declarations (like vars or defaults)?

set_fact is used to define or update variables dynamically during playbook execution, with values that may depend on earlier tasks. In contrast, static variables (vars, defaults) are defined before playbook execution begins.


How do callback plugins work, and how can you configure them?
Callback plugins modify Ansible’s output or behavior (e.g., logging, profiling) and can run automatically during playbook execution. To enable one, configure ansible.cfg like so:

[defaults]
callback_whitelist = profile_tasks

Custom plugins should be placed in a callback_plugins folder.


How do you use tags to control playbook execution?
You can add tags to tasks or roles like this:

- name: Install nginx
  apt:
    name: nginx
    state: present
  tags: install

Then run only tagged tasks with:

ansible-playbook playbook.yml --tags install

Advanced Questions

What are Ansible Collections?
Collections are packaged sets of roles, modules, and plugins, distributed via Ansible Galaxy for modular automation.

What is Ansible Automation Platform (AAP) 2?
AAP 2 is Red Hat’s enterprise-grade solution for scalable automation, incorporating AWX (open‑source Tower), execution environments, analytics, and more.

What is Ansible Navigator?
A command‑line tool introduced to run and debug playbooks with improved logging and support for containerized environments.

How do you optimize performance in large environments?
Strategies include enabling pipelining, using asynchronous tasks, dynamic inventory, and caching facts.

What are execution environments?
Containerized environments that include dependencies (Python version, modules), ensuring consistent execution across systems.

How does Ansible fit into modern CI/CD pipelines?
Ansible integrates with tools like Jenkins or GitLab to automate provisioning, configuration, and deployment, using event‑driven automation and collections.

How do you handle errors in playbooks?
Use ignore_errors, failedwhen, and structured blocks like block, rescue, and always for robust error handling.

What is the Ansible.cfg file?
It’s the main configuration file controlling inventory location, SSH defaults, role paths, and other behaviors.

How to use the synchronize module?
A wrapper around rsync, it efficiently syncs files or directories between the control node and managed hosts.

Describe Tower or AWX features.
Provides a web UI, role‑based access control, job scheduling, dashboards, and REST APIs for higher‑level management.

Delegating tasks with delegate_to.
Enables running a task on a different host than the target host. Useful for tasks like gathering data from a central server.

Configuration drift—how to manage it?
Enforce the desired state by regularly running playbooks, using CI/CD, and monitoring.

Multi‑tier application automation?
Use separate roles/playbooks for database, app, and web tiers to ensure consistency across environments.

More from this blog

Ashwin's Blog

108 posts