Using Ansible Galaxy for managing Ansible roles and collections

Ansible Galaxy is a hub for finding and sharing Ansible content, such as roles and collections. Roles are pre-packaged units of work that can be referenced in Ansible playbooks and immediately put to work. Collections are larger units that can contain multiple roles, modules, plugins, and other Ansible components.

Ansible is a powerful tool for automating tasks across multiple systems. However, as our Ansible projects grow, we may find ourselves repeating some common tasks or configurations in different playbooks. This can lead to code duplication, inconsistency and maintenance issues.

One way to avoid this problem is to use Ansible roles. Roles are reusable units of work that can be applied to any host or group of hosts in our inventory. Roles can contain tasks, variables, files, templates, handlers and other components that are relevant for a specific purpose.

But how do we create and manage our roles? How do we share them with others or reuse them in different projects? How do we keep them updated and secure?

This is where Ansible Galaxy comes in handy. Ansible Galaxy is a hub for finding and sharing Ansible content. It provides pre-packaged units of work known as collections. Collections are groups of roles and other content that work together to provide a complete solution for a specific domain or scenario.

For example, there is a collection called community.general that contains hundreds of roles and modules for various purposes such as cloud, database, monitoring, packaging, security and web development.

We can use Galaxy to download collections from the online repository or upload our own collections to share with the community. We can also use Galaxy to create skeleton frameworks for our roles or collections using ansible-galaxy init command.

Installing collections from Galaxy

To install collections from Galaxy, we need to use ansible-galaxy collection install command. This command takes one or more collection names as arguments and downloads them into the default collection path (~/.ansible/collections/ansible_collections ).

By default, ansible-galaxy collection install uses https://galaxy.ansible.com as the Galaxy server (as listed in the ansible.cfg file under GALAXY_SERVER). You do not need any further configuration.

Ansible Documentation

For example, if we want to install the community.general collection, we can run:

ansible-galaxy collection install community.general

This will download the latest version of the collection from Galaxy and store it under ~/.ansible/collections/ansible_collections/community/general.

We can also specify a specific version of the collection using : followed by the version number.

For example:

ansible-galaxy collection install community.general:3.8.0

This will download version 3.8.0 of the collection instead of the latest one.

To upgrade a collection to the latest available version from the Galaxy server we can use the --upgrade option:

ansible-galaxy collection install community.general --upgrade

We can also install multiple collections at once by passing a list of names separated by spaces:

ansible-galaxy collection install community.general community.mysql community.aws

This will download all three collections into their respective directories under ~/.ansible/collections/ansible_collections.

Alternatively, we can use a requirements file that contains a list of collections with their versions and sources (if not from Galaxy).

For example:

collections:
  - name: my_namespace.my_collection
    version: 1.0.0

Then we can use the -r option of ansible-galaxy to install multiple collections or roles from a requirements file.

For example:

ansible-galaxy collection install -r requirements.yml

This will download all the collections according to their sources and versions specified in the requirements file.

A requirements file for installing a role from GitHub might look like this:

roles:
  - name: geerlingguy.apache
    src: https://github.com/geerlingguy/ansible-role-apache
    scm: git
    version: master

We can also mix collections and roles in the same requirements file.

Using collections in our playbooks

Once we have installed some collections from Galaxy, we can use them in our playbooks by referencing their content using dot notation.

For example, if we want to use the apache role from the community.general collection in our playbook, we can write:

- name: Install Apache using apk
  community.general.apk:
    name: apache2
    state: present

- name: Enable mod_proxy for Apache
  community.general.apache2_module:
    state: present
    name: proxy

We can also use the collections keyword to simplify the module names:

collections:
  - community.general

tasks:
  - name: Install Apache using apk
    apk:
      name: apache2
      state: present

Using Ansible Galaxy can help us manage our Ansible roles on GitLab by allowing us to download, install, build, publish, and share them easily. We can use the ansible-galaxy command line tool to perform various operations on roles and collections, such as creating a skeleton framework with collection init, building an artifact with collection build, or installing dependencies with collection install.

By using Ansible Galaxy, we can leverage the great content from the Ansible community and improve our automation project.

Leave a comment