When you think of Red Hat’s open source IT automation software Ansible, what comes to mind? Automation? Efficient deployment across a landscape of hardware? Or are you filled with dread at having to write playbooks that get more and more complex with each task?
In fact, the majority of your time spent with Ansible will be in the construction of playbooks. For some, this is a great opportunity to flex and improve your developer and administrator skills. To others, however, it can be an exercise in frustration and hair loss.
And the more complex the task at hand, the more complicated those playbooks will get.
Ansible is an open source platform used for software provisioning, configuration management, and application deployment. From a single point of entry you can manage and deploy Linux and Windows desktops and servers. Ansible uses a declarative language to describe system configurations, called Playbooks. By constructing proper Playbooks, there’s almost no limit to what you can do with Ansible. Those playbooks, however, can get very complex.
That’s where Ansible Collections come into play.
Starting with Ansible 2.9, released last year, that’s not only possible, but the construction of those playbooks has also gotten slightly easier. With the help of Ansible Collections, you have an entire galaxy of content you can make use of.
Ansible Collections are pre-packaged content that can be used as-is or modified to meet your needs. The content found in Ansible Collections includes those for specific purposes, tools, and even demos to help you learn the ins and outs of Ansible. For anyone new to or even highly adept with Ansible, these collections of complex content can be referenced from within your own playbooks, to help extend your provisioning abilities.
I want to show you how to use Ansible Collections.
The only thing required to work with Collections is a running instance of Ansible. It doesn’t matter what platform you’re running Ansible on, so long as it’s functioning. I’ll be demonstrating on Ubuntu Server 18.04, but as long as you have a functioning Ansible system, this will work.
Locating an Ansible Collection
The first thing you must do is locate the collection you want to add to Ansible. To do that, point your browser to the Ansible Galaxy. Within this site, you’ll find collections for:
- Playbook bundles
If you click on any one of those categories, you’ll find hundreds (if not thousands) of collections to choose from. Say, for example, you want to develop with prxysql. You could click on the Database category, scroll down, and you’ll see the proxysql collection.
Installing’ a Collection
Truth be told, you don’t actually install a collection. Instead, you download the collection, using Ansible, to your local drive. Once downloaded, a collection can be found in the ~/.ansible/collections directory, where you can begin working with the included files.
Can you trust these collections? That’s an important question. As you know, Docker experienced some serious issues, when it turned out that images, hosted on Docker Hub, included malicious code or were vulnerable to hacking attempts.
Fortunately, all collections are downloaded from the official Ansible servers, so you should be able to trust what you’re getting. On top of which, you can comb through every single file in the collection, to find out if there’s rogue or malicious code.
With that out of the way, let’s download that proxysql collection. To do so, click on the proxysql. In the resulting page, you’ll see the very command necessary to install the collection (Figure 1).
To download this collection, log into your Ansible server, and issue the command:
ansible-galaxy collection install community.proxysql
Once Ansible completes the download, it will inform you where you can locate the files (Figure 3).
For this NGINX collection, you’d then change into the folder in question with the command:
Issue the ls command within this directory to reveal everything included with the collection (Figure 3).
In this instance, you’ll find a number of configuration files, as well as plugins that can be customized. The plugin files are found in plugins/modules/ and are all Python files. You will also find YAML files in roles/proxysql. To get a better view of the contents of this collection, you’d want to use the tree command. If tree is not found on your machine, you can install it from the standard repositories, like so:
- On a Debian-based system – sudo apt-get install tree -y
- On a Red Hat-based system – sudo dnf install tree -y
Issuing the tree at the root directory will display the structure of the collection (Figure 4).
Using a Collection in Your Playbook
Now that you’ve downloaded a collection, and combed through its contents, you might be wondering, “How can I use a collection from within my own playbook?” I’m glad you asked, because it’s actually quite easy.
In order to reference a collection in your Ansible playbook, you must make use of the collection’s name For instance, the name of the collection we downloaded earlier is community.proxysql. So to reference this collection in a playbook, you could use one of the following methods:
collections: name: community.proxysql:
For more information on using Ansible collections in your playbook, make sure to check out the official Ansible documentation.
If you’re looking for a way to extend your usage of Ansible into more complicated realms, Collections might well be the fastest route. Instead of re-inventing the wheel, or spending a great deal of time crafting complex content, why not simply repurpose something that has already been created and reference it from within your own playbooks. Save time and avoid frustration.
Give the Ansible Galaxy a look to see if there isn’t a number of collections that you can use to make your life a bit easier.
At this time, The New Stack does not allow comments directly on this website. We invite all readers who wish to discuss a story to visit us on Twitter or Facebook. We also welcome your news tips and feedback via email: email@example.com.