0Pricing
Ansible Academy · Lesson

Apply Roles & Pass Role Variables

Include a role and parameterize it.

Apply Roles & Pass Role Variables is a free Ansible Academy lesson on CoddyKit — lesson 4 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.

Apply a Role with roles:

The simplest way to use a role is the roles key in a play. List the role names and Ansible runs each one in order.

- hosts: web
  roles:
    - nginx
    - firewall

Roles Run Before Tasks

Roles listed under roles execute before any tasks in the same play. Keep that ordering in mind when mixing roles and tasks.

Pass Variables Inline

To customize a role, pass variables right where you apply it. Ansible feeds them in, overriding the role's defaults.

roles:
  - role: nginx
    nginx_port: 8080

The role: Key Spells It Out

When you pass variables, use the role key to name the role, then add the variables as sibling keys beneath it.

- role: postgres
  db_name: shop
  db_user: admin

Defaults Are the Fallback

If you pass no variable, the role uses its value from defaults/main.yml. This is why every role works out of the box.

Override the Same Role Twice

You can apply one role multiple times with different variables, like configuring two database instances from a single role.

Group Vars Reach Roles Too

Variables in group_vars and host_vars automatically flow into roles applied to those hosts, no inline passing required.

vars_files Keep Plays Clean

Pull role settings out of the play into a separate file with vars_files, so your playbook stays short and readable.

vars_files:
  - web_settings.yml

include_role Runs Mid-Play

Need a role partway through your tasks? Use include_role as a task to apply it dynamically at exactly that point.

- name: Set up nginx
  ansible.builtin.include_role:
    name: nginx

Document the Variables You Accept

A good role lists the variables it expects in its README. Clear inputs make a role easy and safe for anyone to reuse.

One Role, Many Servers

Apply the same parameterized role across many hosts, tuning each with different variables. That is reuse without rewriting a single task.

Quick Check

Think about how to override a role's default value when applying it.

Recap

Apply roles with the roles key, pass variables inline to override defaults, and reuse one parameterized role across many hosts. 🚀

Frequently asked questions

Is the “Apply Roles & Pass Role Variables” lesson free?

Yes — the full text of “Apply Roles & Pass Role Variables” 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 “Apply Roles & Pass Role Variables”?

Include a role and parameterize it. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Apply Roles & Pass Role Variables” 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