DevOps Bootcamp · Lesson

The Role Directory Anatomy

tasks, handlers, templates, defaults, vars.

Lesson 2 of 413 steps

The Role Directory Anatomy is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

A Role Is a Folder Tree

A role is just a directory with a fixed set of subfolders. Ansible knows each folder by name and loads its contents automatically.

roles/
  nginx/
    tasks/
    handlers/
    templates/

tasks/main.yml Is the Entry Point

The tasks/main.yml file holds the role's main list of tasks. It is the first thing Ansible runs when the role is applied.

tasks/main.yml:
- name: Install nginx
  ansible.builtin.package:
    name: nginx

handlers/ Holds Restart Logic

The handlers folder defines actions like restarting a service. Tasks notify these handlers so restarts happen only on change.

templates/ for Jinja2 Files

Put your .j2 template files in templates/. The template module finds them there by name with no full path needed.

templates/
  nginx.conf.j2

files/ for Static Files

The files folder stores plain files you copy as-is. The copy module locates them automatically, just like templates.

defaults/ Sets Low Priority Vars

The defaults/main.yml file holds variables with the lowest precedence, so users can easily override them from outside the role.

defaults/main.yml:
nginx_port: 80

vars/ Sets High Priority Vars

The vars/main.yml file holds variables with higher precedence. Use it for values the role relies on that should rarely change.

meta/main.yml Describes the Role

The meta/main.yml file lists the role's author, license, supported platforms, and any role dependencies.

main.yml Is the Magic Name

Inside tasks, handlers, vars, and defaults, Ansible automatically loads the file named main.yml. That naming is the convention that ties it together.

You Only Need What You Use

A role does not need every folder. If it has no templates, just skip the templates directory. Ansible ignores missing optional folders.

tests/ and README for Polish

Mature roles add a README and a tests folder so others understand and verify your automation before using it.

Quick Check

Recall where a role's main task list lives.

Recap

A role is a folder tree where tasks, handlers, templates, files, defaults, vars, and meta each have a fixed home, all keyed off main.yml. 📁

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “The Role Directory Anatomy” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “The Role Directory Anatomy”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “The Role Directory Anatomy”?

tasks, handlers, templates, defaults, vars. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “The Role Directory Anatomy” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Roles: From Monolith to Modules
  2. The Role Directory Anatomy
  3. Scaffold a Role with ansible-galaxy init
  4. Apply Roles & Pass Role Variables
← Back to DevOps Bootcamp