Ansible roles refer to bundling automation definitions to make it easy to reuse and maintain. However, there are instances where specific roles depend on other functions to function correctly.
In this tutorial, we will explore the functionality of Ansible role dependencies, what they are, why they are essential, and how to define and use them.
Ansible Role Dependencies
Like other tools and utilities that depend on libraries or modules, Ansible roles might rely on different roles to run the specified instructions.
Ansible role dependencies allow one role to use the functionality of another, either by including its tasks or handlers.
Defining the Role Dependencies
In Ansible, we can define the dependencies in the role’s meta/main.yml file under the “dependencies” keyword.
Example 1: Ansible Role Dependency
Let us demonstrate how we can create and use the Ansible role dependency.
Suppose we have a role called “webserver” that depends on another role called “firewall” to ensure that the ports that we require are open.
The meta/main.yml for the webserver role looks similar to the one in the following:
dependencies:
- role: firewall
As you can guess, this tells Ansible that the webserver role requires the firewall role to run properly.
Example 2: Parameterized Dependency
We can also pass the parameters to the roles that we depend on. Let us say that the firewall requires a list of port to open. We can pass the parameters as follows:
dependencies:
- role: firewall
vars:
open_ports:
- 80
- 443
In this case, we define a variable that passes port 80 and port 443 as the parameters of the firewall role.
How to Use the Ansible Depedency Roles
Before another role can depend on another, the dependee must exist first. In the case of webserver and firewall roles, the firewall role must exist first.
You can use the ansible-galaxy command to create the roles as follows:
$ ansible-galaxy init webserver
Once we created the role, we can define the tasks that we wish to accomplish in the main role.
For example, in firewall/tasks/main.yml, we can define the tasks as follows:
- name: Open ports
firewalld:
port: "{{ item }}"
permanent: true
state: enabled
with_items: "{{ open_ports }}"
The next step is to specify the dependent roles in the meta/main.yml file as shown in the previous example.
Finally, you can use the main role in your playbook as follows:
- hosts: webservers
roles:
- webserver
Once we run the previous playbook example, Ansible executes the tasks from the firewall role and then move on to the tasks in the webserver role.
Using the Role Dependencies from Ansible Galaxy
The Ansible Galaxy is a repository for Ansible roles if you are unfamiliar with it. If you have an available role in the Ansible Galaxy that you wish to use, you can specify it using the namespace as follows:
dependencies:
- geerlingguy.firewall
However, ensure to install it with the ansible-galaxy command:
Conclusion
We learned about the functionality of Ansible role dependencies. We covered how to create and use them, how to pass the parameters, and more. Using the skills that we learned here, you can introduce a robust and modular way of reusing the code, ensuring consistency, and maintaining modularity in Ansible tasks.