Create more complex Ansible playbooks by installing pre-built content from the Ansible Galaxy.
If you’ve worked with Ansible, you know that the majority of your time spent is in writing content: Playbooks, roles, modules, plugins, etc. That content can get complicated, which means you’re not working very efficiently.
However, imagine if you could quickly download pre-packaged content for your own use. Playbooks for specific purposes, tools, demos–all by way of a single, built-in command.
Believe it or not, that’s actually possible with any Ansible installation newer than version 2.9 and I’m going to show you just how easy it is.
SEE: How smart tech is transforming the transportation industry (TechRepublic Premium)
What you’ll need
The only thing you’ll need to make this work is a running instance of Ansible. It won’t matter the platform, but I’ll be demonstrating on Ubuntu Server 18.04. As long as you have Ansible up and running, this should work.
How to install a collection
You don’t so much “install” a collection as you download them. The package of files downloads to your ~/.ansible/collections/ directory, but you don’t want to just download them with a browser or the wget tool. Why? Because you already have the built-in tool for the job and you want to make sure you’re only getting this content from a reputable site. Since the ansible tool in question pulls the content from the official Ansible servers, you should be able to trust what you’re getting.
So how do you do this? Simple. The first thing you’ll want to do is point a web browser to the Ansible Galaxy site. From this page, you can select a category or search for the content you need. Say you’re looking for a particular database. Click Database and you’ll see all the relevant content (Figure A).
Let’s install the MongoDB content. To do that, click on the MongoDB entry to reveal the necessary command to install the content (Figure B).
As you can see, the collection page contains plenty of information, such as the command for installation, as well as what the collection includes. If this collection sounds like what you need, log in to your Ansible hosting server, and install it with the command:
ansible-galaxy collection install joelwking.mongodb
Once the collection has installed, you can view the README.md file with the command:
In that file, you should find all the information necessary to use the installed content. You can use the tree command to see how the content structure is laid out (Figure C).
At this point, you can view and use the content, or modify it to fit your needs. You can also reference a collection in your own Ansible playbooks, by using the Fully Qualified Domain Name (FDQN) of the collection (as in my_namespace.my_collection.mymodule), like so:
- hosts: all tasks: - my_namespace.my_collection.mymodule: option1: value
And that’s how you can use Ansible collections, from the Ansible Galaxy, to improve and expand your playbook creation. Make sure to go through the entire Galaxy catalog, so you know what types of content are available for you to implement.