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.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.