Working with Collections
1. Understanding Collections
Ansible Collections are a distribution format for Ansible content that includes playbooks, roles, modules, and other components. They allow you to organize and share Ansible content in a structured way.
| Component | Description | Usage |
|---|---|---|
| modules | Reusable code units that perform specific tasks | Used in playbooks to automate operations |
| roles | Organized sets of tasks, handlers, and variables | Reusable automation units for configuration management |
| playbooks | YAML files that define automation workflows | Define sequences of tasks to execute on managed nodes |
| plugins | Extensions that add functionality to Ansible | Include connection plugins, inventory plugins, and more |
2. Installing Collections
Collections can be installed using the ansible-galaxy command-line tool.
| Action | Command | Description |
|---|---|---|
| install collection | ansible-galaxy collection install namespace.collection_name |
Installs a specific collection from Ansible Galaxy or a local path |
| install from requirements file | ansible-galaxy collection install -r requirements.yml |
Installs collections listed in a requirements file |
| install from local path | ansible-galaxy collection install /path/to/collection |
Installs a collection from a local directory or tarball |
3. Using Collection Modules
After installing a collection, you can use its modules in your playbooks by specifying the full namespace and module name.
| Module Reference | Description | Usage Example |
|---|---|---|
| community.docker.docker_container | Manage Docker containers | ansible.builtin.docker_container: |
| community.mysql.mysql_user | Manage MySQL users | community.mysql.mysql_user: |
| ansible.builtin.file | Manage files and directories | ansible.builtin.file: |
Example: Using a collection module
- name: Create a Docker container
community.docker.docker_container:
name: web_server
image: nginx
ports:
- "80:80"
4. Setting Collection Path
You can configure where Ansible looks for collections using the ANSIBLE_COLLECTIONS_PATH environment variable or in ansible.cfg.
| Configuration Option | Default Value | Description |
|---|---|---|
| ANSIBLE_COLLECTIONS_PATH | ~/.ansible/collections:/usr/share/ansible/collections |
Colon-separated list of directories to search for collections |
| collections_paths | ~/.ansible/collections:/usr/share/ansible/collections |
Same as above, but in ansible.cfg |
Example: Setting collection path in ansible.cfg
[defaults]
collections_paths = /opt/ansible/collections:/home/user/my_collections
5. Creating Custom Collections
To create a custom collection, you must follow the directory structure and metadata format.
| Directory | Description | Required? |
|---|---|---|
| meta/main.yml | Collection metadata including dependencies and supported platforms | Yes |
| plugins/ | Directory for plugins (connection, inventory, etc.) | No |
| roles/ | Directory for roles | No |
| playbooks/ | Directory for playbooks | No |
| modules/ | Directory for custom modules | No |
Example: Creating a minimal collection structure
my_collection/
├── meta/
│ └── main.yml
├── roles/
│ └── my_role/
│ ├── tasks/
│ │ └── main.yml
│ └── handlers/
│ └── main.yml
└── modules/
└── my_module.py
6. Defining Collection Dependencies
Collection dependencies are defined in the meta/main.yml file to ensure that required collections are installed before using the current one.
| Field | Description | Example |
|---|---|---|
| requires_ansible | Minimum version of Ansible required | requires_ansible: ">=2.15" |
| collections | List of collections this collection depends on | collections: [ansible.posix, community.docker] |
Example: Defining dependencies in meta/main.yml
---
requires_ansible: ">=2.15"
collections:
- ansible.posix
- community.docker
7. Using Collections in Playbooks
To use collections in playbooks, you must reference them with their full namespace and module name.
| Reference Format | Description | Example |
|---|---|---|
| namespace.collection.module | Full module path | community.docker.docker_container |
| namespace.collection.role | Full role path | my_namespace.my_collection.my_role |
Example: Using a collection in a playbook
- name: Manage Docker containers
hosts: localhost
tasks:
- name: Start a container
community.docker.docker_container:
name: web_server
image: nginx
ports:
- "80:80"
8. Listing Collections
You can list installed collections using the ansible-galaxy collection list command.
| Command | Description | Output Example |
|---|---|---|
| ansible-galaxy collection list | Lists all installed collections | community.docker 2.6.0 |
| ansible-galaxy collection list namespace.collection_name | Lists a specific collection | community.docker 2.6.0 |
9. Upgrading Collections
To upgrade collections, use the ansible-galaxy collection install command with the --upgrade flag.
| Action | Command | Description |
|---|---|---|
| upgrade collection | ansible-galaxy collection install namespace.collection_name --upgrade |
Upgrades a specific collection to the latest version |
| upgrade all collections | ansible-galaxy collection install -r requirements.yml --upgrade |
Upgrades all collections listed in a requirements file |
10. Verifying Collections
You can verify the integrity and correctness of collections using ansible-galaxy collection verify.
| Command | Description | Output Example |
|---|---|---|
| ansible-galaxy collection verify namespace.collection_name | Verifies a specific collection | Verification successful |
| ansible-galaxy collection verify --all | Verifies all collections | All collections verified successfully |
11. Building Collections
To build a collection into a distributable format, use the ansible-galaxy collection build command.
| Action | Command | Description |
|---|---|---|
| build collection | ansible-galaxy collection build |
Builds a tarball of the collection |
| build with custom directory | ansible-galaxy collection build --output-path /path/to/output |
Builds a collection to a custom directory |
12. Publishing Collections
To publish a collection to Ansible Galaxy, you must first build it and then upload it using the ansible-galaxy collection publish command.
| Action | Command | Description |
|---|---|---|
| publish collection | ansible-galaxy collection publish namespace.collection_name.tar.gz |
Publishes a built collection to Ansible Galaxy |
| publish with token | ansible-galaxy collection publish --token YOUR_TOKEN namespace.collection_name.tar.gz |
Publishes a collection using an API token |