How to Create and Download Roles on Ansible Galaxy and Use Them – Part 9

In Part 9 of Ansible Series, you will learn how to create and download roles on Ansible Galaxy and use them. Ansible is a simple yet effective configuration management & automatic deployment tool that seamlessly automates complex tasks in an efficient manner. You can manage hundreds or even thousands of servers from a single control node using a single playbook file.

However, writing playbooks for managing the same service in different environments can be quite cumbersome and this usually leads to code redundancy. Additionally, more complexity can add to the difficulty in managing all the devices.

In comes roles. In Ansible, roles are used for breaking down playbooks into reusable files that can be used across several other instances where the need arises to perform a similar task. This eliminated the need for rewriting playbooks over and over again and saves a great deal of time and energy.

Roles are simply functionalities of playbooks. A role ships with pretty much what would constitute a playbook: Tasks, files, modules, variables, and templates. Also, note that each role is limited to a particular task or the desired output.

Creating an Ansible Role

To create a role in Ansible, simply use the syntax.

# ansible-galaxy init role_name 

Multiple directories and files will be created in your current working directory. In this case, I have decided to create a role in the /etc/ansible/roles directory.

Let’s create a role called apache.

# ansible-galaxy init apache
Create Apache Role in Ansible
Create Apache Role in Ansible

Use the tree command to have a glance at the directory structure of the role.

# tree apache
Check Directory Structure of Ansible Role
Check Directory Structure of Ansible Role

As you can see, several directories have been created, however, not all of them will be used in the playbook.

Now, to use your newly created role in a playbook, define a task in the main.yml file contained in the tasks directory of your new role.

/apache/tasks/main.yml

---

- hosts: database_servers

  tasks:

    - name: Install Apache2 on Ubuntu webserver
      apt:
         name: apache2
         state: installed

Afterward, create a playbook file and call the role as shown.

--- 
- hosts: webservers
  roles:
   - apache

Installing a Role from Ansible Galaxy

Roles play a crucial role in sharing code with other users in the Ansible community using the Ansible Galaxy platform. In Ansible Galaxy, you get thousands of roles performing different tasks such as the installation of web servers and databases, monitoring tools, etc.

Ansible Galaxy is a database or a repository of Ansible roles that you can leverage in your playbooks and help streamline your tasks.

To search a role in Ansible Galaxy, simply run the command.

# ansible-galaxy search <role>

For example to search for a role named mysql run.

# ansible-galaxy search mysql
Search Role in Ansible
Search Role in Ansible

As you can see, there are hundreds of roles that match the search keyword mysql. However, not all roles will perform what you intend, so its recommended that you read through the instructions carefully.

To gather more information about a role, simply run the Ansible command:

# ansible-galaxy info 5KYDEV0P5.skydevops-mysql
Check Ansible Role Information
Check Ansible Role Information

In our example, we are going to install the role 5KYDEV0P5.skydevops-mysql.

# ansible-galaxy install 5KYDEV0P5.skydevops-mysql
Install Role in Ansible
Install Role in Ansible

The role is downloaded and extracted to the default roles directory located at /etc/ansible/roles.

The role can thereafter be called in a playbook, for example:

---
- name: Install MySQL server
  hosts: webservers

 roles:
    • 5KYDEV0P5.skydevops-mysql

Now you can safely run the Ansible playbook as shown.

# ansible-playbook install_mysql.yml
Run Ansible Playbook
Run Ansible Playbook

Additionally, you can visit Ansible Galaxy via your web browser and manually search for roles for performing various tasks as outlined by the dashboard.

Ansible Galaxy
Ansible Galaxy

For example, to search for a monitoring role such as elasticsearch, click on the ‘Monitoring’ option and search for the role as shown.

Search Monitoring Role in Ansible Galaxy
Search Monitoring Role in Ansible Galaxy

Ansible Galaxy makes it easier for users to install the best roles by listing the most popular and the most downloaded roles. To get more information about a specific role, simply click on it.

Check Role Info on Ansible Galaxy
Check Role Info on Ansible Galaxy

In a playbook, you can also specify more than one role, for example.

---
- name: Install MySQL server
  hosts: webservers

 roles:
    • 5KYDEV0P5.skydevops-mysql
    • Aaronpederson.mariadb

To list the roles installed, simply run.

# ansible-galaxy list
List Installed Roles in Ansible Galaxy
List Installed Roles in Ansible Galaxy
Conclusion

Roles make it quite easy to reuse and share Ansible playbooks. This way they save a user a lot of time trying to write lots of redundant code and spend too much time which would have been used in other system administration tasks. And that’s it for this guide.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.