Home > Storage > PowerMax and VMAX > Storage Admin > Dell PowerMax: Ansible Modules Best Practices > Creating and Using Ansible Roles
Previous sections have looked at playbooks as a list of tasks that run a single long playbook. Ansible provides several different ways to organize your tasks to make them reusable and easy to maintain. One way to do this is by using Ansible roles. This section gives a brief summary of how to create and use roles. Ansible documentation about roles provides more detail.
A role in Ansible is basically a well-defined playbook that is designed to be shared and reused.
Roles are created with the Ansible-galaxy init role-name command. This command creates a default directory structure for your role. That structure can then be fleshed out with content.
An Ansible role has a defined directory structure with up to eight main standard directories. You must include at least one of these directories in each role. You can omit any directories the role does not use. Figure 45 shows the creation of a new role and the default directories created.
At a minimum, the role should have the main.yml file filled out in the tasks directory. This .yml file is similar to a playbook: Tasks are run in order. Unlike a playbook, the tasks/main.yml file is simply a list of tasks. The tasks keyword is not needed because the role structure has defined that this file will contain tasks. If you are using anchors and hooks to group variables, you must add a set_fact statement, as shown in Figure 46. The full file is on GitHub here. Any variables that the playbook uses should be called out in the README.md file with examples. Default values can also be set in the defaults/main.yml file.
Once the role is complete, you can include it in a playbook, as shown in Figure 47. All the functionality available in the role is available with a single include_role statement in the playbook. One or more roles can be run as part of the playbook. Ansible will look for roles in a roles directory set in the Ansible.cfg file or in the same directory in which the playbook is stored.
Roles can also include handler tasks, which are tasks that will be performed upon successful completion of all the tasks in tasks/main.yml in the playbook. A storage provisioning role could include handlers to rescan the server for new storage devices and update any service tickets to note that the role has been completed.
Once you have a library of roles, you might want to share them with your team or upload them to Ansible Galaxy. Store the roles on a version control system; doing so will also align with CI/CD methodologies, where roles can be validated. Sample roles for PowerMax are available on the Dell GitHub.