Introduction To Ansible

Introduction To Ansible

Sol Vathanak
Sol Vathanak
Member·October 24, 2026·1 ចូលចិត្ត

A Complete Beginner's Guide to IT Automation

Have you ever needed to install the same software or configure the same settings on multiple servers?

Imagine you're working as a DevOps Engineer at a company that manages 20 Linux servers.

Your manager asks you to install Nginx, configure its settings, and ensure the service runs automatically on every server.

Without automation, you might need to:

  • Connect to each server using SSH.
  • Update system packages.
  • Install Nginx manually.
  • Modify configuration files.
  • Start and enable the Nginx service.
  • Verify that everything works correctly.

Now imagine repeating these steps across 20, 50, or even 100 servers.

Sounds time-consuming, right?

Manual server management can lead to configuration inconsistencies, human errors, and deployment delays.

This is where Ansible comes in!

Ansible allows you to automate these repetitive tasks using simple configuration files.

Instead of configuring every server manually, you can write instructions once and execute them across multiple servers.

In this article, we'll explore what Ansible is, why it's important, how it works, and how you can start using it in real-world DevOps projects.


1. What Is Ansible?

Ansible is an open-source IT automation tool used for configuration management, application deployment, infrastructure automation, and system administration.

It allows developers, DevOps engineers, and system administrators to automate tasks across multiple servers from a central location.

Ansible is especially popular because it is relatively easy to learn and generally does not require installing an agent on managed servers.

What Can Ansible Do?

Ansible can automate many common IT operations, including:

  • Installing software packages.
  • Configuring web servers and databases.
  • Managing users and permissions.
  • Deploying applications.
  • Updating operating systems.
  • Managing system services.
  • Configuring network devices.
  • Applying security configurations.
  • Automating cloud infrastructure tasks.

A Simple Example

Suppose you need to install Nginx on an Ubuntu server.

Normally, you might run:

bash
sudo apt update
sudo apt install nginx -y
sudo systemctl enable --now nginx

These commands work well for one server.

But what if you need to install Nginx on 50 servers?

Instead of running the same commands manually, you can create an Ansible Playbook:

yaml
---
- name: Install Nginx on web servers
  hosts: webservers
  become: true

  tasks:
    - name: Install Nginx
      ansible.builtin.apt:
        name: nginx
        state: present

    - name: Start and enable Nginx
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true

You can run this Playbook against all servers in the webservers inventory group.

Ansible will connect to the servers and perform the required tasks automatically.

The main idea: Write once, automate across multiple servers.

“

Important: Ansible is not simply a tool for running commands remotely. It helps you define the desired state of your systems and apply configurations consistently.


2. Why Do DevOps Engineers Use Ansible?

Ansible provides several advantages for modern infrastructure management.

2.1 Automation

Ansible reduces repetitive manual work.

Instead of logging into servers individually, engineers can automate tasks such as:

  • Installing packages.
  • Configuring applications.
  • Managing services.
  • Creating system users.
  • Updating configuration files.

This saves time and reduces the possibility of human error.

2.2 Agentless Architecture

One of Ansible's most important features is its agentless architecture.

For typical Linux server management, Ansible communicates with managed machines using SSH.

You generally do not need to install an Ansible-specific agent on each server.

For Windows systems, Ansible commonly uses WinRM or PowerShell Remoting Protocol (PSRP).

This reduces the operational overhead of maintaining additional agents.

2.3 Easy to Learn

Ansible uses YAML to describe automation tasks.

YAML is a human-readable data serialization format that is easier for many beginners to understand than writing complex automation scripts.

For example:

yaml
- name: Install Git
  ansible.builtin.apt:
    name: git
    state: present

This task simply ensures that Git is installed.

2.4 Consistent Configuration

Imagine managing 30 application servers.

If you configure each server manually, some servers might accidentally have different settings.

Ansible helps maintain consistent configurations across infrastructure.

2.5 Idempotency

Idempotency is an important concept in Ansible.

It means that running the same task multiple times should not create unnecessary changes once the desired state has been achieved.

For example:

yaml
- name: Ensure Nginx is installed
  ansible.builtin.apt:
    name: nginx
    state: present

If Nginx is already installed, Ansible does not need to install it again.

Many Ansible modules are designed to support idempotent operations, although not every task is automatically idempotent.

2.6 DevOps Integration

Ansible can integrate with popular DevOps technologies such as:

  • GitLab CI/CD
  • GitHub Actions
  • Jenkins
  • Docker
  • Terraform
  • AWS
  • Microsoft Azure
  • Google Cloud

These integrations allow teams to automate infrastructure configuration as part of their software delivery workflows.


3. How Does Ansible Work?

Ansible uses a relatively simple architecture.

The three main concepts are the Control Node, Managed Nodes, and Inventory.

3.1 Control Node

The Control Node is the machine where Ansible is installed and executed.

