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
Use the tree command to have a glance at the directory structure of the role.
# tree apache
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
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
In our example, we are going to install the role 5KYDEV0P5.skydevops-mysql.
# ansible-galaxy install 5KYDEV0P5.skydevops-mysql
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
Additionally, you can visit Ansible Galaxy via your web browser and manually search for roles for performing various tasks as outlined by the dashboard.
For example, to search for a monitoring role such as elasticsearch, click on the ‘Monitoring’ option and search for the role as shown.
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.
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
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.