Package and distribute automation content with collections
Collections are a distribution format for Ansible content that can include playbooks, roles, modules, and plugins. Red Hat provides Ansible Content Collections on Ansible automation hub that contain both Red Hat Ansible Certified Content and Ansible validated content.
If you have installed private automation hub, you can create collections for your organization and push them to private automation hub so that you can use them in job templates in Ansible Automation Platform. You can use collections to package and distribute plug-ins. These plug-ins are written in Python.
You can also create collections to package and distribute Ansible roles, which are expressed in YAML. You can also include playbooks and custom plug-ins that are required for these roles in the collection. Typically, collections of roles are distributed for use within your organization.
Understand collections for distributing roles
An Ansible role is a self-contained unit of Ansible automation content that groups related tasks and associated variables, files, handlers, and other assets in a defined directory structure.
You can run Ansible roles in one or more plays, and reuse them across playbooks. Invoking roles instead of tasks simplifies playbooks. You can migrate existing standalone roles into collections, and push them to private automation hub to share them with other users in your organization. Distributing roles in this way is a typical way to use collections.
With Ansible collections, you can store and distribute multiple roles in a single unit of reusable automation. Inside a collection, you can share custom plug-ins across all roles in the collection instead of duplicating them in each role.
You must move roles into collections if you want to use them in Ansible Automation Platform.
You can add existing standalone roles to a collection, or add new roles to it. Push the collection to source control and configure credentials for the repository in Ansible Automation Platform.
Plan your collection
Organize smaller bundles of curated automation into separate collections for specific functions, rather than creating one big general collection for all of your roles.
For example, you could store roles that manage the networking for an internal system called myapp in a company_namespace.myapp_network collection, and store roles that manage and deploy networking in AWS in a collection called company_namespace.aws_net.
Prerequisites
You must install and configure the essential software and accounts listed in this section before you proceed.
- VS Code and the Ansible extension
- Microsoft Dev Containers extension in VS Code
- Ansible development tools
- A containerization platform, for example Podman, Podman Desktop, Docker, or Docker Desktop
- A Red Hat account and log in access to the Red Hat container registry at registry.redhat.io. For information about logging in to registry.redhat.io, see Authenticating with the Red Hat container registry.
Generate the collection structure for roles
Scaffold a collection using the Ansible extension in VS Code. This process creates the necessary directory structure for packaging and distributing your roles and plug-ins.
About this task
Procedure
Results
The following message appears in the Logs pane of the Create Ansible collection tab.
--------------------- ansible-creator logs ---------------------
Note: collection company_namespace.myapp_network created at /path/to/collections/directoryThe following directories and files are created in your collections/ directory:
├── .devcontainer
├── .github
├── .gitignore
├── .isort.cfg
├── .pre-commit-config.yaml
├── .prettierignore
├── .vscode
├── CHANGELOG.rst
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING
├── LICENSE
├── MAINTAINERS
├── README.md
├── changelogs
├── devfile.yaml
├── docs
├── extensions
├── galaxy.yml
├── meta
├── plugins
├── pyproject.toml
├── requirements.txt
├── roles
├── test-requirements.txt
├── tests
└── tox-ansible.iniMigrate existing roles to your collection
Migrate existing standalone roles into the roles/ directory of your new collection. You must rename roles to remove hyphens and update playbooks to use the fully qualified collection name.
About this task
my_role
├── README.md
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.ymlAn Ansible role has a defined directory structure with seven main standard directories. Each role must must include at least one of these directories. You can omit any directories the role does not use. Each directory contains a main.yml file.
Procedure
Create a new role in your collection
Create a new Ansible role within your collection by copying the default role directory.
About this task
Procedure
- To create a new role, copy the default
runrole directory that was scaffolded when you created the collection. - Define the tasks that you want your role to perform in the
tasks/main.ymlfile. If you are creating a role to reuse tasks in an existing playbook, copy the content in the tasks block of your playbook YAML file. Remove the whitespace to the left of the tasks. Useansible-lintin VS Code to check your YAML code. - If your role depends on another role, add the dependency in the
meta/main.ymlfile.
Add documentation for your roles collection
It is important to provide documentation for your roles and roles collection, so that other users understand what your roles do and how to use them.
About this task
Procedure
Publish your collection in private automation hub
Publish your collection by packaging it into a tarball and uploading it to a namespace in private automation hub. This makes the collection available for internal use in Ansible Automation Platform projects.
Before you begin
- Package your collection into a tarball.
- Format your collection file name as follows
<my_namespace-my_collection-x.y.z.tar.gz>.For example,company_namespace-myapp_network-2.0.0.tar.gz
Procedure
- Create a namespace for your collection in private automation hub. See Creating a namespace .
- Optional: Add information to your namespace. See Adding additional information and resources to a namespace in Managing automation content .
- Upload your roles collections tarballs to your namespace. See Uploading collections to your namespaces in Managing automation content .
- Approve your collection for internal publication. See Uploading collections to your namespaces in Managing automation content.
Use your collection in projects in Red Hat Ansible Automation Platform
To use your collection in automation controller projects, add the collection to a custom execution environment. Then tag the new image and push it to private automation hub.
About this task
The following procedure describes the workflow to add a collection to an execution environment. Refer to Customizing an existing automation executions environment image for the commands to execute these steps.
Procedure
- You can pull an execution environment base image from automation hub, or you can add your collection to your own custom execution environment.
- Add the collections that you want to include in the execution environment.
- Build the new execution environment.
- Verify that the collections are in the execution environment.
- Tag the image and push it to private automation hub.
- Pull your new image into your automation controller instance.
- The playbooks that use the roles in your collection must use the fully qualified domain name (FQDN) for the roles.