Publishing Ansible Collections to Ansible Galaxy and Automation Hub
How to distribute our custom Ansible Collection to Ansible Galaxy and Automation Hub.


Introduction
Ansible Collections provides a structured way to package and distribute Ansible content, offering a modular and organized approach to automation. Collections typically include modules, plugins, roles, and playbooks that address specific use cases. In this article, we’ll delve into the process of distributing Ansible Collections, covering key aspects such as configuring distribution servers, building tarballs, and publishing collections.
Distribution Servers
Before distributing an Ansible Collection, you need to choose a distribution server. The prominent options include:
- Ansible Galaxy: Supports all collections and is widely used for community-driven content.
- Pulp 3 Galaxy: Similar to Ansible Galaxy but with added support for signed collections.
- Red Hat Automation Hub: Specifically for Red Hat-certified collections, with support for signed collections.
- Privately Hosted Automation Hub: Enables distribution of collections authorized by the owners.
Distribution Process Overview
Distributing an Ansible Collection involves several steps:
- Initial Configuration of Distribution Servers:
- Create a namespace on each distribution server.
- Obtain an API token for each server.
- Specify the API token and distribution server in the configuration.
- Building the Collection Tarball:
- Review and update the version number in the
galaxy.yml
file. - Use the
ansible-galaxy collection build
command to create a tarball of the collection.
- Preparing to Publish:
- Install the collection locally for testing.
- Review the collection for any issues.
- Understand and adhere to semantic versioning for your collection.
- Publishing the Collection:
- Use either the command line (
ansible-galaxy collection publish
) or the distribution server’s website to upload and publish the collection.
Initial Configuration of Distribution Servers
Creating a Namespace
Create a namespace on each distribution server. Note that Ansible Galaxy namespaces cannot include hyphens.
Getting Your API Token
Obtain a separate API token for each distribution server. Retrieve the token from the respective server, such as the Galaxy profile preferences page.
- API token for Galaxy, go to the Galaxy profile preferences (https://galaxy.ansible.com/me/preferences) page and click API Key.
- API token for Automation Hub, go to the token page (https://cloud.redhat.com/ansible/automation-hub/token/) and click Load token.
Specifying Your API Token and Distribution Server
Specify the API token and distribution server either in the ansible.cfg
file or by passing them as arguments to the ansible-galaxy
command. Configuring in the ansible.cfg
file is more secure.
[galaxy]
server_list = ansible_galaxy
[galaxy_server.ansible_galaxy]
url=https://galaxy.ansible.com/
token=abcdefghijklmnopqrtuvwxyz
Building the Collection Tarball
After configuring distribution servers, build a collection tarball using the ansible-galaxy collection build
command. Ensure you review and update the version number in the galaxy.yml
file.
Ignoring Files and Folders
Exclude unnecessary files using the build_ignore key in the galaxy.yml
file or leverage manifest directives for more control over exclusion patterns.
Signing a Collection
For Pulp 3 Galaxy servers, consider including a GnuPG signature with your collection for added security.
ansible-galaxy collection build
tar -Oxzf namespace-name-1.0.0.tar.gz MANIFEST.json | gpg --output namespace-name-1.0.0.asc --detach-sign --armor --local-user [email protected] -
Preparing to Publish Your Collection
Before publishing, install the collection locally, review it for issues, and ensure the version number is correctly incremented following semantic versioning.
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Publishing Your Collection
Publishing can be done either from the command line using ansible-galaxy collection publish
or directly on the distribution server’s website.
Command Line Publishing
Execute ansible-galaxy collection publish
with the path to the collection tarball. Ensure your API token is correctly configured.
Website Publishing
On the distribution server’s website, navigate to the upload section and select the collection tarball. This triggers an import process, and you can monitor the progress on the My Imports page.
Conclusion
Distributing Ansible Collections involves careful configuration, versioning, and adherence to best practices. Whether using popular platforms like Ansible Galaxy or Red Hat Automation Hub, understanding the process ensures seamless sharing and consumption of Ansible automation content.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate