Ansible Roles Capabilities

Hi, guys! In the previous publication we talked about such a configuration management tool as Ansible and benefits that it can give you. But among all listed advantages it is necessary to mention separately the opportunities provided with one more Ansible concept - with a role. The proper organization of roles will not only simplify your work with scripts, improving their structure and further support, but also eliminates duplication of tasks in playbooks.

A role is a way of splitting Ansible tasks into files which are easier to manipulate with. Each role allows you to automate a particular element of the environment. In other words, it gives you a level of abstraction which can be used to organize the whole playbook's structure in the best way, breaking it apart into smaller logical parts. Each of them presents a related set of tasks, variables and handlers, necessary for its implementation. Instead of accumulating tasks in immense playbooks, that expand in the process of the application development, you can group all the content according to the entities and work they are connected to. For example, describing required behavior for an application, it may be convenient to separate jobs responsible for configurations of its users, remote storages, dependent software installation and so on.

Roles are designed to be reusable. Organizing playbooks with roles allows you to reuse different modules and avoid code duplication. Repetitive configuration steps, carried out into separate files, can be used many times, simply by including the required role functionality in your playbooks when necessary. Instead of dealing with the role tasks every time, you can feel the benefit of the encapsulation that a role gives you. With the proper organization of tasks, with time you will generally forget about the configuration of your applications - any necessary functionality will always be at hand so that the only think you need is just to pick up an existing role and add it to your webservers.

Moreover, a great number of work on creating roles is already done. You are able to download existing Roles from Ansible Galaxy or any of numerous open sources. Thanks to the huge Ansible community, there is a significant base of written roles that continues to grow actively. It contains roles for any taste and needs you can meet during the development process. You can also implement behavior of you own roles and share them with other users. Remember that you are able to find and download our Ansible roles from Galaxy.

Creating a role, you should follow a specific directory structure:

  • files - contains files that will be copied to the configured hosts; Also, it can contain scripts that will later be run on hosts;

  • handlers - handlers that will be used to perform tasks;

  • meta - defines meta data of the role such as a description, supported platforms, a minimum required Ansible version, related tags and existing dependencies on other roles;

  • templates - defines templates which can be deployed via the role.

  • tasks - specifies a list of tasks of the role.

  • vars - contains variables of the role.

Riter development team