CertGrid CertGrid
Hands-on Lab·Ansible

Ansible Role Creation

A role is a directory layout that Ansible knows how to read: tasks here, templates there, variables in two places with different precedence. ansible-galaxy init creates the skeleton in one command. This fills in the parts that matter, explains the defaults versus vars distinction that decides what callers can change, and deletes the directories you will not use.

Roles and Collections Guide 24 of 45 Intermediate

defaults/main.yml has the lowest precedence of any variable in Ansible, and vars/main.yml has a very high one. That is the entire design: defaults are the role's public interface and are meant to be overridden, vars are internal and are not. Putting a tunable value in vars/ makes it effectively unchangeable by the caller.

A role is built on the control node. Nothing is deployed until the next guide.
Server NameIP AddressOSRolesCPURAMHDD
ANS-CTL01192.168.0.36Ubuntu 26.04 LTSAnsible Control Node2 Core3 GB50 GB

This guide includes

Use this when a playbook has grown past one file. This matters because a role is a directory layout Ansible already knows how to read - so paths inside it are implicit, and defaults is its public interface.

Before you start

  1. The configuration this guide assumes

  2. The skeleton

  3. defaults are the role's public interface

  4. The tasks and a handler

  5. Paths inside a role are implicit

  6. meta describes the role

Official sources