An Easy Guide to Ansible Roles: A Practical Example

If you want to deploy a web server like Apache or Nginx with Ansible, you can create a playbook outlining the installation and configuration steps. This approach works well for a single web server but can become repetitive and time-consuming if you need to deploy many servers. To streamline the process, you can use Ansible roles to avoid duplicating tasks and make deployment more efficient. To introduce the concept, this post provides a simple example of using Ansible roles.

Understanding Ansible roles

In Ansible, a playbook is like a recipe, outlining the steps needed to achieve a specific outcome. It combines various tasks, such as installing packages, configuring services, and managing files, to accomplish a specific goal. However, as your infrastructure grows, playbooks can become lengthy and complex.

An Ansible role is a self-contained collection of tasks, variables, templates, and other related files organized in a prescribed structure. Think of them as pre-packaged ingredients for your playbook. Roles help modularize your infrastructure code, making it more reusable, maintainable, and easier to understand.

This tutorial will explore deploying an Nginx web server using Ansible, comparing the traditional playbook to an Ansible role.

Prerequisites

To proceed with this tutorial, you will need the following:

  • An Ansible management node with Ansible installed and configured
  • An Ansible target node.
  • SSH key-based authentication between the management node and the target node

Check out our introductory articles in this Ansible beginner’s tutorial, where we explain how to configure the necessary prerequisites.

Ansible playbook approach

Look at the Ansible playbook named nginx.yml that installs and configures Nginx on a target server.

---

- name: Install and configure Nginx

hosts: node1

become: true

tasks:

- name: Install Nginx

apt:

name: nginx

state: present

become: true

- name: Start Nginx service

service:

name: nginx

state: started

enabled: true

This playbook consists of tasks to install Nginx and ensure that the Nginx service is running. While this works, it may become difficult to manage as your infrastructure grows.

Ansible role approach

You can create an Ansible role dedicated to managing Nginx installations to simplify the process. To create a role named we web server, you have to create a directory first:

mkdir webserver

Next, create a tasks directory within the webserver directory.

mkdir webserver/tasks

Now, create a main.yml file to define the tasks required to install and configure Nginx within the tasks directory of the webserver role:

nano webserver/tasks/main.yml

Add the following configurations:

---

- name: Install Nginx

apt:

name: nginx

state: present

become: true

- name: Start Nginx service

service:

name: nginx

state: started

enabled: true

After defining the webserver role, you can now refactor your playbook file to use this role:

nano playbook.yml

Add the following configuration to define your webserver role:

---

- name: Deploy Web Servers

hosts: node1

become: true

roles:

- webserver

The above playbook contains the webserver role that contains the tasks and configuration necessary to set up an Nginx web server on the target host node1. To configure an entire web server using an Ansible role, you would typically have a directory structure that contains necessary components such as tasks, handlers, templates, and variables. But for now, let’s keep things simple in this example.

Run the above playbook to install and configure Nginx on the target server.

ansible-playbook playbook.yml

This command will deploy Nginx with a playbook that contains the webserver role.

Installing and configuring Nginx using roles

By referencing the roles, you’ve reused existing tasks without repetition. More importantly, you can easily change the web server choice in your entire infrastructure for your playbooks by modifying the single web server role.

Subscribe to 4sysops newsletter!

Conclusion

Ansible roles and complex configurations can be abstracted into modular components, simplifying maintenance and reducing the risk of errors. Please note that I only scratched the surface of Ansible roles. The purpose of this post is to introduce the concept of Ansible roles with an example. For more details, please read the Ansible documentation.


Posted

in

, , ,

by

Tags: