Ansible is a popular open-source configuration management tool. On the other hand, Ansible Galaxy is a repository of Ansible roles.
You might only have a handful of playbooks as a beginner sysadmin. As your automation skills improve and you become more familiar with Ansible, you will learn all the best practices. As you increase your playbooks, you will also realize how important Ansible Galaxy is.
What is Ansible Galaxy?
Ansible Galaxy is a web-based open-source online repository for Ansible content (mainly roles and collections). It is almost as if you have created something new and want to share it with others.
If you have any issues with Ansible automation or need a pre-packaged solution from others to jump-start your automation projects, Ansible Galaxy can help. For example, you could have roles, collections, modules, or plugins. Many community members have already uploaded their Ansible collections and roles.
To accomplish tasks such as installing roles from Galaxy or any git SCM, creating or removing roles, or performing some tasks on the Galaxy site, we can use the ansible-galaxy command. This command comes pre-installed with Ansible, so you can use it immediately after installing Ansible.
The Galaxy project is an open-source project that allows us to host our internal Galaxy servers. To use the internal Galaxy server, you will have to edit the default configuration in Ansible.cfg file with the server address of the Galaxy internal server because, by default, it uses the server address galaxy.ansible.com.
Ansible can be described as a multiplier that automates and scales infrastructures of all sizes. It can be used to manage configurations, orchestrate deployments, and other functions. Ansible is very easy to set up. Ansible can be used by even a novice sysadmin to automate infrastructure in just a few hours.
Ansible automates by using the SSH protocol. The control mechanism uses an SSH connection to communicate with its target hosts (typically Linux hosts). Windows sysadmins can still use Ansible to automate their Windows environments with WinRM instead of SSH. The control machine must still run Linux.
Ansible can perform common sysadmin tasks such as patching, updating, group and user management, provisioning, and other administrative tasks.
Ansible is the most widely used and popular configuration management, orchestration, and deployment tool today. Its simplicity is one of its main strengths. It is simple, powerful, yet agentless. This means that a new or entry-level sysadmin can automate infrastructure in just hours. Ansible allows for rapid scaling, efficiency, and cross-functionality.
Working on Ansible Galaxy
Next, let’s open the terminal and run a few commands using Ansible Galaxy. But before that, you must have the latest Ansible installed on your system.
When you have the latest Ansible installed on your system, just enter ansible-galaxy in the terminal, and you will get all the options available with the Galaxy.
Firstly, you can start with the init command and create a role. Here I am creating a role ‘testing’. Use the ls command to check if the role has been created successfully. The init command also creates the necessary files inside the testing role directory.
Ansible galaxy is a big and active community where many roles have already been created that you can directly use.
Below is the page that will open when you browse ‘galaxy.ansible.com‘, or we can say it is the homepage of the ‘Ansible Galaxy’ website. We can click on any popular category from the homepage or click on the ‘Search’ button to get more filters to apply a granular search.
For example, if you are looking for a MySQL server role, just search for it and click on the filter button. You can see geerlingguy has maximum downloads; we can try installing this role. Click on geerlingguy MySQL.
You will then get the installation command to install this role using ansible-galaxy.
Run the command mentioned in the above snapshot to download and install the MySQL server role on your machine.
The ansible-galaxy command manages roles and collections using galaxy.ansible.com. Below are some very helpful ansible-galaxy commands that every sysadmin uses from time to time:
ansible-galaxy list: This galaxy command displays a list of all the installed roles, with their version numbers.
ansible-galaxy remove:This galaxy command removes an installed role on the system.
ansible-galaxy info: This galaxy command will provide detailed information about Ansible Galaxy.
ansible-galaxy init: This galaxy command can be used to create a role template suitable for submission to Ansible Galaxy.
ansible-galaxy import: This galaxy command requires a login to import a role from the official galaxy website.
The above commands (init, import, install, remove) can be used with the ansible-galaxy collection command to manage collections using Ansible galaxy.
Now let’s talk about roles and collections in Ansible Galaxy.
Create Roles with Ansible Galaxy
Ansible Galaxy is basically a large public repository for Ansible roles. Roles come with READMEs that explain usage and variables. Galaxy has many roles that are continually evolving and growing.
Galaxy can add role sources such as GitHub using git. Ansible Galaxy allows us to create Ansible roles using the ansible-galaxy command. Ansible-galaxy init can be used to initialize a new galaxy role, or you can use the ansible-galaxy install <name of role> command to install the role.
Before you can use them in playbooks, the role must be downloaded. Once they have been placed in the default directory /etc/ansible/roles, you can find role examples.
Ansible Role Structure and its significance
Below are the files and directories created when a role is installed:
README.md is the Readme file that describes the role. The readme file contains all details about the input parameters and example yml.
tasks – All tasks can be enlisted by the Role at tasks. The entry point for the Role is Main.yml. This is where execution begins. This file can contain other files.
Handlers – This category includes handlers that may be used in this role or any other role.
defaults – The default variables for the Role.
vars – Variables that are more important than the defaults for Role.
Files – These files can be deployed using Role.
Templates – This section contains templates that can be used via Role.
meta – Metadata can be used to define the roles in meta. This file should also include any role dependencies.
Tests – CI tests to be executed.
library – Embedding Modules or Plugins in Roles. Not created by the init command. You can also create a custom module in Python in this folder and then use it in your tasks.
Create Collections with Ansible Galaxy
Ansible Galaxy is the best tool to manage roles and has been used for years. However, you will see new features or changes in Ansible Galaxy. Ansible version 2.2.8 now includes the collection feature.
Ansible content can be distributed using collections. They are used for packaging and distributing playbooks and roles, modules, as well as plugins.
The following structure is the basis of collections:
To install a collection on your computer, you can use the ansible-galaxy collection installation command.
Notably, some subcommands can be used with ansible galaxy as well.
init: Creates a basic collection structure basedAnsible’sle’s default template or your own
build: Creates a collection artifact that can be uploaded to Galaxy or your own repository
publish: Galaxy publishes a built-collection artifact
install: Installs one or more collections
Ansible galaxy is a great way to organize and think about your ever-growing role books. Ansible Galaxy has approximately 39K community authors, 30K roles, and 1656 collections that can be used to quickly kickstart our automation project. The developer’s guide and contributor guide explains how to contribute to the existing Ansible Galaxy code, which is an open-source project.