In the ever-evolving landscape of IT automation, Ansible has become a cornerstone for many organizations. One of the strengths of Ansible is its ability to modularize and distribute content using collections. These collections can include playbooks, roles, modules, and plugins, making it easier to manage and share automation tasks. Typically, you’d install and use collections through distribution servers like Ansible Galaxy or Pulp 3 Galaxy. However, in scenarios where enterprises operate in disconnected environments, accessing the internet, and external content repositories, can be challenging.
This is where Red Hat’s Ansible Automation Platform comes to the rescue, offering a Private Automation Hub. This in-house solution enables organizations to create and manage their own collections internally. But what if you prefer not to introduce a new tool into your environment and would rather keep things in-house using Git? Fortunately, the Ansible Automation Controller supports the ability to pull collections directly from your Git repositories. In this blog post, we’ll show you how to achieve this seamlessly.
A Glimpse into the Collections Repository
Before we dive into the technical details, let’s take a look at a sample collections repository structure. In our demo, we’ve organized collections into separate folders within the repository. Here’s what it looks like:
$ git remote -v
origin git@github.com:iamgini/ansible-collections-in-git-demo.git (fetch)
origin git@github.com:iamgini/ansible-collections-in-git-demo.git (push)
$ ls -l
total 12
drwxr-xr-x. 6 iamgini iamgini 4096 Sep 26 16:35 azure-azcollection-1.18.1
drwxr-xr-x. 11 iamgini iamgini 4096 Sep 26 17:39 community-general-7.4.0
-rw-r--r--. 1 iamgini iamgini 26 Sep 26 16:37 README.md
In this demo, there are two collections, each residing in a separate folder:
- azure-azcollection-1.18.1
- community-general-7.4.0
The structure is intuitive and easy to navigate, making it simple to manage your collections within your Git repository.
Configuring requirements.yaml
in the Project Repository
The magic happens when you configure your collections/requirements.yml
file in your playbook project repository. This is where you specify how the Ansible Automation Controller should fetch the collections. Take a look at this example:
$ cat collections/requirements.yml
collections:
- name: https://github.com/iamgini/ansible-collections-in-git-demo.git#/azure-azcollection-1.18.1/
type: git
# version: devel
- name: https://github.com/iamgini/ansible-collections-in-git-demo.git#/community-general-7.4.0/
type: git
# version: devel
In this configuration:
type: git
explicitly tells Ansible to fetch the collections from a Git repository.- The
name
field contains the Git URL, and the path to your specific collection within the Git repository is denoted by#/collection-path/
.
With this setup, the Ansible Automation Controller knows where to look for your collections.
Creating a Project and Syncing
Now that your requirements.yaml
file is properly configured, and you can create a project in Ansible Automation Controller. This project links to your playbook project repository. Once created, initiate a sync operation. During this process, the controller will reach out to your Git repository and pull in the collections based on the paths you defined.
Seeing it in Action
As a technical enthusiast, you’d love to see the magic happen. When you initiate the sync, you can actually visualize the controller pulling collections directly from your Git repository. Here’s a screenshot of what it looks like in action:
When I sync the project to fetch the latest playbooks, the controller will fetch the collection as well; see the highlighted area of screenshot.
If you want to see the details, we can check the logs by clicking on the specific tasks; for example the task “Fetch galaxy collections from collections/requirements.(yml/yaml)” here. The logs clearly explain that the collections were successfully fetched from another repo (our collection repo) and installed into the project directory (on the Automation controller) successfully. See the screenshot below for reference.
Note: Remember to add the Git credential if the repositories are private.
Wrapping it Up
In conclusion, utilizing Ansible collections from a Git server with an Ansible Automation Controller is a powerful way to manage your automation content, especially in disconnected environments. By simply configuring your requirements.yaml
with the Git repository information and path to the collection, you can seamlessly integrate your Git-hosted collections into your automation workflows.
Happy automating!