Table Of Contents

Back to Labs Content

  • CI/CD
  • DevOps
  • Automation

Complete Guide to Ansible: Modern Server Configuration Automation

Sunday, June 15, 2025 at 6:03:22 PM GMT+8


In today's infrastructure management landscape, efficiency, speed, and consistency are essential. One of the most popular and flexible configuration automation tools available today is Ansible. This article explores the urgency of using Ansible, its definition, how it works, and provides a practical tutorial to get you started quickly.

Why is Ansible Needed?

When managing multiple servers, manual configuration such as logging in one by one and executing scripts becomes tedious, inconsistent, and error-prone. Tasks like:

- Installing software on 10+ servers

- Copying configuration files

- Ensuring environments are identical

...are exhausting when done manually.

The solution: Automation. And that’s where Ansible steps in as a lightweight, simple tool that doesn’t require any agent installed on the servers.

What is Ansible?

Ansible is an open-source IT automation engine developed by Red Hat. It automates cloud provisioning, configuration management, application deployment, and many other IT needs.

It enables you to write simple, human-readable YAML scripts called playbooks to automate the setup and maintenance of systems, reducing the need for repetitive manual work.

Key Benefits of Ansible:

1. Agentless: No need to install software on managed nodes. It uses SSH by default.

2. Simple syntax: Written in YAML (Yet Another Markup Language), playbooks are easy to write and understand.

3. Idempotency: Re-running the same playbook won't change anything if the system is already in the desired state.

4. Modular: Use modules to perform tasks like installing packages, copying files, restarting services, etc.

How Does Ansible Work?

Ansible follows a simple architecture:

1. Control Node: This is the machine where Ansible is installed and from which you run commands (typically your local machine or a dedicated automation server).

2. Managed Nodes: These are the target machines that Ansible configures. They don’t require any agent—just SSH access.

3. Inventory File: A file that defines which hosts Ansible will manage, grouped for specific tasks.

4. Playbooks: YAML files that define tasks to execute on target hosts.

5. Modules: Ansible provides hundreds of built-in modules that handle tasks like package management, file manipulation, and service management.

Execution Flow:

- You run a command like ansible-playbook -i inventory playbook.yml from the control node.

- Ansible reads the inventory to determine target hosts.

- It connects to each host over SSH using the user defined in the inventory.

- It then runs the tasks described in the playbook using appropriate modules.

- Output is streamed back, showing success, changes, or errors.

This architecture eliminates the need for complex client-server setups and makes Ansible ideal for simple to moderately complex environments.

Hands-On Ansible Tutorial

1. Install Ansible (on your local machine)

Ubuntu/Debian

sudo apt update
sudo apt install ansible -y

macOS (Homebrew)

brew install ansible

After installation, confirm the ansible and ansible-playbook commands are available:

which ansible
which ansible-playbook

Both should return a valid path if installed correctly.

2. Create an SSH Key for Passwordless Login

Run the following on your local machine:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

- Press ENTER to use the default location (~/.ssh/id_rsa)

- Leave the passphrase empty for fully automated access

Copy the public key to the target server:

ssh-copy-id user@<TARGET_IP>

Example:

ssh-copy-id vagrant@192.168.56.10

If ssh-copy-id is not available, do it manually:

cat ~/.ssh/id_rsa.pub | ssh user@<TARGET_IP> 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'

Test the connection:

ssh user@<TARGET_IP>

If you connect without being prompted for a password, it’s working.

3. Set Up Your Ansible Project Structure

Organize your Ansible files in a dedicated project folder:

mkdir my-ansible-project
cd my-ansible-project

Inside this folder:

my-ansible-project/
├── inventory       # List of target servers
└── playbook.yml    # The automation instructions (YAML)

File: inventory

[web]
192.168.56.10 ansible_user=vagrant

The inventory file defines the groups and IP addresses of your target servers. The ansible_user specifies which user to SSH into the server as.

Note: The inventory file does not require any file extension (like .ini or .yaml). You can name it anything (e.g., myhosts, prod_inventory). What matters is that you provide the filename when using the -i option with ansible-playbook.

File: playbook.yml

- name: Install nginx on web server
  hosts: web
  become: yes
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

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

This playbook tells Ansible to connect to all servers in the web group and perform the listed tasks with root privileges (via become: yes).

4. Execute the Playbook

To run the playbook, use the ansible-playbook command:

ansible-playbook -i inventory playbook.yml

Explanation:

- i inventory: Specifies the inventory file (list of target hosts)

- playbook.yml: The YAML file with instructions to execute

If successful, you'll see task-by-task output. The target server will now have NGINX installed.

Verify by visiting:

http://192.168.56.10

Ansible in CI/CD Pipelines (e.g., Jenkins Integration)

Once you're comfortable running Ansible manually, it becomes even more powerful when integrated into automated CI/CD workflows.

With Jenkins, for example, you can:

1. Trigger Ansible playbook execution after code is pushed to Git

2. Use Jenkins pipelines to call Ansible commands for deployment

3. Automate full delivery pipelines — from code change to deployment

Sample Jenkins pipeline step:

sh 'ansible-playbook -i inventory deploy.yml'

You can also use ansible-vault in combination to protect secrets and credentials in your playbooks.

This approach combines the strengths of Jenkins (pipeline orchestration) and Ansible (server configuration) into a robust, automated infrastructure management workflow.

Conclusion

Ansible makes server configuration fast, consistent, and repeatable. With simple YAML files and secure SSH-based execution, you can manage tens or hundreds of servers efficiently. This is just the beginning — advanced topics like roles, handlers, variables, and CI/CD integration offer even more power. Start automating your infrastructure today with Ansible!


Another Recommended Labs Content

DeploymentDistributed SystemsMicroservicesDevOps

From Kubernetes Overload to Observability: How the Sidecar Pattern in Service Mesh Saves the Day

Kubernetes, a popular tool, helps manage these pieces by running them in containers and ensuring they’re available and scalable. But as your application grows, Kubernetes alone can’t handle everything. Let’s break this down and see how the sidecar pattern in a service mesh comes to the rescue, making your system easier to monitor, secure, and manage.

In the fast-paced world of modern software development, delivering high-quality applications quickly is no longer optional—it's essential. This is where Jenkins steps in as a game-changer. Imagine having a virtual assistant that tirelessly builds, tests, and deploys your code, ensuring every update you make reaches production seamlessly.