Understanding and Setting up Ansible Roles [Tutorial]


The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

In this tutorial, we will show you how to set up and use Ansible roles. We’ll also discuss what a role is, why it’s important in your infrastructure and where should you be using them. Lastly, we’ll cover some best practices for managing your ansible roles as well as a few examples of different ways that Roles can be used!

Ansible roles are a new way to organize your playbooks. Roles can be used for many different purposes, but it’s most commonly used to manage application servers. This tutorial will teach you how to set up and use Ansible roles.

Understanding and Setting up Ansible Roles [Tutorial]

Ansible roles are an excellent alternative for deploying a consistent set of settings to numerous nodes using Ansible. Ansible roles enable you to reuse common setups, which speeds up and simplifies deployment.

This tutorial will teach you all you need to know about Ansible Roles and how to set them up!


This guide will walk you through Ansible roles step by step. If you want to join in, make sure you have the following items:

  • A host for Ansible controllers – Ansible v2.9.24 will be used in this course on an Ubuntu 18.04.5 LTS computer. By executing Ansible —version, you may verify your Ansible version.

Ansible Installation Guide (Ubuntu, RHEL, CentOS, macOS)

  • You can write playbooks and execute them on managed nodes using a user account on the Ansible controller server.
  • A distant computer on which to execute instructions.

High-level ansible roles

When companies have hundreds of apps or configuration sets to maintain, they quickly realize that they need to simplify the process. Ansible roles may be beneficial.

Roles let you to automatically load relevant variables, files, tasks, handlers, and other packages for a shared application or group. Ansible saves roles in various directories, with each role including typical folders based on the application’s needs.

The APP1 Ansible role file structure is shown here, including usual folders such as tasks, handlers, and so on.

Viewing Ansible Role Structure's Diagram Diagram of the Ansible Role Structure

Setting up the Ansible Role File Structure in Tomcat

That’s enough theory! Let’s get started setting up an Ansible role using the demo. The ansible-playbook tool will be used to install Apache Tomcat on a remote server in this example.

Ansible Windows Playbooks: Creating and Running

1. Log in to the Ansible controller host using SSH.

2. Create a directory in your home directory called /ansible role demo and set it as the working directory. The demo files for this lesson may be found in the /ansible role demo directory.

/ansible role demo/mkdir /ansible role demo/cd

3. Next, within the /ansible role demo directory, execute the command below to create a new directory called roles. The Tomat role that you need to deploy is located in the /ansible role demo/roles directory.

By default, Ansible searches for roles in one of two places: the roles/ directory inside the playbook directory, or the /etc/ansible/roles directory. Declare the pathways using the – role: argument in the playbook if you want to store roles at separate paths.

4. Change to the /ansible role demo/roles directory (cd roles) and create the folders needed by the role using the instructions below.

The p option instructs the mkdir command to construct the tomcat parent directory as well as the directories tasks, handlers, defaults, vars, and templates. Every Ansible role shares each of these folders. To deploy the Tomcat role, these files will ultimately include a main.yml.

Within the role directory, you may name the role anything you like.

cd tomcat/tasks,handlers,defaults,vars,templates mkdir -p

Each folder serves a distinct function:

  • tasks: A role’s task directory provides a list of tasks that it must complete. All of the tasks should be specified in the task directory’s main.yml file.
  • handlers: Handlers are similar to regular tasks in Ansible, except they only execute if the job has a “notify” directive. Handlers are specified in the main.yml file and may be used anywhere inside a role or even outside of it.
  • If you need to declare any plugins or modules, such as Python code, you must do so in the library directory’s main.yml file.
  • files: If you need to transfer files from the Ansible controller host to other nodes, make sure they’re all in the files folder.
  • templates: Templates used by the role. The dynamic values are presented as variables in Ansible. A template is a file that includes all of your configuration settings.

Related:How to Save Configuration Time with Ansible Templates

  • vars: The vars directory includes all of the variables you’ll need in the tasks directory’s main.yml file. Variables are also specified in the vars directory’s main.yml file.
  • defaults: The defaults directory contains the variables that the role needs to run.
  • role dependencies are stated in the meta directory if they exist.

5. Finally, use the tree command to check that the roles directory has all of the essential folders.

Checking the files in the roles directory Checking the files in the roles directory

Ansible Role for Tomcat Configuration

Next, add all of the files and code for the Tomcat Ansible role, which will be used to deploy Tomcat on the target node.

1. Copy/paste the following code into the /ansible role demo/roles/tomcat/tasks/main.yml file.

The script below installs the Tomcat Java dependencies, as well as downloads, installs, and configures Tomcat to start up automatically on the node on which it is executed.

