Ansible Series Chapter4: Detailed Explanation of Ansible Content Collections
Explanation of Ansible Content Collections
Introductions
What Are Ansible Content Collections?
Ansible Content Collections are a structured approach within the Ansible ecosystem for organising and distributing automation resources.
They bundle together modules
, plugins
, roles
, documentation
, and other related files
into a reusable and independently managed unit, allowing users to easily access and utilise the necessary resources.
Core Components of a Content Collection:
Modules: Small scripts that perform specific tasks, such as managing files or installing software.
Plugins: Pluggable components that extend Ansible’s functionality, such as callback plugins or filter plugins.
Roles: Structured directories used to organise and reuse automation tasks.
Documentation and Examples: Includes usage guides, YAML example files, and other resources to help users understand and implement the collection effectively.
Advantages
Modularity and Decoupling: Separates resources from the Ansible core, allowing independent updates.
Ease of Distribution and Management: Enables quick installation and management of collections using the ansible-galaxy command.
Namespace Support: Utilises the namespace.collection_name structure to prevent naming conflicts.
Community-Driven: Developers and organisations can share content collections, fostering the growth of the open-source ecosystem.
Differences Between Using Content Collections and the Traditional Approach
Before the introduction of Ansible Content Collections, Ansible relied on role directory structures and core module packages.
This traditional approach had several limitations:
Difficult Module Updates: All modules were bundled with the Ansible core version, meaning that updating a module required upgrading the entire Ansible version.
Lack of Flexibility: Roles and modules were managed separately, requiring users to handle multiple independent resources manually.
In contrast, Content Collections unify these resources by packaging modules, plugins, roles, and documentation into a single, standalone distribution unit. This design eliminates the limitations of the traditional approach, making automation more modular, flexible, and easier to manage.
Namespace
Why Are Namespaces Needed?
To make it easier to specify collections and their contents by name, collection names are organized into namespaces
.
The introduction of namespaces helps prevent conflicts and improve organisation when managing Ansible content.
In the early stages of Ansible’s development, all modules, plugins, and roles were structured in a flat manner, leading to several issues:
Naming Conflicts: Different developers might create modules or roles with the same name, causing conflicts and confusion.
Difficult Organisation and Management: As the Ansible ecosystem expanded, the growing number of resources became harder to manage without a structured hierarchy.
With namespaces, Ansible creates separate logical spaces for different sources, such as individual developers or organisations. Using the structure namespace.collection_name.module_name, resources are ensured to be unique
while also improving code readability and maintainability.
The namespace is the first part of a collection name. For example, all the collections that the Ansible community maintain
are in the community namespace, and have names such ascommunity.crypto
, community.postgresql
, and community.rabbitmq
. Collections that Red Hat maintain
and support might use the redhat namespace, and have names such as redhat.rhv
, redhat.satellite
, and redhat.insights
.
Names of namespaces are limited to ASCII lowercase letters
, numbers
, and underscores
, must be at least two characters long
, and must not start with an underscore
.
Accessing Ansible Content Collection Documentation
Use the ansible-navigator collections
command to list the collections available in automation execution environments. Type a colon before the collection number, you could check the number x line available module. Enter the module number to access its documentation.
1 | Name Version Shadowed Type Path |
Use the following command to display the help documentation in plain text format:
1 | ansible-navigator doc <module_name> -m stdout |
E.g.
1 | [student@workstation ~]$ ansible-navigator doc redhat.insights.insights_register \ |
Using Ansible Content Collections in Playbooks
To use a module or a role from a collection, refer to it with its fully qualified collection name
(FQCN).
For example, use redhat.insights.insights_register to refer to the insights_register module.
1 |
|
The play in the following playbook uses the organizations role from the redhat.satellite collection.
1 |
|
Many Ansible Content Collections also use this redirection mechanism
to translate old short names into Fully Qualified Collection Names (FQCN).
For example, the acl module is now part of the ansible.posix collection, and it uses ansible.posix.acl as its FQCN.
Using the Built-in Ansible Content Collection
Ansible always includes a special collection named ansible.builtin. This collection includes a set of common modules, such as copy
, template
, file
, yum
, command
, and service
.
You can use the short names
of these modules in your playbooks. For example, you can use file
to refer to the ansible.builtin.file
module.
However, Red Hat recommends that you use the FQCN notation to prevent conflicts with collections that might use the same module names.