AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Publishing Ansible Collections to Ansible Galaxy and Automation Hub

By Luca Berton · Published 2024-01-01 · Category: installation

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

Publishing Ansible Collections 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.

See also: Ansible Development: Write Custom Modules, Plugins & 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.

See also: Ansible Error 'Use loop or with_': Fix Deprecated with_items Syntax

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@example.com -

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.

See also: Running a Playbook with JetPorch: A Quick Guide

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.

Related Articles

publishing collections to Ansible Galaxythe Ansible roles overview

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home