Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Roles in Ansible

What are Roles?

In Ansible, roles are a way to group tasks, handlers, variables, and other components into a reusable and organized structure. Roles are designed to simplify the management of complex playbooks and make them more modular and maintainable. By using roles, you can reuse code across multiple playbooks and projects, which helps to keep your Ansible code DRY (Don't Repeat Yourself).

Role Directory Structure

A role has a specific directory structure that Ansible expects. This structure includes several subdirectories, each serving a specific purpose. Here is what a basic role directory structure looks like:

my_role/
├── tasks/
│   └── main.yml
├── handlers/
│   └── main.yml
├── templates/
│   └── my_template.j2
├── files/
│   └── my_file.txt
├── vars/
│   └── main.yml
├── defaults/
│   └── main.yml
├── meta/
│   └── main.yml
                

Each of these directories serves a different purpose:

  • tasks/: Contains the main list of tasks to be executed by the role.
  • handlers/: Contains handlers, which are tasks that are triggered by the "notify" directive in other tasks.
  • templates/: Contains Jinja2 templates that can be used in tasks.
  • files/: Contains files that can be deployed by the role.
  • vars/: Contains variables that are specific to the role.
  • defaults/: Contains default variables for the role.
  • meta/: Contains metadata about the role, including its dependencies.

Creating a Simple Role

Let's create a simple role named my_role that installs and starts the Apache web server. First, we need to create the role directory structure:

mkdir -p my_role/{tasks,handlers,templates,files,vars,defaults,meta}
                

Next, we'll add a task to install Apache in the tasks/main.yml file:

# my_role/tasks/main.yml
- name: Install Apache
  apt:
    name: apache2
    state: present
    update_cache: yes

- name: Start Apache
  service:
    name: apache2
    state: started
    enabled: yes
                

Using the Role in a Playbook

Now that we have created our role, we can use it in a playbook. Here is an example playbook that uses the my_role role:

# site.yml
- hosts: webservers
  roles:
    - my_role
                

You can run this playbook with the following command:

ansible-playbook site.yml
                
PLAY [webservers] ***********************************************************

TASK [Gathering Facts] *******************************************************
ok: [webserver1]

TASK [my_role : Install Apache] **********************************************
changed: [webserver1]

TASK [my_role : Start Apache] ************************************************
changed: [webserver1]

PLAY RECAP *******************************************************************
webserver1                 : ok=3    changed=2    unreachable=0    failed=0
                

Conclusion

In this tutorial, we introduced roles in Ansible and discussed their benefits in terms of code reusability and organization. We also covered the basic directory structure of a role and demonstrated how to create a simple role to install and start Apache. Finally, we showed how to use this role in a playbook. By using roles, you can keep your Ansible projects organized and maintainable, making it easier to manage complex configurations and deployments.