Getting Started with Ansible: A Hands-On Guide

Table of Contents

Introduction

Ansible is an open-source automation tool that simplifies the configuration management, application deployment, and task automation on multiple servers. It uses a simple syntax written in YAML and requires minimal setup, making it an excellent choice for both beginners and experienced sysadmins. In this hands-on guide, we’ll walk through the basics of Ansible and demonstrate a simple example.

Installation

Before we begin, make sure you have Ansible installed on your control machine. You can install Ansible on Linux using package managers or on other operating systems using Python’s pip. For example, on Ubuntu:

sudo apt update
sudo apt install ansible

Verify the installation by running:

ansible --version

Inventory

Ansible uses an inventory file to define the hosts it manages. Create a file named inventory.ini with the following content:

[web_servers]
web1 ansible_host=192.168.1.101
web2 ansible_host=192.168.1.102

Replace the IP addresses with the actual addresses of your servers.

Configuration File

Create a configuration file named ansible.cfg:

[defaults]
inventory = inventory.ini
remote_user = your_username

Replace your_username with your username on the remote servers.

Writing your First Ansible Playbook

Ansible playbooks are written in YAML and define a set of tasks to be executed on remote hosts. Create a file named deploy_web_app.yml with the following content:

---
- name: Deploy Web App
  hosts: web_servers
  become: true

  tasks:
    - name: Ensure Apache is installed
      apt:
        name: apache2
        state: present

    - name: Copy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html

    - name: Start Apache and enable on boot
      service:
        name: apache2
        state: started
        enabled: true

This playbook does the following:

  1. Ensures Apache is installed.
  2. Copies an index.html file to the web servers.
  3. Starts Apache and enables it to start on boot.

Creating the Directory Structure

Create a directory structure for your Ansible project:

mkdir ansible_project
cd ansible_project

Inside this directory, create the following structure:

ansible_project/
|-- ansible.cfg
|-- deploy_web_app.yml
|-- inventory.ini
|-- files/
|   `-- index.html

Place a simple index.html file inside the files directory.

Running the Ansible Playbook

Run the Ansible playbook using the following command:

ansible-playbook deploy_web_app.yml

Ansible will connect to the specified hosts, execute the tasks defined in the playbook, and report the results.

Conclusion

Congratulations! You’ve successfully written and executed your first Ansible playbook. This is just the tip of the iceberg—Ansible has a rich set of features for managing configurations, orchestrating complex tasks, and more. Explore the official documentation to deepen your understanding and discover more advanced use cases.

Happy automating!

Mustafa Arif
Mustafa Arif
HPC | Cloud | DevOps | AI

My research interests include HPC, Cloud Computing