Ansible Collections – More clarity and easier sharing in Ansible

Ansible is the fastest-growing configuration management tool these days. This has also created a steadily growing community. Due to the simple architecture and the resulting easy extensibility, more and more companies and private persons provide their roles, modules and playbooks for their products and projects.

The Ansible Core project now has a four-digit number of open pull requests and issues on Github. This has become confusing, not only from Red Hat’s perspective.

For more common uses of Ansible – for example, installing the Docker Daemons or the apache web server – Red Hat created Ansible Galaxy some time ago: A place where members of the community provide generic roles for free use. Thus the wheel does not have to be reinvented every time. The community develops not only roles but also modules and other plugins for sometimes very special cases. To provide more structure and better community participation in the Ansible Project, Red Hat has created Ansible Collections: These are product or project-specific collections of playbooks, roles, modules, and plug-ins. As before, these roles are provided via the Ansible Galaxy. The introduction of collections gives companies or projects more control over the Ansible Code they provide themselves. This allows you to bring your own modules or plugins upstream faster: the pull requests are less likely to slip down the list or be forgotten. Once merged, you no longer have to wait for the next release of Ansible, but can simply include the latest version of your collection. At the same time, the Ansible Galaxy maintains the visibility of the collection. Red Hat has taken the first step in this direction by creating collections of projects. These are also maintained by official bodies. These include Ansible Tower, Amazon AWS, and Red Hat Satellite. The Ansible Modules, which we use and actively develop for our orcharhino are now also available as a collection.

Application

The handling is similar to that of the roles in the Ansible Galaxy. A collection is installed either via command line:

ansible-galaxy collection install my_namespace.my_collection:1.0.0

or, if you want to install several at once, via a requirements file:

---
collections:
# With just the collection name
- my_namespace.my_collection

# With the collection name, version, and source options
- name: my_namespace.my_other_collection
 version: 1.0.3 # version range identifiers (default: ``*``)
 source: https://galaxy.ansible.com # The Galaxy URL to pull the collection from (default: ``--api-server`` from cmdline)
...

Once the collection is installed, the modules, roles and plugins it contains can be easily integrated:

---
- hosts: all
 tasks:
 - import_role:
 name: my_namespace.my_collection.role1

 - my_namespace.mycollection.mymodule:
 option1: value

 - debug:
 msg: '{{ lookup("my_namespace.my_collection.lookup1", 'param1')| my_namespace.my_collection.filter1 }}'
...

To avoid getting your fingers sore, you can import the collection completely at the beginning of the game and then use all modules and roles (but not filters and lookup plugins!):

---
- hosts: all
 collections:
 - my_namespace.my_collection
 tasks:
 - import_role:
 name: role1

 - mymodule:
 option1: value

 - debug:
 msg: '{{ lookup("my_namespace.my_collection.lookup1", 'param1')| my_namespace.my_collection.filter1 }}'
...

Source of the code examples

In addition to the modules and plug-ins officially supported and maintained by Red Hat, the previous Ansible versions (≤2.9) also contain a large number of plug-ins and modules maintained by the community. With Ansible 2.10 all the community maintained content is separated into a community.general Collection. Ansible’s premise has always been “batteries included”, i.e. nothing needs to be installed after installing Ansible. The default installation of Ansible 2.10 still contains the > 3000 modules and plugins, so most playbooks with the upgrade to 2.10 probably require only minor changes. So this premise is still correct. The big changes happen with 2.10 under the hood. Therefore they will appear gradually as the community provides more and more collections.

To learn more about the use and creation of Ansible Collections, attend our Ansible Trainings. There will also be an Advanced Ansible Workshop at our OSAD, where we will go into collections in more detail.

Ansible Training

The Ansible Training – “Fast Track” teaches participants how to manage infrastructures with Ansible as well as the basic concepts and best practices of Ansible. The “Ansible Extended Training” also offers an additional day where the trainer can specifically address individual questions and needs of the participants.

The following two tabs change content below.

ATIX-Crew

Der ATIX-Crew besteht aus Leuten, die in unterschiedlichen Bereichen tätig sind: Consulting, Development/Engineering, Support, Vertrieb und Marketing.