Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Role Directory Structure in Ansible

Introduction

In Ansible, roles are a way to group tasks, handlers, variables, and other files into reusable units. This makes it easier to manage and organize your Ansible code, especially as your projects grow in complexity. Understanding the directory structure of a role is crucial for effectively utilizing roles in Ansible.

Basic Role Directory Structure

An Ansible role has a specific directory structure. Each directory within a role serves a distinct purpose. Below is a basic example of a role directory structure:

my_role/
├── defaults/
│   └── main.yml
├── files/
├── handlers/
│   └── main.yml
├── meta/
│   └── main.yml
├── tasks/
│   └── main.yml
├── templates/
├── tests/
│   ├── inventory
│   └── test.yml
├── vars/
│   └── main.yml
└── README.md

Directory and File Explanations

Each directory and file in the role structure has a specific purpose:

defaults/main.yml

This directory contains the default variables for the role. These variables have the lowest priority and can be overridden by other variables.

files/

This directory is used to store files that can be deployed by the role. These files can be referenced in tasks using the copy or template module.

handlers/main.yml

Handlers are tasks that are triggered by other tasks. They are defined in this file and can be used to restart services, for example.

meta/main.yml

This file contains metadata about the role, such as dependencies on other roles.

tasks/main.yml

This is the main file where the tasks for the role are defined. Tasks are the actions that Ansible will execute on the managed nodes.

templates/

This directory is used to store Jinja2 templates. These templates can be used to dynamically generate configuration files based on variables.

tests/

This directory contains files for testing the role. The inventory file defines the hosts, and the test.yml file contains the test playbook to validate the role.

vars/main.yml

This directory contains variables that have a higher priority than those in the defaults directory. These variables can still be overridden by variables defined in the playbook or inventory.

README.md

This file contains documentation about the role, including how to use it and what variables it accepts.

Example Role

Let's create a simple role named webserver to install and start an Apache web server. The directory structure will look like this:

webserver/
├── defaults/
│   └── main.yml
├── handlers/
│   └── main.yml
├── meta/
│   └── main.yml
├── tasks/
│   └── main.yml
└── vars/
    └── main.yml

defaults/main.yml

---
http_port: 80

handlers/main.yml

---
- name: restart apache
  service:
    name: apache2
    state: restarted

meta/main.yml

---
dependencies: []

tasks/main.yml

---
- name: install apache
  apt:
    name: apache2
    state: present
  become: yes

- name: write the apache config file
  template:
    src: httpd.conf.j2
    dest: /etc/apache2/sites-available/000-default.conf
  notify: restart apache

- name: start apache
  service:
    name: apache2
    state: started
  become: yes

vars/main.yml

---
server_name: "localhost"

Conclusion

Understanding the role directory structure in Ansible is essential for writing clean, reusable, and maintainable code. By organizing your tasks, handlers, variables, and other components into a well-defined structure, you can simplify your automation tasks and make your playbooks more efficient. The example role provided demonstrates a basic implementation of this structure, which can be expanded as needed for more complex scenarios.