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
- Why collections
- What
aurora_store.infrawill include - Creating the skeleton:
ansible-galaxy collection init - The metadata:
galaxy.yml - Runtime requirements:
meta/runtime.yml - Roles inside the collection
- Playbooks inside the collection
plugins/anddocs/: what's to come- Building the artifact:
collection build - Installing and testing the collection in another project
- Versioning and changelog
- Distributing and publishing the collection
- What about
aurora-infra? The decision for the rest of the course
- 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.
- What
aurora_store.infra will include
aurora_store.infra will includeThe 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.
- Creating the skeleton:
ansible-galaxy collection init
ansible-galaxy collection initCollections 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| 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:
- The metadata:
galaxy.yml
galaxy.ymlinit 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.
- Runtime requirements:
meta/runtime.yml
meta/runtime.ymlmeta/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 olderansible-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 thecommunity.generalversion 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 withmodule_defaults. You won't need it in Aurora.
- 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 varsInside the collection, the nginx role doesn't need its own LICENSE or CHANGELOG.md (the collection will have its own):
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/blogThe 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.
- 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.existsChanges from the original:
hostsuses a variable with a default value, because the collection doesn't know the groups in each inventory. Sincehostsis evaluated before the inventory variables are loaded,infra_upgrade_hostscan 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:
plugins/ and docs/: what's to come
plugins/ and docs/: what's to comeplugins/ 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
- What Is Ansible?
- Installing Ansible
- Setting Up the Practice Lab
- Ansible Architecture
- Basic Ansible Commands
Module 2: Ansible Fundamentals
- Inventory Files
- Inventory Variables: group_vars and host_vars
- Configuring Ansible (ansible.cfg)
- Ad Hoc Commands
- Ansible Modules
- Collections and Fully Qualified Names (FQCN)
Module 3: Playbooks
- Introduction to Playbooks
- Essential YAML for Ansible
- Writing Your First Playbook
- Playbook Structure
- Variables and Facts
- Conditionals and Loops
- Registering Results and Debugging Playbooks
Module 4: Roles
- Introduction to Roles
- Creating Roles
- Role Directory Structure
- Using Roles in Playbooks
- Reusing Tasks: include and import
- Role Dependencies and Argument Validation
Module 5: Advanced Playbook Techniques
- Handlers
- Jinja2 Templates
- Filters and Lookups
- Ansible Vault
- Error Handling
- Tags and Selective Execution
- Rolling Deployments and Delegation
- Optimizing Playbooks
Module 6: Ansible Galaxy
- What Is Ansible Galaxy?
- Using Ansible Galaxy
- Managing Dependencies with requirements.yml
- Creating and Sharing Roles
- Creating Your Own Collections
Module 7: Ansible Tower, AWX and Automation Platform
- Introduction to Ansible Tower and AWX
- Installing AWX
- Using AWX: Job Templates and Jobs
- Managing Projects and Inventories
- Credentials, Permissions (RBAC) and Workflows
- Execution Environments and ansible-navigator
Module 8: Ansible for DevOps
- Ansible and Docker
- Ansible and Kubernetes
- CI/CD with Ansible
- Ansible in Cloud Environments
- Ansible and Terraform
Module 9: Ansible Best Practices
- Code Organization
- Security Best Practices
- Performance Tuning
- Code Quality: ansible-lint and yamllint
- Testing Ansible Code with Molecule
Module 10: Advanced Ansible Topics
- Custom Modules
- Dynamic Inventory
- Ansible Plugins
- The Ansible API and ansible-runner
- Event-Driven Ansible
