Unlock the Power of Automation with Ansible

Rohit Kumar
4 min readJul 26, 2024

Introduction

In today’s fast-paced world, managing multiple servers and applications can be a daunting task for a developer. This is where automation tools like Ansible come into play. Ansible helps automate repetitive tasks, making it easier to manage large-scale environments efficiently.

What is Ansible?

Ansible is an open-source automation tool that simplifies tasks like:

  • Configuration Management: Keeping software configurations consistent across servers.
  • Application Deployment: Installing and updating applications.
  • Task Automation: Performing routine tasks automatically.

Unlike some other tools, Ansible doesn’t require any special software to be installed on the computers it manages (known as nodes). Instead, it uses standard SSH (Secure Shell) connections to communicate with them. This makes Ansible easy to set up and use.

Why Use Ansible?

  • Simple and Easy to Learn: Ansible uses YAML (Yet Another Markup Language) for its playbooks (scripts), which are easy to read and write.
  • Agentless: You don’t need to install any additional software on your nodes.
  • Idempotent: Running the same playbook multiple times will produce the same result, avoiding unintended changes.
  • Scalable: Ansible can manage everything from a few servers to thousands of them efficiently.

Key Concepts

Inventory

The inventory is a list of servers (or nodes) that Ansible will manage. This list can be as simple as a text file with the names or IP addresses of your servers.

Example:

[webservers]
web1.example.com
web2.example.com

[databases]
db1.example.com

Playbook

A playbook is a YAML file that contains instructions on what tasks to perform on your nodes. Think of it as a recipe that Ansible follows.

Example:

- hosts: webservers
tasks:
- name: Install Nginx
apt:
name: nginx
state: present

Tasks

Tasks are individual actions that Ansible performs on the nodes. Each task uses a module to do something specific, like install a package or start a service.

Modules

Modules are like tools in a toolbox. Each module performs a specific function, such as installing software, managing files, or controlling services.

Getting Started with Ansible

Prerequisites

Before you start using Ansible, you need:

  • A control machine (this can be your laptop or a server where you install Ansible).
  • Managed nodes (the machines you want to manage with Ansible).

Installation

On macOS:

  1. Install Homebrew if you haven’t already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

2. Install Ansible:

brew install ansible

On Ubuntu/Debian-based systems:

  1. Update your package index:
sudo apt update

2. Install Ansible:

sudo apt install ansible

Creating Your First Playbook

Let’s create a simple playbook to install and start Nginx (a popular web server) on a web server.

  1. Create an Inventory File:

Create a file named inventory.ini and add the following content:

[webservers]
web1.example.com

Replace web1.example.com with the hostname or IP address of your web server.

2. Create a Playbook:

Create a file named playbook.yml and add the following content:

- hosts: webservers
become: yes
tasks:
- name: Ensure Nginx is installed
apt:
name: nginx
state: present

- name: Ensure Nginx is running
service:
name: nginx
state: started

This playbook does two things:

  • It installs Nginx if it is not already installed.
  • It ensures that the Nginx service is running.

3. Run the Playbook:

Run the following command to execute the playbook:

ansible-playbook -i inventory.ini playbook.yml

Ansible will connect to the servers listed in the inventory file and perform the tasks specified in the playbook.

Using Roles

Roles in Ansible allow you to group tasks, variables, files, templates, and modules into reusable units. This makes your playbooks easier to manage and reuse.

  1. Create a Directory Structure for the Role:

Create the following directory structure for an Nginx role:

roles/
nginx/
tasks/
main.yml
handlers/
main.yml
files/
templates/
vars/
main.yml

2. Define Tasks in the Role:

Create the file roles/nginx/tasks/main.yml and add the following content:

- name: Ensure Nginx is installed
apt:
name: nginx
state: present

3. Define Handlers in the Role:

Create the file roles/nginx/handlers/main.yml and add the following content:

- name: Restart Nginx
service:
name: nginx
state: restarted

4. Define Variables in the Role:

Create the file roles/nginx/vars/main.yml and add the following content:

nginx_port: 80

5. Update the Playbook to Use the Role:

Modify playbook.yml to use the Nginx role:

- hosts: webservers
become: yes
roles:
- nginx

6. Run the Playbook:

Run the playbook as before:

ansible-playbook -i inventory.ini playbook.yml

Advanced Features

Ansible provides several advanced features to help you manage complex environments more effectively:

  • Variables: Use variables to customize your playbooks. Variables can be defined in the playbook, in inventory files, or in separate variable files.
  • Conditionals: Apply tasks based on conditions. For example, you can run a task only if a certain variable is set.
  • Loops: Repeat tasks multiple times with different inputs. This is useful for tasks like creating multiple users or installing multiple packages.
  • Handlers: Run tasks in response to changes made by other tasks. For example, restart a service if its configuration file is modified.

Conclusion

Ansible is a powerful and flexible automation tool that can help you manage your infrastructure more efficiently. Its simple syntax and agentless architecture make it easy to adopt and scale across your environment. We can leverage Ansible to automate a wide range of tasks, improving efficiency and consistency in your operations and also to save some hassle ;) like I did by automating the setup of my laptop.

Thanks for reading ;)

Rohit Kumar is a passionate software evangelist. Who loves implementing, breaking and engineering software products. He actively engages on platforms such as LinkedIn, GitHub, & Medium through email.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response