Creating Your Own Collections

In the previous lesson you shared the nginx role from its own repository, and you saw its limits: a standalone role can't declare the collections it needs (the README has to ask the consumer to install community.general), each role has its own repository and version, and there's no clean place for your own modules or plugins. If the store's blog team also wants the base configuration and PostgreSQL, they'd have to manage three repositories and three versions and guess their dependencies. Collections solve exactly that: a single versioned package, with a namespace, that bundles roles, playbooks, modules and plugins, and declares its own dependencies. In this lesson you'll create the aurora_store.infra collection with Aurora's platform roles: you'll get to know its structure (galaxy.yml, roles/, playbooks/, plugins/, meta/runtime.yml, docs/), reference its roles by FQCN (aurora_store.infra.nginx), build it as an artifact, install it in another project, see how it's versioned and published, and decide, with reasons, what role it plays in aurora-infra for the rest of the course.

Contents

  1. Why collections
  2. What aurora_store.infra will include
  3. Creating the skeleton: ansible-galaxy collection init
  4. The metadata: galaxy.yml
  5. Runtime requirements: meta/runtime.yml
  6. Roles inside the collection
  7. Playbooks inside the collection
  8. plugins/ and docs/: what's to come
  9. Building the artifact: collection build
  10. Installing and testing the collection in another project
  11. Versioning and changelog
  12. Distributing and publishing the collection
  13. What about aurora-infra? The decision for the rest of the course

  1. Why collections

You've known collections as a consumer since Lesson 2.6. Now you're looking at them as an author. Compare the two ways of sharing:

