Ansible

Ansible Collections

Ansible collections refer to a distributable format of an Ansible content. Ansible collections allow us to package and distribute an Ansible content such as playbooks, roles, modules, and plugins. This is essential when sharing a code with other teams as it provides a modular way of sharing a functionality across Ansible playbooks.

In this tutorial, we will explore the fundamentals of working with Ansible collections using Ansible Galaxy.

Requirements:

To follow along with this post, ensure that you have the following:

  1. Installed Ansible 
  2. Access to the Ansible Galaxy

Installing Collections with Ansible Galaxy

The best way to install the Ansible collections is using the publicly hosted Ansible collections repository at https://galaxy.ansible.com.

You can visit the repository and search for the collection that you wish to use. You can install it using the “ansible-galaxy” command.

$ ansible-galaxy collection install my_namespace.my_collection

You can also upgrade an already installed collection using the “–upgrade” option as shown in the following command syntax:

$ ansible-galaxy collection install my_namespace.my_collection --upgrade

To install a specific version of a given collection, you can specify the target version as follows:

$ ansible-galaxy collection install my_namespace.my_collection:==<version>

Installing Multiple Collections

Ansible also allows us to configure a “requirements.yml” file to install multiple collections in a single command.

The YAML file follows a format as shown in the following:

---
collections:
- my_namespace.my_collection
# specifying versions.
- name: my_namespace.my_other_collection
  version: ">=1.2.0"

You can specify the following parameters in each collection entry:

  • name
  • version
  • signatures
  • source
  • type

An example syntax is as follows:

collections:
  - name: namespace.name
    version: 1.0.0
    type: galaxy
    signatures:
      - https://examplehost.com/detached_signature.asc
      - file:///path/to/local/detached_signature.asc

Ansible List Collections

To view the information about the installed collections, you can use the “ansible-galaxy” command as follows:

$ ansible-galaxy collection list

The command should return a list of all the installed collections.

Using Collections in a Playbook

Once you installed a given collection, you can use it as a playbook by referring to the collection content using its fully qualified collection name or FQCN.

The syntax is as follows:

- hosts: all
  tasks:
    - my_namespace.my_collection.mymodule:
        option1: value

Conclusion

This tutorial taught us the basics of configuring and working with Ansible collections using the Ansible Galaxy. Ansible collections allow you to organize and modularize your Ansible workflows in reusable sections.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list