Its responsibilities include:

  • Running Ansible commands.
  • Reading inventory files.
  • Executing Playbooks.
  • Connecting to managed systems.
  • Collecting execution results.

The Control Node typically runs on Linux or another supported Unix-like environment.

Windows users can use a Linux environment such as WSL for Ansible development.

3.2 Managed Nodes

Managed Nodes are the servers or devices that Ansible manages.

Examples include:

  • Ubuntu Server
  • Debian Server
  • Rocky Linux
  • Windows Server
  • Cloud Virtual Machines
  • Supported Network Devices

For many Linux automation tasks, the managed server needs SSH access and a compatible Python environment.

Requirements can vary by module and target device.

3.3 Inventory

An Inventory is a file or data source containing information about the systems Ansible manages.

For example, create a file named inventory.ini:

ini
[webservers]
192.0.2.10
192.0.2.11

[databases]
192.0.2.20

In this example:

  • webservers represents a group of web servers.
  • databases represents a group of database servers.
  • The IP addresses identify the managed hosts.

These are documentation example addresses. Replace them with your own server addresses when practicing.

Ansible Architecture Diagram

flowchart TD
    A["DevOps Engineer"] --> B["Ansible Control Node"]
    B --> C["Inventory"]
    B --> D["Playbook"]
    C --> E["Ansible Execution"]
    D --> E
    E -->|SSH| F["Web Server 1"]
    E -->|SSH| G["Web Server 2"]
    E -->|SSH| H["Database Server"]
    F --> I["Task Results"]
    G --> I
    H --> I
    I --> B

How the Process Works

  1. An engineer creates an inventory and a Playbook.
  2. Ansible reads the inventory to identify target servers.
  3. The Control Node connects to the Managed Nodes.
  4. Ansible executes the configured tasks.
  5. Each server reports its task results.
  6. Ansible displays the execution summary.

The output commonly includes statuses such as ok, changed, failed, and unreachable.


4. Important Ansible Concepts

Before working with Ansible, you should understand its core terminology.

Concept Description
Control Node The machine running Ansible
Managed Node A server or device managed by Ansible
Inventory A list of managed hosts and groups
Playbook A YAML file defining automation workflows
Play A set of tasks applied to specific hosts
Task An individual automation operation
Module A component that performs a specific operation
Role A reusable structure for organizing automation
Handler A task triggered by notifications
Variable A reusable value used in automation
Ansible Vault A feature for encrypting sensitive data

Inventory

An Inventory tells Ansible which systems to manage.

Inventories can organize servers into groups, making it easier to target specific environments.

Playbook

A Playbook describes the tasks Ansible should perform.

It uses YAML syntax and may contain multiple plays.

Module

Modules perform specific automation operations.

Common built-in modules include:

  • ansible.builtin.apt: Manages packages on APT-based systems.
  • ansible.builtin.copy: Copies files to managed hosts.
  • ansible.builtin.service: Manages system services.
  • ansible.builtin.user: Manages user accounts.
  • ansible.builtin.file: Manages files, directories, and permissions.

Role

A Role organizes related Playbooks, variables, templates, tasks, and handlers into a reusable structure.

For example, a team might create separate roles for Nginx, application security, and monitoring configuration.

Handler

A Handler is a special task that runs when another task notifies it.

For example, changing an Nginx configuration file can notify a handler to reload Nginx.

This avoids unnecessary service reloads.


5. What Is an Ansible Playbook?

An Ansible Playbook is a YAML file containing instructions that automate operations on managed systems.

Playbooks help engineers describe what should happen and which servers should receive the configuration.

Here is a basic example:

yaml
---
- name: Configure web servers
  hosts: webservers
  become: true

  tasks:
    - name: Install Nginx
      ansible.builtin.apt:
        name: nginx
        state: present

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

Understanding the Playbook

Parameter Purpose
name Describes a play or task
hosts Specifies the target hosts or groups
become Enables privilege escalation
tasks Defines the operations to execute
ansible.builtin.apt Manages packages on APT-based systems
state: present Ensures a package is installed
state: started Ensures a service is running
enabled: true Enables a service at system startup

This Playbook is intended for Ubuntu or Debian-based servers.

Other Linux distributions may require different package management modules.

6. Ansible vs Terraform: What's the Difference?

Ansible and Terraform are both popular in DevOps, but they primarily solve different infrastructure problems.

Feature Ansible Terraform
Primary Purpose Configuration management and automation Infrastructure provisioning
Language YAML HCL
Common Use Install and configure software Provision servers, networks, and cloud resources
State Management Generally does not require a central state file Uses state to track managed infrastructure
Architecture Typically agentless Provider-based
Example Install Nginx on Ubuntu Create an AWS EC2 instance

Should You Learn Ansible or Terraform?

Both tools are valuable, and they can work together.

