Ansible, an open-source configuration management and orchestration tool, provides us with an excellent role feature to help manage our playbooks more efficiently. But what if we store the roles in different locations than the default location?
When you get to the point of creating more complex Ansible configurations, you are likely to find yourself splitting them into different roles and are stored across different directories.
Luckily, we have the role_path variable in Ansible that allows us to define multiple locations for our roles which can make the playbooks more flexible and adaptable across multiple projects.
Before diving into the tutorial, ensure that you have the following:
- Installed Ansible
- Basic knowledge of Ansible playbooks and tasks
Ansible Role_Path Variable
The roles_path configuration specifies the directories that Ansible looks in for roles. Think of it like the $PATH variable in Linux where the system looks in multiple directories for executables.
By default, Ansible checks the roles in /etc/ansible/roles. However, we can customize this by defining the roles_path in the “ansible.cfg” file or using the -roles-path command-line parameter.
Using the Ansible Configuration File
To set the role_path in the Ansible configuration file, edit the “ansible.cfg” file and add the entry as follows:
roles_path = /path/to/your/roles:/another/path/to/roles
The directories are separated by colons. Ansible now looks in all the provided directories for roles when we reference them in a playbook.
Using the Command Line
In other cases, we may want to specify the role path in a command. We can use the command syntax as follows:
You learned how to work the Ansible role_path parameter. Understanding and utilizing the roles_path can provide flexibility in structuring your projects and allows for more efficient role-sharing and reuse.