—- # Java installation (Open Jdk) – name: apt: name=openjdk-8-jdk Install Java 1.8 # Adding the group “tomcat” – name: add group “tomcat” # Adding the user tomcat – name: user=”tomcat” user=”tomcat” user=”tomcat” user=”tomcat” user=”tomcat” user home=/usr/share/tomcat group=tomcat createhome=no sudo become method: true # Tomcat package download – name: Download Tomcat dest: “tomcat download location” get url: “tomcat download url” url: “tomcat download url” # Tomcat directory creation – name: Make a directory for Tomcat: /usr/share/tomcat/path owner: tomcat group: tomcat state: tomcat # Unarchiving the tomcat archive – name: Unarchive the tomcat archive “tomcat download location” is the source. extra opts: “—strip-components=1” dest: /usr/share/tomcat owner: tomcat group: tomcat remote src: yes /usr/share/tomcat/bin is created. # Copy tomcat service file template: templates/tomcat.service.j2 (source) tomcat.service in /etc/systemd/system when ansible service mgr is equal to “systems” # On the target node, start and enable the Tomcat service. daemon reload: yes – name: Start and enable Tomcat service state: tomcat name: tomcat enabled to begin: yes when systemd == ansible service mgr

2. Create a new file called /ansible role demo/roles/tomcat/vars/main.yml and put the following code into it.

While executing the playbook, Ansible role selects the variables tomcat download url and tomcat download location with provided values from /ansible role demo/roles/tomcat/tasks/main.yml.

http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.61/bin/apache-tomcat-7.0.61.tar.gz tomcat download url /usr/share/tomcat/apache-tomcat-7.0.61.tar.gz tomcat download location

3. Finally, copy/paste the following code into the template file /ansible role demo/roles/tomcat/templates/tomcat.service.j2.

The following template has many sections:

  • The name of the service you need to deploy, which is Tomcat, is defined by unit.
  • Service — In the service section, you must specify the kind of forking service. Forking the Tomcat service enables you to launch it as a system startup and keep it running in the background.
  • Environment – Environment includes a list of environment variables that Tomcat needs to execute, such as JAVA HOME.
  • Install — The Install portion determines whether or not the given Unit is activated. WantedBY activates the specified unit, which starts Tomcat automatically when the computer boots up.

When you run the playbook, the tomcat.service.j2 template is displayed. The data from the template is subsequently transferred to the /etc/systemd/system/tomcat.service directory’s target node.

[Unit] Description=Tomcat After=network.target [Service] User=tomcat Type=forking Group=tomcat Environment=JAVA HOME=/usr/lib/jvm/java-8-openjdk-amd64 Environment=CATALINA HOME=/usr/share/tomcat Environment=CATALINA BASE=/usr/share/tomcat Environment=CATALINA PID=/usr/share/tomcat/temp/tomcat.pid ExecStart=/usr/share/tomcat/bin/startup.sh ExecStop=/usr/share/tomcat/bin/shutdown.sh [Install] WantedBy=multi-user.target

Using the Tomcat Ansible Role to Run the Ansible Playbook

You’ve successfully configured the Ansible role file structure, as well as the files and folders inside the Tomcat Ansible role. However, until you execute the playbook, the role you previously established is useless.

Let’s get the Ansible role up and running soon!

1. Create a YML file with the name you choose and put the code below into it. The file is titled /ansible role demo/tomcat-setup.yml in this example.

The code below deploys the Ansible role (tomcat) that you defined previously to the target server IP ( with admin access for the remote user (ubuntu).

There are three methods to utilize roles: The roles option is available at the play level. With the include role and import role options at the task level.

– Tomcat deployment’s name remote user: ubuntu roles: playbook hosts: – tomcat

2. Use the tree command to check that all needed files are in the /ansible role demo/ directory, as you did before.

All of the important points should be visible in the picture below. The handlers, tasks, and vars folders all have yml files.

Verifying the ansible role demo folder's files Verifying the ansible role demo folder’s files

3. Finally, run the ansible-playbook command to deploy the Tomcat Ansible role to the server address specified in the playbook’s hosts section (

ansible-playbook tomcat-setup.yml

The Tomcat Ansible role has been successfully installed on the remote host, as seen below.

Putting the Ansible Playbook into Action Putting the Ansible Playbook into Action

Apache Tomcat Web Page Validation

How can you tell whether Apache Tomcat is up and running? A default web page is supplied to test that Tomcat is operating properly and that the service has been launched.

To access the Apache Tomcat default web interface, open a web browser and navigate to <Remote-server:8080>. You’ll see a similar page to the one shown below.

Apache Tomcat Web Page Validation is Running Apache Tomcat Web Page Validation is Running


You learnt how to create an example Ansible role and deploy it using Ansible in this lesson. Instead of spinning the wheel every time, Ansible roles may help you avoid code duplication and deploy groups of settings.

Which application do you intend to install next utilizing an Ansible Role now that you have this newfound knowledge?

Ansible is a powerful automation tool that allows users to manage servers. It can be used for configuration management, application deployment, and more. This tutorial will help you understand how to use Ansible roles with the “ansible-galaxy init” command.

Related Tags

  • ansible role example
  • ansible roles vars
  • ansible galaxy tutorial
  • ansible roles directory structure
  • ansible roles example github

Table of Content