For example:

  1. Terraform provisions three cloud virtual machines.
  2. Ansible connects to the new servers.
  3. Ansible installs Docker and configures Nginx.
  4. A CI/CD pipeline deploys the application.
  5. Monitoring tools track the application's health.

Terraform commonly provisions infrastructure, while Ansible commonly configures and automates operations on that infrastructure.

Their capabilities overlap in some areas, so the best choice depends on your requirements.


7. Real-World Example: Ansible in a DevOps Pipeline

Imagine a company developing a Next.js e-commerce application.

The application runs inside Docker containers on Linux servers.

The team wants to automate server configuration and application deployment.

Ansible can help manage:

  • Docker installation.
  • Server configuration.
  • Application directories.
  • Nginx reverse proxy settings.
  • System services.
  • Monitoring agents.

Example CI/CD Workflow

flowchart TD
    A["Developer"] --> B["GitLab Repository"]
    B --> C["GitLab CI/CD"]
    C --> D["Build and Test"]
    D --> E["Build Docker Image"]
    E --> F["Container Registry"]
    C --> G["Deployment Stage"]
    G --> H["Ansible Playbook"]
    H --> I["Configure Target Server"]
    F --> J["Deploy Container Image"]
    I --> J
    J --> K["Production Application"]
    K --> L["Monitoring and Logs"]

How This Workflow Operates

Step 1: Development

A developer creates a new feature and pushes the code to GitLab.

Step 2: Continuous Integration

GitLab CI/CD runs automated checks, tests, and builds.

Step 3: Containerization

Docker packages the application into a container image.

Step 4: Container Registry

The pipeline pushes the image to a container registry.

Step 5: Server Configuration

Ansible ensures that the production server has the required software, directories, and configuration.

Step 6: Deployment

The deployment system pulls and starts the validated container image.

Step 7: Monitoring

Monitoring tools collect logs and performance metrics to help engineers identify problems.

“

Security Reminder: Production automation should use securely managed SSH credentials, protected CI/CD variables, least-privilege permissions, and appropriate deployment approvals.


8. Where Is Ansible Used in Real Life?

Ansible supports a wide range of infrastructure automation use cases.

8.1 Server Configuration Management

System administrators use Ansible to manage operating system settings across multiple servers.

Examples include managing SSH configurations, system users, packages, and services.

8.2 Application Deployment

DevOps engineers use Ansible to prepare deployment environments, distribute configuration files, and automate application releases.

8.3 Cloud Automation

Ansible can interact with supported cloud platforms through modules and collections.

Teams may use it to automate resource management and application configuration in AWS, Azure, and Google Cloud environments.

8.4 Security Automation

Ansible can automate selected security tasks such as user management, configuration enforcement, software patching, and security policy deployment.

8.5 Database Configuration

Teams can use Ansible to install and configure database servers such as PostgreSQL and MySQL.

8.6 Network Automation

Network engineers use Ansible collections and supported connection methods to configure routers, switches, and other network devices.


9. Common Beginner Mistakes

Mistake 1: Running Playbooks Directly in Production

Executing an untested Playbook against production infrastructure can introduce unexpected changes.

Best Practice: Validate syntax, test in a staging environment, and use Check Mode where supported.

Mistake 2: Hardcoding Passwords

Storing passwords, API keys, or authentication tokens directly inside Playbooks creates security risks.

Best Practice: Use Ansible Vault or a suitable secrets management system. Never commit plaintext credentials to source control.

Mistake 3: Using Shell Commands for Everything

Beginners sometimes use the shell or command module for every task.

Best Practice: Prefer purpose-built Ansible modules where possible. They generally provide better state management and idempotency.

Mistake 4: Ignoring Idempotency

Automation that creates duplicate resources or changes settings unnecessarily can become difficult to maintain.

Best Practice: Design tasks around a clearly defined desired state.

Mistake 5: Changing Every Server at Once

Applying major configuration changes across all production servers simultaneously can increase the impact of mistakes.

Best Practice: Use test environments, limited host groups, and rolling execution strategies such as serial when appropriate.

Mistake 6: Not Using Version Control

Without version control, it becomes difficult to track infrastructure configuration changes.

Best Practice: Store Ansible projects in Git repositories and use code review before applying important updates.

Learning Ansible also helps you understand important engineering concepts such as configuration management, infrastructure consistency, desired state, reusable automation, and operational reliability.

However, learning Ansible alone is not enough to become a DevOps Engineer.

You should also develop practical skills in Linux, networking, Git, scripting, CI/CD, containerization, cloud platforms, and monitoring.

ចូលរួមការពិភាក្សា
0 មតិយោបល់រហូតមកដល់ពេលនេះ - ចូលចិត្ត ផ្ដល់មតិ និងចែករំលែករបស់អ្នកនៅលើ Cambostack។
បើកក្នុង Cambostack