0Pricing
Ansible Academy · Lesson

The Role Directory Anatomy

tasks, handlers, templates, defaults, vars.

The Role Directory Anatomy is a free Ansible Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 📁

Frequently asked questions

Is the “The Role Directory Anatomy” lesson free?

Yes — the full text of “The Role Directory Anatomy” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.

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

tasks, handlers, templates, defaults, vars. You practise Ansible Academy 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 Ansible Academy?

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

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 Ansible Academy lesson?

Yes. Every Ansible Academy 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 Ansible Academy