Aurora Store's four roles work, they're well organized and the playbooks apply them in the right order. But a good part of what you need to know to use them still isn't written down anywhere: that aurora_app is useless without nginx, that it needs an owner user created by common, which variables are mandatory, what type of value each one expects. Today you know it; six months from now, or for a colleague who joins the team, it will be a source of errors. In this last lesson of the module you'll learn to declare dependencies between roles in meta/main.yml (and when it's better not to), to automatically validate a role's variables with meta/argument_specs.yml, which makes the role fail right at the start with a clear message if it receives a wrong value, and to document roles with a README.md and with ansible-doc.

Contents

  1. Dependencies between roles
  2. Declaring dependencies in meta/main.yml
  3. Execution order and deduplication of dependencies
  4. Practice: aurora_app depends on nginx
  5. When to avoid dependencies
  6. Checking requirements without dependencies
  7. The argument specification: meta/argument_specs.yml
  8. Types and options of the specification
  9. Practice: specifications for nginx and aurora_app
  10. Validation in action
  11. Documenting roles: README.md
  12. Reading the documentation with ansible-doc -t role
  13. Verifying and saving the work

  1. Dependencies between roles

A role depends on another when it can't do its job unless the other one has been applied first. In Aurora:

  • aurora_app publishes the store website in /var/www/aurora, but without nginx nobody would serve it: it depends on nginx.
  • aurora_app also needs the deploy user to exist, which common creates.
  • nginx and postgresql don't depend on any other Aurora role.

Right now those relationships are guaranteed only by the order of site.yml and of the roles: list in webservers.yml. If someone wrote a new playbook with just roles: [aurora_app], they'd get a website published on a server without nginx, and no error at all. Ansible offers two tools to prevent that: declared dependencies and checks inside the role.

  1. Declaring dependencies in meta/main.yml

Dependencies are declared in the dependencies key of meta/main.yml, which until now has been empty in every role. It's a list with the same syntax as a play's roles: keyword:

dependencies:
  # Simple form: just the role name
  - role: nginx

  # With parameters, a condition or tags, as in roles:
  - role: nginx
    vars:
      nginx_port: 8080
    when: ansible_facts['os_family'] == 'Debian'
    tags:
      - web
  • Each dependency is looked up just like a role in the play: in roles_path, or by its fully qualified name if it lives in a collection (namespace.collection.role).
  • Dependencies aren't installed on their own: they must be available when the role runs. When you install third-party roles that have dependencies, ansible-galaxy can download them (Lessons 6.3 and 6.4).
  • When they run, their tasks appear with the dependency's role name (nginx : ...), as if it had been listed in the play.

  1. Execution order and deduplication of dependencies

When a play applies a role with dependencies, Ansible runs the dependencies first (and, if they have their own, those come first, recursively) and then the role itself:

flowchart LR
    subgraph Play["roles: [aurora_app]"]
        direction LR
        A["nginx<br/>(dependency)"] --> B["aurora_app"]
    end
    subgraph Play2["roles: [nginx, aurora_app]"]
        direction LR
        C["nginx<br/>(from the play)"] --> D["nginx<br/>(dependency of aurora_app)<br/>skipped: already run"] --> E["aurora_app"]
    end

Dependencies follow the same deduplication rule you saw in Lesson 4.4: within a play, a role with the same parameters runs only once. So if nginx has already run (because it came earlier in the play's list or because another role depended on it), the dependency is skipped. This has two consequences:

  • You can declare dependencies without worrying about repeating work, as long as they don't carry parameters that differ from the play's.
  • A dependency with its own parameters (like the second example in section 2) counts as a different application and runs again, with those parameters. If the play had already applied nginx with port 80 and the dependency asks for it with 8080, nginx would be configured twice, and the second run would undo part of the first.

If the dependency's role declares allow_duplicates: true in its meta/main.yml, it always runs, as in Lesson 4.4.

  1. Practice: aurora_app depends on nginx

The relationship between aurora_app and nginx is a real technical dependency: the application can't work without the web server. Declare it in roles/aurora_app/meta/main.yml:

---
galaxy_info:
  author: Alex Admin
  description: Deploys the Aurora Store website and its database access configuration
  company: Aurora Store
  license: proprietary
  min_ansible_version: "2.17"
  platforms:
    - name: Ubuntu
      versions:
        - noble

dependencies:
  - role: nginx

No parameters: nginx takes its values from the inventory, just as when the play applies it. That's why the dependency and the nginx in webservers.yml are "the same" role with the same parameters, and only one of them runs.

So do you remove nginx from webservers.yml? Aurora keeps it:

  roles:
    - nginx
    - aurora_app

The playbook still states explicitly what's on the web servers (whoever reads it doesn't have to open meta/main.yml to find out there's nginx), and the dependency is the guarantee that aurora_app will never be applied without nginx, even if someone uses it in another playbook.

Check it:

ansible-playbook webservers.yml --list-tasks

Depending on the ansible-core version, --list-tasks may show the nginx tasks twice (the play's and the dependency's): the listing is computed before deduplication is applied. What counts is the actual run:

ansible-playbook webservers.yml
TASK [nginx : Install nginx] ***************************************************
ok: [web1]
ok: [web2]
...
TASK [nginx : Allow SSH through the firewall] **********************************
...
TASK [aurora_app : Create the web root directory] ******************************
ok: [web1]
ok: [web2]
...
PLAY RECAP *********************************************************************
web1                       : ok=...  changed=0    unreachable=0    failed=0    ...
web2                       : ok=...  changed=0    unreachable=0    failed=0    ...

The nginx tasks appear only once, and the result is still changed=0. To see the dependency in action, try a playbook that applies only aurora_app, in check mode:

---
# test-dependency.yml (for testing only: delete it afterwards)
- name: Apply only the store website
  hosts: web1
  become: true
  roles:
    - aurora_app
ansible-playbook test-dependency.yml --check
rm test-dependency.yml

You'll see all the nginx : ... tasks first and then the aurora_app : ... ones, even though the play only mentions aurora_app.

  1. When to avoid dependencies

Should aurora_app also depend on common, which creates the deploy user? Tempting, but no. Dependencies have costs worth knowing:

  • They're implicit: whoever reads the playbook doesn't see them. A role with chains of dependencies turns into a "box of surprises" that applies things nobody asked for.
  • Deduplication is per play. In site.yml, common is applied in its own play (on aurora). If aurora_app depended on common, common would run in full again on web1 and web2 in the webservers play: packages, users, motd, facts… Time wasted on every run.
  • They couple roles: a role that depends on common can no longer be used in another project that doesn't have that common.
  • With parameters, they run twice (section 3), with results that are hard to understand.

Criteria for deciding:

Situation Best option Example in Aurora
The role can't work without the other one on the same hosts, and the other one is a specific component Dependency in meta/main.yml aurora_app → nginx
Base configuration applied to every server in its own play Order in the playbook (site.yml), without a dependency common before everything else
The role needs something that anyone could provide (a user, a directory, a package) A check inside the role that fails with a clear message That the deploy user exists
Relationship between different hosts (the website needs the database on db1) Play order in site.yml and data through the inventory dbservers.yml before webservers.yml

  1. Checking requirements without dependencies

For the owner user, aurora_app will check that it exists before using it. Add this at the beginning of roles/aurora_app/tasks/main.yml:

- name: Check that the website owner user exists
  ansible.builtin.getent:
    database: passwd
    key: "{{ aurora_app_owner }}"

The ansible.builtin.getent module queries the system databases (here, the user database, passwd). If the key doesn't exist, it fails:

fatal: [web1]: FAILED! =>
    changed: false
    msg: One or more supplied key could not be found in the database.

Without this task, the error would show up later, in Create the web root directory, with a message about an unknown user or group that doesn't say why. With it, the role documents and checks its requirement without forcing common to run. It's a query: it never reports changed and it works in check mode.

  1. The argument specification: meta/argument_specs.yml

The second problem is variables. Right now nothing stops anyone from passing nginx_port: eighty or nginx_firewall_enabled: "maybe": the role would fail halfway through or, worse, generate an incorrect configuration. Since Ansible 2.11, a role can include meta/argument_specs.yml, a formal specification of its variables:

---
argument_specs:
  main:                                  # entry point: tasks/main.yml
    short_description: Installs nginx and publishes a website
    description:
      - Longer description, in one or more sentences.
    options:
      nginx_port:                        # variable name
        type: int
        default: 80
        description: TCP port the site listens on.

If a role has this file, Ansible automatically adds, before the role's first task, a task that validates the variables against the specification:

TASK [nginx : Validating arguments against arg spec 'main' - Installs nginx and publishes a website] ***
ok: [web1]

If anything doesn't meet the specification, the role fails right there, before touching the server. The validation task carries the special always tag: it runs even if you filter with --tags (Lesson 5.6).

The top-level keys under argument_specs are the role's entry points: main corresponds to tasks/main.yml. If a role is used with tasks_from: install (Lesson 4.5), it's validated against the install entry, if there is one.

  1. Types and options of the specification

Each variable in options accepts these keys:

Key Meaning Example
type Expected type str, int, bool, float, list, dict, path, raw
required Whether the variable must be defined required: true
default Default value for the documentation default: 80
choices List of allowed values choices: [scram-sha-256, md5]
elements Type of the elements, if type: list elements: str
options Suboptions, if the elements are dictionaries (with the same keys) name, owner of each database
description Description (text or list of paragraphs) —

Details worth being clear about:

  • Validation converts when it can: "80" (text) is valid for type: int; eighty isn't. yes, "true" or 1 are valid for type: bool.
  • path is a text value interpreted as a path (it expands ~ and environment variables).
  • raw accepts anything: use it only if the variable really does accept several types.
  • default does NOT define the variable. This is the most surprising thing about argument_specs: the default value is used for documentation (and for the validation itself), but the role does not receive it as a variable. The real default values stay in defaults/main.yml, and they must match the ones in the specification.
  • required: true only makes sense for variables without a default value. If the variable is in defaults/main.yml, it always "exists" and the check never fails.
  • There are no ranges: the specification can't express "between 1 and 65535". For that, use an ansible.builtin.assert task at the beginning of the role (Lesson 3.7).

  1. Practice: specifications for nginx and aurora_app

Create roles/nginx/meta/argument_specs.yml:

---
argument_specs:
  main:
    short_description: Installs nginx and publishes a website
    description:
      - Installs nginx from the distribution repositories, enables gzip compression
        for text responses and publishes a single static site.
      - Optionally manages the ufw firewall, allowing SSH and the site's port.
    author:
      - Alex Admin
    options:
      nginx_port:
        type: int
        default: 80
        description: TCP port the site listens on.
      nginx_site_name:
        type: str
        default: app
        description: Site name (file in sites-available and link in sites-enabled).
      nginx_server_name:
        type: str
        default: "_"
        description: Value of the server_name directive. Several names, separated by spaces.
      nginx_root:
        type: path
        default: /var/www/html
        description: Root directory of the site. The role doesn't create it.
      nginx_remove_default_site:
        type: bool
        default: true
        description: Disable the default site installed by the package.
      nginx_firewall_enabled:
        type: bool
        default: false
        description:
          - Manage the ufw firewall. If true, allows SSH and the site's port and enables ufw.

For aurora_app, take the opportunity to make a design decision: what default value makes sense for the database server? localhost doesn't: the store website is never on the same server as PostgreSQL, and a wrong default would produce a configuration that looks valid but is useless. It's better for the variable to be mandatory. Remove aurora_app_db_host from roles/aurora_app/defaults/main.yml, so that the database block looks like this:

# Database connection. aurora_app_db_host has no default value:
# it's mandatory (see meta/argument_specs.yml).
aurora_app_db_port: 5432
aurora_app_db_name: aurora
aurora_app_db_user: aurora

And create roles/aurora_app/meta/argument_specs.yml:

---
argument_specs:
  main:
    short_description: Deploys the Aurora Store website
    description:
      - Publishes the store's home page and static pages in the web server's root
        directory, and the database access configuration in aurora_app_config_dir.
      - Depends on the nginx role. Requires the aurora_app_owner user to exist.
    author:
      - Alex Admin
    options:
      aurora_app_web_root:
        type: path
        default: /var/www/aurora
        description: Root directory of the website. It must match the web server's root.
      aurora_app_owner:
        type: str
        default: deploy
        description: User and group that own the root directory. They must already exist.
      aurora_app_domain:
        type: str
        default: localhost
        description: Domain shown on the home page.
      aurora_app_static_pages:
        type: dict
        default: {}
        description: Additional static pages, as a dictionary {file name = content}.
      aurora_app_config_dir:
        type: path
        default: /etc/aurora
        description: Application configuration directory.
      aurora_app_db_host:
        type: str
        required: true
        description: Address of the database server.
      aurora_app_db_port:
        type: int
        default: 5432
        description: Port of the database server.
      aurora_app_db_name:
        type: str
        default: aurora
        description: Name of the store database.
      aurora_app_db_user:
        type: str
        default: aurora
        description: Store database user.

Notice the consistency you have to maintain: every variable in defaults/main.yml appears in the specification with the same default value, and the only mandatory one (aurora_app_db_host) is precisely the one that isn't in defaults. In Aurora, the inventory defines it in group_vars/webservers.yml, so everything keeps working.

argument_specs validates the already resolved values: in the inventory, aurora_app_db_port is "{{ hostvars[aurora_db_server]['postgresql_port'] }}", and what gets validated is the result, 5432.

  1. Validation in action

Run the web servers as usual:

ansible-playbook webservers.yml
TASK [nginx : Validating arguments against arg spec 'main' - Installs nginx and publishes a website] ***
ok: [web1]
ok: [web2]
...
TASK [aurora_app : Validating arguments against arg spec 'main' - Deploys the Aurora Store website] ***
ok: [web1]
ok: [web2]

TASK [aurora_app : Check that the website owner user exists] *******************
ok: [web1]
ok: [web2]
...

Now trigger some errors. A wrong type, with -e (which beats everything) and in check mode so you don't risk anything:

ansible-playbook webservers.yml --check --limit web1 -e nginx_port=eighty
TASK [nginx : Validating arguments against arg spec 'main' - Installs nginx and publishes a website] ***
fatal: [web1]: FAILED! =>
    argument_errors:
    - 'argument ''nginx_port'' is of type str and we were unable to convert to int: "''eighty''"
        cannot be converted to an int'
    changed: false
    msg: |-
        Validation of arguments failed:
        argument 'nginx_port' is of type str and we were unable to convert to int: "'eighty'" cannot be converted to an int
    ...

The role fails before its first task: no file has been touched. Without the specification, the value would have reached the site configuration (listen eighty), and the error would have come from nginx on reload. A value outside choices or a dictionary where a list is expected produce equally clear messages.

Now a missing mandatory variable. Temporarily comment out the aurora_app_db_host line in inventory/group_vars/webservers.yml and run:

ansible-playbook webservers.yml --check --limit web1
TASK [aurora_app : Validating arguments against arg spec 'main' - Deploys the Aurora Store website] ***
fatal: [web1]: FAILED! =>
    argument_errors:
    - 'missing required arguments: aurora_app_db_host'
    ...

Restore the line and check that ansible-playbook webservers.yml --check finishes without errors again. If you wanted to validate variables at any point in a playbook (not at the start of a role), there's also the ansible.builtin.validate_argument_spec module, which uses the same format.

  1. Documenting roles: README.md

argument_specs.yml documents the variables in a structured way, but a role also needs documentation for people: what it does, what it requires, how to use it. That's the job of the README.md that ansible-galaxy role init generated. Replace the template in roles/nginx/README.md with:

# nginx role

Installs nginx and publishes a single static website. Enables gzip compression
for text responses and, optionally, manages the ufw firewall.

## Requirements

- Ubuntu 24.04 (noble). Other distributions aren't supported.
- The `community.general` collection (`ufw` module).
- The play must run with `become: true`.

## Variables

All of them have a default value in `defaults/main.yml`. The full specification,
with types, is in `meta/argument_specs.yml` (`ansible-doc -t role nginx`).

| Variable | Default | Description |
|----------|---------|-------------|
| `nginx_port` | `80` | Site port |
| `nginx_site_name` | `app` | Name of the site file |
| `nginx_server_name` | `_` | `server_name` directive |
| `nginx_root` | `/var/www/html` | Root directory (the role doesn't create it) |
| `nginx_remove_default_site` | `true` | Disable the `default` site |
| `nginx_firewall_enabled` | `false` | Manage ufw (SSH + site port) |

## Dependencies

None.

## Example usage
  • name: Blog web server hosts: blog become: true roles:
    • role: nginx vars: nginx_site_name: blog nginx_server_name: blog.example.com nginx_root: /var/www/blog
## Notes

- Reloading nginx after a configuration change is temporary
  (`register` + `when`) and will be replaced by a handler.

## Author

Alex Admin - Aurora Store (internal use).

A good role README.md always answers the same questions: what it does, what it needs (systems, collections, privileges), which variables it accepts, what it depends on and how to use it. Notice that it doesn't repeat every variable's description in full detail: it points to argument_specs.yml, which is the source that gets validated. Write a similar one for aurora_app (and, in the exercises, for postgresql), mentioning its dependency on nginx and its owner-user requirement.

  1. Reading the documentation with ansible-doc -t role

Just as ansible-doc shows a module's documentation (Lesson 2.5), with -t role it shows a role's documentation, built from its meta/argument_specs.yml. It looks for roles in roles_path, which is why you configured it in Lesson 4.2:

ansible-doc -t role aurora_app
> ROLE: aurora_app (/home/student/aurora-infra/roles/aurora_app)

ENTRY POINT: main - Deploys the Aurora Store website

        Publishes the store's home page and static pages in the web server's root
        directory, and the database access configuration in aurora_app_config_dir.
        Depends on the nginx role. Requires the aurora_app_owner user to exist.

Options (red indicates it is required):

   aurora_app_config_dir  Application configuration directory.
        default: /etc/aurora
        type: path

   aurora_app_db_host  Address of the database server.
        type: str
   ...

Mandatory variables are highlighted in red in the terminal. To list the roles that have documentation:

ansible-doc -t role -l

The list also includes the roles shipped in the collections installed with the ansible package; yours appear with their short name (aurora_app, nginx…) and the number of documented entry points. A role without argument_specs.yml appears in the list, but with no documentation.

  1. Verifying and saving the work

Check that, after all the changes, the infrastructure is still intact:

ansible-playbook site.yml --check --diff
ansible-playbook site.yml
PLAY RECAP *********************************************************************
db1                        : ok=...  changed=0    unreachable=0    failed=0    ...
web1                       : ok=...  changed=0    unreachable=0    failed=0    ...
web2                       : ok=...  changed=0    unreachable=0    failed=0    ...

Save the work (include the files from Exercises 1 to 3 once you complete them):

git add roles/
git commit -m "aurora_app -> nginx dependency, argument_specs and role READMEs"

Final structure of the repository at the end of Module 4:

aurora-infra/
├── ansible.cfg                  ← roles_path = roles
├── site.yml                     ← imports common.yml, dbservers.yml, webservers.yml
├── common.yml                   ← roles: [common]
├── dbservers.yml                ← roles: [postgresql] + post_tasks (ping)
├── webservers.yml               ← roles: [nginx, aurora_app] + post_tasks (checks)
├── inventory/
│   ├── hosts.yml
│   ├── group_vars/
│   │   ├── all/
│   │   │   ├── aurora.yml       ← aurora_domain, aurora_env, aurora_admin_email
│   │   │   ├── common.yml       ← common role parameters
│   │   │   └── team.yml         ← common_users
│   │   ├── dbservers.yml        ← aurora_db_*, postgresql parameters
│   │   └── webservers.yml       ← aurora_db_server, aurora_app and nginx parameters
│   └── host_vars/{db1.yml, web1.yml, web2.yml}
├── roles/
│   ├── common/
│   │   ├── README.md
│   │   ├── defaults/main.yml
│   │   ├── meta/{main.yml, argument_specs.yml}
│   │   ├── tasks/main.yml
│   │   └── vars/main.yml
│   ├── nginx/
│   │   ├── README.md
│   │   ├── defaults/main.yml
│   │   ├── files/gzip.conf
│   │   ├── meta/{main.yml, argument_specs.yml}
│   │   ├── tasks/{main, install, configure, site, service, firewall}.yml
│   │   └── vars/main.yml
│   ├── postgresql/
│   │   ├── README.md
│   │   ├── defaults/main.yml
│   │   ├── meta/{main.yml, argument_specs.yml}
│   │   ├── tasks/{main, install, configure, users, access}.yml
│   │   └── vars/Debian.yml
│   └── aurora_app/
│       ├── README.md
│       ├── defaults/main.yml
│       ├── meta/{main.yml, argument_specs.yml}   ← dependency: nginx
│       ├── tasks/main.yml
│       └── vars/main.yml
└── lab/
    └── Vagrantfile

Common Mistakes and Tips

  • the role 'X' was not found when running a role with dependencies: the dependency isn't in roles_path and isn't installed. Dependencies in meta/main.yml aren't downloaded at run time.
  • Dependencies with parameters that "undo" the configuration: if a dependency passes parameters that differ from the play's, the role runs twice with different values. Prefer dependencies without parameters and leave the data in the inventory.
  • Depending on a "base" role like common: it runs again in full in every play that uses the dependent role. Order the plays in site.yml and, if needed, check the requirement inside the role.
  • Relying on the default in argument_specs.yml: it doesn't define the variable. If it isn't in defaults/main.yml, the task will fail with "undefined variable" even though validation passes.
  • Marking as required a variable that's in defaults/main.yml: the check never fails, so it adds nothing. Mandatory means "no default value".
  • Specification and defaults out of sync: you change a default value in one place but not the other, and the documentation lies. Review both files together; ansible-lint (Lesson 9.4) helps detect inconsistencies in roles.
  • Tip: write argument_specs.yml as soon as a role has users other than its author. The cost is small, and it turns errors that would surface halfway through the run (or in production) into an immediate failure with a clear message.

Exercises

Exercise 1: Specification for the postgresql role

Write roles/postgresql/meta/argument_specs.yml with all the variables in defaults/main.yml. Use elements and options to describe the elements of postgresql_users and postgresql_databases, and choices for postgresql_hba_method (scram-sha-256, md5, trust, reject). Then check with --check --limit db1 that -e postgresql_hba_method=password fails validation.

Exercise 2: Specification for the common role

Write roles/common/meta/argument_specs.yml. Describe the elements of common_users (name mandatory; comment; admin boolean; state with the values present or absent).

Exercise 3: README for postgresql

Write roles/postgresql/README.md following the structure of the nginx one. Under "Requirements", include the collection it needs and its behavior on unsupported distributions; under "Notes", which variables must be in the inventory (and not only in defaults) if other hosts read them through hostvars.

Solutions

Exercise 1. roles/postgresql/meta/argument_specs.yml:

---
argument_specs:
  main:
    short_description: Installs PostgreSQL and manages its configuration, users, databases and access
    description:
      - Installs PostgreSQL from the distribution repositories (Debian family only),
        publishes its main parameters in conf.d, creates users and databases
        and adds access rules to pg_hba.conf.
    author:
      - Alex Admin
    options:
      postgresql_version:
        type: int
        default: 16
        description: Major version installed by the distribution. It determines the configuration paths.
      postgresql_conf_name:
        type: str
        default: ansible
        description: Name (without extension) of the parameters file in conf.d.
      postgresql_listen_addresses:
        type: str
        default: localhost
        description: Value of listen_addresses (comma-separated addresses).
      postgresql_port:
        type: int
        default: 5432
        description: Server port.
      postgresql_max_connections:
        type: int
        default: 100
        description: Maximum number of concurrent connections.
      postgresql_shared_buffers:
        type: str
        default: 128MB
        description: Memory for the shared cache (with a unit, for example 512MB).
      postgresql_users:
        type: list
        elements: dict
        default: []
        description: Users to create.
        options:
          name:
            type: str
            required: true
            description: User name.
      postgresql_databases:
        type: list
        elements: dict
        default: []
        description: Databases to create.
        options:
          name:
            type: str
            required: true
            description: Database name.
          owner:
            type: str
            required: true
            description: Owner user (it must be in postgresql_users or already exist).
      postgresql_hba_clients:
        type: list
        elements: str
        default: []
        description: IP addresses of the authorized clients (one /32 rule for each).
      postgresql_hba_database:
        type: str
        default: all
        description: Database for the access rules.
      postgresql_hba_user:
        type: str
        default: all
        description: User for the access rules.
      postgresql_hba_method:
        type: str
        default: scram-sha-256
        choices:
          - scram-sha-256
          - md5
          - trust
          - reject
        description: Authentication method for the access rules.

required: true inside the options of a list means "mandatory in each element", even though the list itself has a default value. With -e postgresql_hba_method=password, validation fails with value of postgresql_hba_method must be one of: scram-sha-256, md5, trust, reject, got: password.

Exercise 2. roles/common/meta/argument_specs.yml:

---
argument_specs:
  main:
    short_description: Base server configuration
    description:
      - Installs basic packages, sets the time zone, manages the team users
        and publishes a welcome message and some local facts.
    author:
      - Alex Admin
    options:
      common_packages:
        type: list
        elements: str
        default: [curl, vim]
        description: Packages installed on every server.
      common_timezone:
        type: str
        default: Etc/UTC
        description: System time zone.
      common_users:
        type: list
        elements: dict
        default: []
        description: Team users.
        options:
          name:
            type: str
            required: true
            description: User name.
          comment:
            type: str
            description: Full name or description.
          admin:
            type: bool
            description: If true, the user is added to the sudo group.
          state:
            type: str
            choices: [present, absent]
            description: present (the default) or absent to remove the user.
      common_organization:
        type: str
        default: Server managed with Ansible
        description: Organization shown in the welcome message.
      common_env:
        type: str
        default: production
        description: Server environment (motd and local facts).
      common_domain:
        type: str
        default: localdomain
        description: Project domain (local facts).
      common_contact:
        type: str
        default: root@localhost
        description: Contact for incidents (motd).
      common_facts_name:
        type: str
        default: local
        description: Name of the local facts file (without .fact).
      common_managed_by:
        type: str
        default: ansible
        description: Value of managed_by in the local facts.

The suboptions have no default: the default values of admin and state are applied by the tasks themselves with the default filter, and this way you avoid suggesting that the specification fills them in.

Exercise 3. A possible README.md (abridged):

# postgresql role

Installs PostgreSQL and manages its main parameters, users, databases
and access rules in pg_hba.conf.

## Requirements

- Debian family (tested on Ubuntu 24.04). On other families the role fails at the start
  ("No file was found when using first_found") because there's no vars/<family>.yml.
- The `community.postgresql` collection.
- The play must run with `become: true`.

## Variables

See `meta/argument_specs.yml` (`ansible-doc -t role postgresql`).

## Dependencies

None.

## Example usage

    - hosts: dbservers
      become: true
      roles:
        - postgresql

## Notes

- A role's default values aren't part of `hostvars`: if other hosts
  need to read `postgresql_port` (for example, to build a connection string),
  define it in the inventory.
- User passwords aren't managed yet.
- The restart and reload after a change are temporary and will be replaced by handlers.

Conclusion

You've completed Module 4. You know how to declare dependencies in meta/main.yml, how they're ordered (before the role, recursively) and how they're deduplicated within a play, and, above all, when not to use them: Aurora declares only the technical dependency aurora_app → nginx, keeps common as a play of its own in site.yml and checks the owner user with getent inside the role. You've written argument specifications in meta/argument_specs.yml (types, required, choices, elements, options and descriptions), which automatically validate the variables before the first task and turn a wrong value into an immediate, readable failure, keeping in mind that their default documents the variable but doesn't define it; and you've documented the roles with README.md and ansible-doc -t role. Looking back over the module, Aurora Store has gone from three long playbooks to four reusable roles (common, postgresql, nginx and aurora_app), split into phases, with their parameters in defaults, their constants in vars, their data in the inventory and playbooks that only orchestrate and verify, without a single server changing because of the refactoring. But those roles still carry the tasks marked "(temporary)": the nginx reload with its condition on four registered variables, the PostgreSQL restart when its parameters change and its reload when pg_hba.conf changes. In Module 5: Advanced Playbook Techniques you'll start with the Handlers lesson, where you'll replace all those temporary tasks with handlers inside each role: tasks that only run when another task notifies them, just once and at the right moment.

Ansible Course: From Beginner to Advanced

Module 1: Introduction to Ansible

Module 2: Ansible Fundamentals

Module 3: Playbooks

Module 4: Roles

Module 5: Advanced Playbook Techniques

Module 6: Ansible Galaxy

Module 7: Ansible Tower, AWX and Automation Platform

Module 8: Ansible for DevOps

Module 9: Ansible Best Practices

Module 10: Advanced Ansible Topics

Module 11: Final Project

© Copyright 2026. All rights reserved