Ansible Roles and Ansible Galaxy

Danuka Praneeth
4 min readMay 20, 2018

--

I already made an introduction to Ansible with my previous publication and this will be a continuation to Ansible roles and Ansible Galaxy.

Ansible Roles

Since Ansible is designed to handle complex and multitier deployments across large number of servers, their should be a proper mechanism to delegate tasks to manage them easily. Here comes the Ansible roles to fulfill that requirement.

A role is an Ansible approach for bundling automation content and making it reusable. It is an arrangement of files in a predefined folder structure that will load certain variable files, tasks, handlers automatically during the process execution. Roles allow you to create very simple playbooks and which then look to a directory structure to identify the actual configuration steps they need to perform. Grouping content of the code in this format will help to manage large pieces of codes in complex deployments in a more user friendly manner. Ansible roles also facilitate the easy sharing and reuse of the scripts with other users.

The first step in creating a role is creating its directory structure. You can save time in creating these directories and main.yml files by using the tool ansible-galaxy which comes bundled with Ansible.

$ ansible-galaxy init role_name

Following is a sample role with the standard directory arrangement created using the above command.

Directory structure of a role

Roles should include at least one of these directories, however it is perfectly fine to exclude any which are not being used. When in use, each directory must contain at-least one main.yml file, which contains the relevant content.

defaults : This directory will contain data about the role which are ideally the default variables for the role.

files : This directory contains regular files which can be deployed by this role. Any scripts to be run on target host or any other files to be transferred to the remote host should be placed in this directory.

handlers : This will contains handlers used by this role. Handlers are tasks which are based on some action or trigger (eg: restart mysql)

meta : This directory can contain files that establish role dependencies. You can list any dependencies or roles that must be applied before the current role can work properly. It will also contain information about role and it’s author (eg: author’s name, company, license details etc)

templates : You can place all dynamic files/templates that use variables which will be substituted during the execution of the role in this directory.

tasks : This directory contains all of the tasks to be executed by the role that would normally be in a playbook. These can reference files and templates contained in their respective directories without using a path.

vars : Variables for the roles can be specified in this directory. Any default variables defined in the default directory will be overwritten by the same variables defined in this folder.

Ansible-galaxy

Ansible Galaxy is the Ansible’s official community hub for sharing Ansible roles. It is a community and a shared resource hub where people can download roles or contribute with your own roles. So, we don’t have to write all the tasks needed for our configuration or the deployment from the scratch. We can download an already encapsulated role from Galaxy community and use them in our playbook. At the same time we can exchange our ideas, get feedback on our roles from other developers or users in this community.

There is a role rating mechanism in the galaxy using the user feedback for each role. So you can find high quality roles which suits your requirement and download it using the following ansible-galaxy command that comes bundled with Ansible.

$ ansible-galaxy install username.rolename

username : name of the role owner
rolename : name of the role

You can find the above respective command for each role under the detailed description of the role in the galaxy .

Below are some useful Galaxy commands you might use from time to time:

ansible-galaxy list displays a list of installed roles, with version numbers
ansible-galaxy remove [role] removes an installed role
ansible-galaxy init can be used to create your own role template for submission to Ansible Galaxy

With this basic understanding on Ansible automation platform, I will discuss some sample codes in the upcoming publications.

--

--

Danuka Praneeth

Senior Software Engineer | BSc (Hons) Engineering | CIMA | Autodidact | Knowledge-Seeker