Role Defaults, Argument Specs & Dependencies
A role is an API: its variables are the parameters, its effect on the server the return value. This lesson is about making that API clear and safe.
Defaults vs vars
defaults/main.yml |
vars/main.yml |
|
|---|---|---|
| precedence | lowest of all | higher than inventory and play vars |
| meant for | everything a user may tune | internals: package names per OS, paths that must not change |
| overridable by group_vars | yes | no (a frequent surprise) |
If users complain that "setting the variable in group_vars does nothing", the role probably put it in vars/.
Argument specs: typed, validated inputs
meta/argument_specs.yml declares each input with its type, whether it is required, allowed values and a description. Ansible then validates the inputs before the role runs (as an automatic first task), and ansible-doc -t role shows the documentation:
mkdir -p roles/app_user/{meta,defaults,tasks}
cat > roles/app_user/meta/argument_specs.yml <<'YML'
---
argument_specs:
main:
short_description: Create the application's config for one service user
options:
app_user_name:
type: str
required: true
description: Login name of the service account.
app_user_shell:
type: str
default: /usr/sbin/nologin
choices: [/usr/sbin/nologin, /bin/bash]
description: Login shell.
app_user_groups:
type: list
elements: str
default: []
description: Supplementary groups.
YML
cat > roles/app_user/defaults/main.yml <<'YML'
---
app_user_shell: /usr/sbin/nologin
app_user_groups: []
YML
cat > roles/app_user/tasks/main.yml <<'YML'
---
- name: Record the account (stand-in for ansible.builtin.user)
ansible.builtin.debug:
msg: "{{ app_user_name }} shell={{ app_user_shell }} groups={{ app_user_groups }}"
YML
cat > good.yml <<'YML'
- name: Valid input
hosts: localhost
gather_facts: false
roles:
- role: app_user
vars:
app_user_name: linkly
app_user_groups: [www-data]
YML
cat > bad.yml <<'YML'
- name: Invalid input
hosts: localhost
gather_facts: false
roles:
- role: app_user
vars:
app_user_shell: /bin/zsh
YML
ansible-playbook good.yml | grep -E '^TASK|"msg"'
echo "=== bad input"
ansible-playbook bad.yml | grep -o '"argument_errors": \[[^]]*\]'
echo "=== docs"
ansible-doc -t role -r roles app_user 2>/dev/null | head -13TASK [app_user : Validating arguments against arg spec 'main' - Create the application's config for one service user] ***
TASK [app_user : Record the account (stand-in for ansible.builtin.user)] *******
"msg": "linkly shell=/usr/sbin/nologin groups=['www-data']"
=== bad input
"argument_errors": ["missing required arguments: app_user_name", "value of app_user_shell must be one of: /usr/sbin/nologin, /bin/bash, got: /bin/zsh"]
=== docs
> ROLE: *app_user* (/home/learner/roles/app_user)
ENTRY POINT: *main* - Create the application's config for one service user
Options (= indicates it is required):
- app_user_groups Supplementary groups.
default: []
elements: str
type: list
= app_user_name Login name of the service account.
type: str
The validation task runs before any real task, so a typo in an inventory variable stops the run with a precise message instead of a half-configured server. Write argument specs for every role that other people use.
Dependencies
meta/main.yml can list roles that must run first:
# roles/linkly_app/meta/main.yml
dependencies:
- role: common
- role: nginx_site
vars:
nginx_site_name: linklyDependencies run before the role, and a dependency used twice with the same parameters runs once (unless it sets allow_duplicates: true). They are convenient but hide ordering in a file people rarely open; many teams prefer listing roles explicitly in the play, so the order is visible in one place.
Role design checklist
- Prefix every variable with the role name:
nginx_site_port,app_user_name. - Defaults for everything optional;
required: truein the argument spec for the rest. - Support check mode: avoid unconditional
commandtasks, or guard them. - Make OS differences data, not task copies:
vars/Debian.ymlandvars/RedHat.ymlloaded withinclude_vars, then one set of tasks. - No secrets in defaults. Required secret inputs come from Vault in the inventory.
- Tag the role's tasks with its name so
--tags nginx_siteworks. - Test it with Molecule (Testing Roles with Molecule) and lint it (ansible-lint).
Test yourself
A role puts app_port: 80 in vars/main.yml. You set app_port: 8080 in group_vars. What does the role use?
- 8080
- 80
- An error
- Both
What does meta/argument_specs.yml add?
- Faster runs
- Input validation before the role runs, and
ansible-docdocumentation - Dependencies
- Encryption
Where do role dependencies go?
defaults/main.ymlmeta/main.ymlunderdependenciesrequirements.ymltasks/deps.yml