Ansible Roles Tutorial: How to Create and Use Roles in Ansible?
System administrators often have to do repetitive tasks, especially when managing several servers. From installing new applications to configuring them across servers, those tasks take time and are typically prone to errors if done manually. One of the most common ways to optimize those processes is to use tools that automate those tasks, such as Ansible. In this Ansible roles tutorial, we will first briefly explain what roles in Ansible are, and then delve into how to create and use roles, step by step.
#What is Ansible?
Ansible is an open-source software platform for remotely configuring and managing servers. It allows system administrators to manage their infrastructure as code. The Ansible suite includes software provisioning, configuration management, and application deployment functionality. Not surprisingly, Ansible is one of the most in-demand DevOps tools.
#What are Ansible roles?
Ansible roles help maintain complex infrastructure by adding a layer of abstraction to Ansible playbooks. While Ansible provides tasks and playbooks to organize automation flows, they can become difficult to manage on more complex projects. Roles in Ansible are essentially sets of tasks that come with different elements encapsulated in a way that makes them reusable and easily shareable with other Ansible users.
#How to use Ansible roles?
Ansible roles package configuration components in a known file structure. The key components of Ansible roles are:
- Tasks: The steps to be executed.
- Variables: Parameters to customize the role's behavior.
- Ansible Templates : Files to be populated during execution.
- Defaults: Default values for variables.
- Files: Static files to be copied to the target hosts.
- Handlers: Defined actions to be triggered when specific events occur.
- Meta: Metadata such as author information.
As we will see later in this how-to, most of those components correspond to folders in the role file structure. Ansible roles folder structure helps keep roles well organized and maintains a logical separation of different assets like tasks, files, vars, etc.
For example, a role in Ansible can be used to install and configure a database, using predetermined file templates. As the Ansible role itself deals with all the above, this helps simplify playbooks by adding a layer of abstraction. Instead of adding all those configurations to our playbook, we simply add the role. Here is a quick example showing the abstraction:
- name: Install and Configure Postgresql hosts: your_target_servers become: yes roles: - db_postgresql
In this how-to Ansible roles tutorial, you will learn how to create roles in Ansible using the command ansible-galaxy
, we will also cover the basic components of a role, such as templates.
Run your deployments in a scalable and cost-effective open cloud infrastructure. Cherry Servers' secure virtual private servers offer automatic scaling, flexible pricing, and 24/7 technical support. Our pre-defined Ansible module helps you automate the installation and configuration.
#Prerequisites
To follow along with this how-to Ansible roles tutorial, you will need: The latest Ubuntu installed The latest Ansible installed
Note: You can deploy your infrastructure and manage server configuration automatically with a minimal learning curve with Cherry Servers Ansible module.
#How to create roles in Ansible: Step-by-step
In the below steps, we will cover how to create roles in Ansible and how to use Ansible roles to abstract your infrastructure environment. We will create a basic Ansible role that will deploy a dynamic motd
on target hosts. A motd
- Message of the day - is the message displayed on unix-like systems when you log in to them. The role will be executed via a playbook.
Let us start by creating the role folder:
#Step 1: Create the Ansible role folder
We will create a folder where we will keep our roles.
mkdir -p ~/ansible/roles
We will then move to this folder:
cd ~/ansible/roles
#Step 2: Create the Ansible role using Ansible Galaxy
We will now create the role in Ansible. Ansible Galaxy is a central repository for sharing Ansible roles. To create our Ansible roles, we will use the Ansible command ansible-galaxy
. It will create a skeleton file structure for our role, which we will populate with our tasks and other data. We will name the role: dynamic_motd
.
ansible-galaxy init dynamic_motd
You should see the following output:
This will also create a folder named dynamic_motd
. You can list the content of the folder using the ls
command, like the following screenshot:
#Step 3: Define Ansible role default variable
We will now set a welcome message for the motd using a role variable. Low-priority variables are usually defined in the default/main.yml
folder instead of vars/main.yml
as it provides a default variable for this role.
Let us now edit default/main.yml
and add our default variable:
---
motd_message: "Welcome to !"
`` will display the message matching the hostname of the server.
#Step 4: Create a motd template
We will now create a template that will be used by Ansible to generate the dynamic motd file on each target host. Ansible uses Jinja2
as a template engine.
We will create the templates
folder and create a file named motd.j2
in it.
To create the folder, use the following commands inside the dynamic_motd
role folder.
mkdir templates
Create and add the following data to the jinja2
template file motd.j2
, in the templates
folder:
{{ motd_message }}
System information:
OS: {{ ansible_distribution }} {{ ansible_distribution_version }}
Hostname: {{ ansible_hostname }}
IP Address: {{ ansible_default_ipv4.address }}
{{ custom_message | default('') }}
Note that the template contains a custom_message
variable that can be defined in your playbook to display additional content.
#Step 5: Create the role task
Edit the tasks/main.yml
file within the "dynamic_motd" role folder and add the following task to it:
---
# tasks file for dynamic_motd
- name: Copy dynamic motd template
template:
src: motd.j2
dest: /etc/motd
become: yes
The task uses the template
module to copy the motd
template to the /etc/modtd
file on the target host.
#Step 6: Create Ansible Playbook to use the role
Now that we have created our role, we will create a Playbook to execute it.
Create the file dynamic_motd_playbook.yml
in the Ansible folder we created earlier, and add the following lines to it:
---
# tasks file for dynamic_motd
- name: Configure Dynamic motd
hosts: server1
become: yes
roles:
- dynamic_motd
You can replace server1
with the host where you want to configure the motd.
Discover how Tempesta, an open-source application delivery controller (ADC), leveraged Cherry Servers' bare metal cloud to complete tests and validation of their ADC successfully, benefiting from 99.97% uptime, server customization, and 24/7 technical support.
#Step 7: Create an inventory file
We will also create an inventory file inventory.yml
in the ansible folder and add the following line to it:
server1 ansible_host=your_server_ip
#Step 8: Run the Playbook
Now that our role, Playbook, and inventory file have been created, let us run the Ansible Playbook:
ansible-playbook -i inventory.yaml dynamic_motd_playbook.yml
You should see a similar output:
Connecting to server1
will now give us a dynamic motd as configured via the Ansible role.
Also read: How to use 'when' condition in Ansible
#Wrapping up
In this how-to guide, we learned what Ansible roles are and how they help abstract our infrastructure environment. We covered how to create those roles in Ansible and how to use Ansible roles in Playbooks. We have also shown you how to use Ansible templates and variables so that you can better customize your infrastructure configurations. You can find more about Ansible roles in the Ansible community documentation.
Cloud VPS - Cheaper Each Month
Start with $9.99 and pay $0.5 less until your price reaches $6 / month.