Aspect Standalone roles (Lesson 6.4) Collection
Contents One role per repository Several roles, playbooks, modules, plugins and documentation
Name nginx (or author.nginx on Galaxy) FQCN: aurora_store.infra.nginx, with no possible ambiguity
Versions Git tags, one per role One semantic version for the whole package, declared in galaxy.yml
Collection dependencies Can't be declared (only documented) Declared in galaxy.yml and installed automatically
Distribution Git repository or import from GitHub Immutable .tar.gz artifact: Galaxy, Automation Hub, private servers, Git or files
Your own Python code Possible (library/, filter_plugins/ in the role), but only visible to that role and its users Its natural home: plugins/modules, plugins/filter… accessible by FQCN from anywhere
Enterprise servers Automation Hub doesn't accept standalone roles The only format for Automation Hub and private servers
Ansible version requirement min_ansible_version (informational) requires_ansible in meta/runtime.yml (Ansible warns if it isn't met)

In short: standalone roles are ideal for sharing a piece; collections, for sharing a platform.

  1. What aurora_store.infra will include

The name follows the namespace.collection form. The aurora_store namespace is the same one you set in the nginx role's meta/main.yml (Lesson 6.4), and infra describes the contents: the base infrastructure. Remember that names only allow lowercase letters, numbers and underscores.

Content Included? Why
common role Yes Base configuration for any server (packages, time zone, users, motd, local facts); fully parameterized
nginx role Yes Already generic and shared (version 1.0.0 of ansible-role-nginx)
postgresql role Yes Generic: version, parameters, users, databases and access, all through variables
aurora_app role No It deploys the store website: it's Aurora's business logic, not platform
Maintenance playbook (upgrade.yml) Yes, adapted Useful for any team; a good example of a playbook inside a collection
Your own modules and plugins Not yet Aurora doesn't have any yet; they're previewed in section 8

The intended consumer is the store blog team, which will have its own project (blog-infra) and wants to reuse Aurora's platform without copying code.

  1. Creating the skeleton: ansible-galaxy collection init

Collections have a peculiarity: Ansible only finds them if they're inside a path of the form ansible_collections/<namespace>/<collection>. That's why it's a good idea to create the working copy directly in that structure:

mkdir -p ~/ansible_collections
ansible-galaxy collection init aurora_store.infra --init-path ~/ansible_collections
- Collection aurora_store.infra was created successfully
cd ~/ansible_collections/aurora_store/infra
find . | sort
.
./docs
./galaxy.yml
./meta
./meta/runtime.yml
./plugins
./plugins/README.md
./README.md
./roles
Item What it's for
galaxy.yml The collection's metadata: name, version, authors, license, dependencies… (section 4)
meta/runtime.yml Runtime requirements and redirects (section 5)
roles/ The collection's roles, each with the usual structure (section 6)
plugins/ Python code: modules/, filter/, lookup/, inventory/… (section 8)
docs/ Additional documentation (section 8)
README.md The collection's front page on Galaxy or on the Git server
playbooks/ Not created by init, but it's a standard directory (section 7)

The ~/ansible_collections/aurora_store/infra path has a practical advantage: during development you can use the collection without building or installing it, by pointing ANSIBLE_COLLECTIONS_PATH at ~ (the directory that contains ansible_collections). You'll use this as your development loop, just like ANSIBLE_ROLES_PATH in Lesson 6.4.

Turn the directory into a Git repository:

git init -b main

  1. The metadata: galaxy.yml

init generates a galaxy.yml full of explanatory comments (read them once: they document every field). Replace it with Aurora's version:

---
# Metadata for the aurora_store.infra collection (Lesson 6.5).
namespace: aurora_store
name: infra
version: 1.0.0
readme: README.md

authors:
  - Aurora Store Operations team <[email protected]>

description: Aurora Store platform roles (base configuration, nginx and PostgreSQL) and a maintenance playbook

license:
  - MIT

tags:
  - infrastructure
  - linux
  - nginx
  - postgresql

# Collections the roles need. They are RANGES: the collection is a "library".
dependencies:
  community.general: ">=12.0.0,<13.0.0"
  community.postgresql: ">=4.0.0,<5.0.0"

repository: https://git.aurora-store.example/ops/ansible-collection-infra
issues: https://git.aurora-store.example/ops/ansible-collection-infra/issues

build_ignore:
  - "*.tar.gz"
  - .gitignore
Field Required Comment
namespace, name Yes Must match the FQCN (aurora_store.infra) and the path ansible_collections/aurora_store/infra
version Yes Strict semantic versioning (1.0.0, 1.1.0-beta.1); changed with every release (section 11)
readme Yes File shown as the front page
authors Yes A list; here, the team's generic email address, not a personal one
description No One sentence
license One of the two List of SPDX identifiers; alternative: license_file with the path to the file
tags No Keywords (lowercase letters, numbers and _)
dependencies No Required collections with their version range. ansible-galaxy installs them when installing this collection
repository, documentation, homepage, issues No Links shown by Galaxy
build_ignore No Patterns for files that must not go into the artifact (.git is always excluded)

Pay attention to the dependencies: here ranges are used (Lesson 6.3, section 5). The collection is a library: it says "I work with any community.general 12.x", and each consuming project pins whichever exact version it prefers within that range (Aurora has 12.6.7). If it declared exactly 12.6.7, it would clash with a consumer that had pinned 12.6.8. The ranges were chosen from the series Aurora has tested; they'll be widened when others are tested.

  1. Runtime requirements: meta/runtime.yml

meta/runtime.yml describes how the collection should be loaded. init generates it with all its options commented out. Leave it like this:

---
# ansible-core versions the collection works with (required for publishing).
requires_ansible: ">=2.17.0"
  • requires_ansible: if someone runs the collection with an older ansible-core, Ansible shows an unsupported collection warning. Galaxy and Automation Hub require this field for publishing. It's the same piece of data you looked up to choose the community.general version in Lesson 6.3.
  • plugin_routing (commented out in the generated file): lets you redirect a module or plugin to another name, or mark it as deprecated or removed. It's how collections maintain compatibility when they rename something (you saw it in Lesson 2.6 with module redirects).
  • action_groups: groups modules so they can be given common default values with module_defaults. You won't need it in Aurora.

  1. Roles inside the collection

Copying the roles

The common and postgresql roles are copied from aurora-infra. The nginx role is taken from its repository, at exactly the published version, with git archive (which extracts the contents of a tag without the .git directory):

cd ~/ansible_collections/aurora_store/infra
cp -r ~/aurora-infra/roles/common roles/
cp -r ~/aurora-infra/roles/postgresql roles/
mkdir roles/nginx
git -C ~/ansible-role-nginx archive 1.0.0 | tar -x -C roles/nginx
ls roles/ roles/nginx/
roles/:
common  nginx  postgresql

roles/nginx/:
CHANGELOG.md  defaults  handlers  LICENSE  meta  README.md  tasks  templates  vars

Inside the collection, the nginx role doesn't need its own LICENSE or CHANGELOG.md (the collection will have its own):

rm roles/nginx/LICENSE roles/nginx/CHANGELOG.md

Rules for roles in a collection

A role inside a collection works just like a standalone one, with a few differences:

Rule Detail
Name Only lowercase letters, numbers and _, and it must start with a letter (aurora_app would be valid; ansible-role-nginx wouldn't)
How it's referenced By FQCN: aurora_store.infra.nginx
galaxy_info Ignored: the metadata that counts is in galaxy.yml. dependencies still works
Dependencies between roles With FQCN: dependencies: [{role: aurora_store.infra.nginx}]
Modules and plugins Always with FQCN (Aurora's roles already do this)
argument_specs Works the same: validation and ansible-doc -t role aurora_store.infra.nginx
Files and templates Relative paths (templates/, files/, vars/) are resolved inside the role as always; the first_found in postgresql (Lesson 4.5) still works

If aurora_app were in the collection, its role: nginx dependency would have to become role: aurora_store.infra.nginx. The three chosen roles have no dependencies, so nothing needs to change.

Using the roles by FQCN

In any project that has the collection installed, the roles are used like this:

- name: Configure the blog web server
  hosts: blog
  become: true
  roles:
    - role: aurora_store.infra.common
    - role: aurora_store.infra.nginx
      vars:
        nginx_site_name: blog
        nginx_server_name: blog.aurora-store.example
        nginx_root: /var/www/blog

The FQCN removes any ambiguity: it doesn't matter if the consuming project has another role called nginx in its roles/; aurora_store.infra.nginx is always the collection's.

  1. Playbooks inside the collection

A collection can also distribute complete playbooks, in playbooks/. Adapt Aurora's maintenance playbook (Lesson 5.8) so it doesn't depend on Aurora's groups. Create playbooks/upgrade.yml:

---
# Maintenance: upgrade the system packages (Debian family).
# Usage: ansible-playbook aurora_store.infra.upgrade -e infra_upgrade_hosts=<pattern>
# Caution: if services are upgraded (nginx, PostgreSQL...), they are restarted.

- name: Upgrade the system packages
  hosts: "{{ infra_upgrade_hosts | default('all') }}"
  become: true
  gather_facts: false

  tasks:
    - name: Upgrade all the system packages
      ansible.builtin.apt:
        upgrade: dist
        update_cache: true
        cache_valid_time: 3600
      async: 1800
      poll: 15

    - name: Check whether the system needs a reboot
      ansible.builtin.stat:
        path: /var/run/reboot-required
      register: infra_reboot_required

    - name: Report the servers that need a reboot
      ansible.builtin.debug:
        msg: "{{ inventory_hostname }} needs a reboot to complete the upgrade."
      when: infra_reboot_required.stat.exists

Changes from the original:

  • hosts uses a variable with a default value, because the collection doesn't know the groups in each inventory. Since hosts is evaluated before the inventory variables are loaded, infra_upgrade_hosts can only be passed with -e.
  • The variables carry the collection's prefix (infra_), following Aurora's prefix convention.

A collection playbook is run by its FQCN, without the extension:

ansible-playbook aurora_store.infra.upgrade -e infra_upgrade_hosts=webservers

  1. plugins/ and docs/: what's to come

plugins/ is where the collection keeps Python code, organized by type:

Directory Contents Resulting FQCN
plugins/modules/ Modules (run on the managed servers) aurora_store.infra.<module>
plugins/filter/ Jinja2 filters aurora_store.infra.<filter>
plugins/lookup/ Lookups lookup('aurora_store.infra.<lookup>', ...)
plugins/inventory/ Dynamic inventory plugins plugin: aurora_store.infra.<plugin>
plugins/module_utils/ Code shared between modules (internal use)

Aurora doesn't have any Python code of its own yet, so plugins/ is left with just its README.md. But you can already picture how it'll be used: the expression with dict, map('extract') and zip that computes the load balancer's servers (Lesson 5.3) could become a readable, tested filter in the collection. You'll write modules in Lesson 10.1, dynamic inventories in 10.2 and plugins in 10.3, and the collection will be their natural home.

docs/ accepts additional documentation in Markdown or reStructuredText. For a small collection, a good README.md is enough. Replace the one generated by init with a short one:

# aurora_store.infra collection

Aurora Store platform roles for Ubuntu 24.04.

| Content | Type | Description |
|---------|------|-------------|
| `aurora_store.infra.common` | Role | Packages, time zone, users, motd and local facts |
| `aurora_store.infra.nginx` | Role | nginx as a static site or load balancer, with optional ufw |
| `aurora_store.infra.postgresql` | Role | PostgreSQL: parameters, users, databases and pg_hba.conf |
| `aurora_store.infra.upgrade` | Playbook | Package upgrade with a reboot notice |

## Requirements

- ansible-core 2.17 or later.
- The `community.general` and `community.postgresql` collections are installed
  automatically as dependencies.

## Installation

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