TExcelTAD
Home › Configuration Management: Ansible › Role Defaults, Argument Specs & Dependencies

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:

bash · try it yourself
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 -13
TASK [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:

yaml
# roles/linkly_app/meta/main.yml
dependencies:
  - role: common
  - role: nginx_site
    vars:
      nginx_site_name: linkly

Dependencies 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: true in the argument spec for the rest.
  • Support check mode: avoid unconditional command tasks, or guard them.
  • Make OS differences data, not task copies: vars/Debian.yml and vars/RedHat.yml loaded with include_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_site works.
  • 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?

  1. 8080
  2. 80
  3. An error
  4. Both

What does meta/argument_specs.yml add?

  1. Faster runs
  2. Input validation before the role runs, and ansible-doc documentation
  3. Dependencies
  4. Encryption

Where do role dependencies go?

  1. defaults/main.yml
  2. meta/main.yml under dependencies
  3. requirements.yml
  4. tasks/deps.yml