# Ansible Roles

In 
Published 2022-12-03

This tutorial explains to you how to use roles in Ansible. This tutorial has an example as well.

Ansible roles are created as a primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse.
Ansible roles contain tasks related to a particular functionality, but this is not mandatory.

Ansible roles could be shared with other programmers. If you want to use a specific task, you can take a look at https://galaxy.ansible.com/home as well. You can find something to reuse.

In order to create a role named "myrole", you must run the following command:

ansible-galaxy init myrole

This, you can see in the image below:

This command creates the following directory structure (if you want you can create this directory structure manually):

In order to add content to a role, you have to create the Ansible playbook and add the roles key word instead task key word:

The tasks are moved to the ./myrole/tasks/main.yml file:

The variables are moved to the ./myrole/vars/main.yml file:

Now you can run the playbook and see the result: