> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# What Is Ansible

> Explains Ansible, an agentless automation tool for configuration management, playbooks, inventories, modules, idempotency, and applying desired state across multiple servers.

What is Ansible?

Ansible is an open-source automation tool for configuration management, application deployment, orchestration, and task automation. Instead of manually repeating the same steps across multiple servers, you declare the desired state (what the servers should look like) and let Ansible apply those instructions consistently and idempotently.

Why do we need it?

Imagine you run the same website on 20 servers. Each server must have Apache installed and the same configuration file. If you configure each machine by hand you duplicate effort and increase the risk of mistakes:

```ini theme={null}
# apache2.conf
Timeout 300
```

One missing or incorrect setting can lead to a single server serving a 404 page while the rest work correctly. To prevent manual drift and ensure consistency, we use configuration automation.

![A diagram showing two versions of shop.example.com — a red 404 error page and a green-bordered working storefront — with arrows pointing to a grid of 20 server tiles, one tile marked as problematic. The caption asks how to apply the required setup across all 20 servers.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/CICD-DevOps/What-Is-Ansible/deploy-fix-to-20-servers.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=a65adca2a54f072502fe3003df566c61)

Define the desired state and apply it

With Ansible you describe the required setup once and apply it to any number of hosts. For the website example you need:

* Apache installed
* The correct `apache2.conf` file in place
* Apache running

Ansible takes these instructions and enforces them on the selected servers.

![A dark-themed diagram titled "Configuration Management" showing a "Required Setup" panel (Apache installed, settings file) feeding into Ansible. Arrows from Ansible point to a dashed group of numbered server nodes (01–20), each with an Apache icon.](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/CICD-DevOps/What-Is-Ansible/configuration-management-ansible-apache-01to20.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=21213eff47c8f0066b6be74091325814)

How Ansible knows which servers to manage

Ansible needs two primary inputs:

1. Inventory — a list (or dynamic source) of hosts to manage.
2. Playbook — a YAML file that describes the tasks to achieve the desired state.

Example inventory (INI-style):

```ini theme={null}
[web]
web01
web02
...
web20
```

Example playbook (install Apache, copy config, ensure service is running):

```yaml theme={null}
- hosts: web
  become: true
  tasks:
    - name: Apache installed
      ansible.builtin.package:
        name: apache2
        state: present

    - name: Settings file
      ansible.builtin.copy:
        src: apache2.conf
        dest: /etc/apache2/apache2.conf

    - name: Apache running
      ansible.builtin.service:
        name: apache2
        state: started
```

Modules and tasks

Each playbook task calls a module — a focused unit that performs a specific action (package installation, file copy, service management, user creation, cloud operations, etc.). Use the appropriate module and provide the required parameters (for example, the package name).

Core Ansible components

| Component | Purpose | Example |
| - | - | - |
| Inventory | Lists hosts to target (static or dynamic) | `inventory.ini` |
| Playbook | YAML instructions describing desired state | `site.yml` |
| Module | Executes a single operation on a host | `ansible.builtin.package` |
| Control node | The machine running Ansible | Your laptop or CI runner |
| Handler | Runs only when notified (useful for reloads/restarts) | `reload apache` |

How Ansible executes tasks on managed servers

Ansible runs on a control node and connects to managed hosts (typically over SSH). It pushes modules to the remote host and executes them there. Ansible is agentless — no persistent agent is required on the managed hosts. Most standard modules expect Python to be present on the target systems.

Ansible is agentless — ensure SSH access and that Python (usually Python 3) is installed on managed hosts. For systems without Python, connection plugins and raw modules can help, but most standard modules assume Python is available.

Idempotency

Ansible is idempotent: running the same playbook repeatedly results in no unnecessary changes if the target already matches the desired state. For example, if Apache is already running on 19 hosts and stopped on one, re-running the playbook will only start Apache on the affected host and leave the others untouched.

Updating configuration and handlers

When you update a configuration file, Ansible copies the new file only where it differs. If Apache needs a reload to apply the new configuration, use a handler and notify it from the task that changes the file. Handlers only run when a notifying task reports a change.

Example task with notification and handler:

```yaml theme={null}
- name: Settings file
  ansible.builtin.copy:
    src: apache2.conf
    dest: /etc/apache2/apache2.conf
  notify: reload apache

handlers:
  - name: reload apache
    ansible.builtin.service:
      name: apache2
      state: reloaded
```

Handlers only run when a task reports `changed`. If a copy results in no difference, the handler will not be executed. Test changes in a safe environment before applying to production.

Running playbooks

Ansible does not continuously watch hosts by default. Apply (or re-apply) desired state by running playbooks manually, via cron, or from a CI/CD pipeline:

```bash theme={null}
$ ansible-playbook -i inventory.ini site.yml
```

Adding or removing servers

To add a host, include it in your inventory and re-run the playbook so the new server receives the same configuration:

```ini theme={null}
[web]
web01
web02
...
web20
web21
```

```bash theme={null}
$ ansible-playbook -i inventory.ini site.yml
```

For dynamic or cloud environments, use a dynamic inventory script or cloud inventory plugins to discover and manage hosts automatically.

Links and references

* [Ansible Documentation](https://docs.ansible.com/)
* [Ansible Best Practices](https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html)
* [Ansible Inventory Guide](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/370000ef-b6bc-4986-8d29-0793ebb2c9e7/lesson/c158c334-f9ab-44c6-b46f-19002f934472)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.