Ansible Galaxy
What is ansible-galaxy ?
ansible-galaxy is Ansible’s “extension” manager.
It is used to install (and organise) ready-to-use Ansible code:
roles and, above all, collections.
In short: rather than reinventing “install nginx + configuration + service” every time,
you obtain a role or collection maintained by the community (or your team).
And
ansible-galaxy lets you install all of this without manually copying and pasting folders.
Roles vs Collections
- Role: a package of configuration-oriented content (tasks, templates, handlers, defaults…).
- Collection: a broader package (modules, plugins, roles). This has become the standard since Ansible 2.10.
# Install a role (example)
ansible-galaxy role install geerlingguy.nginx
# Install a collection (example)
ansible-galaxy collection install community.general
Additional Information about the geerlingguy.nginx role
This role allows you to:
- Install Nginx on Debian, Ubuntu, Red Hat, and other Linux distributions.
- Configure the Nginx web server.
- Create and manage virtual hosts (Virtual Hosts).
- Configure various performance settings.
- Start the Nginx service and enable it to start automatically at boot.
- Manage reverse proxy configurations.
Installing a role
Installing a role places it in your roles/ directory (or another configured role path),
after which you use it in a playbook.
# Installation
ansible-galaxy role install geerlingguy.nginx -p roles
# Check what is installed
ansible-galaxy role list
Example of use in a playbook:
- name: Web
hosts: web
become: true
roles:
- geerlingguy.nginx
Installing a collection
A collection is installed in a dedicated location (by default in the user’s environment), and its modules are called
using their fully qualified names (namespace.collection.module).
# Installation
ansible-galaxy collection install community.general
# List installed collections
ansible-galaxy collection list
Example of calling a module from a collection:
- name: Manage the time zone (example)
hosts: all
become: true
tasks:
- name: Set Europe/Paris
community.general.timezone:
name: Europe/Paris
Installing dependencies properly with requirements.yml
Manual installation is fine. Reproducible installation is better.
To achieve this, define dependencies in a requirements.yml.
You can manage both roles and collections.
Example: roles
# requirements.yml (roles)
- name: geerlingguy.nginx
- name: geerlingguy.mysql
# Install from requirements.yml
ansible-galaxy role install -r requirements.yml -p roles
Example: collections
# requirements.yml (collections)
collections:
- name: community.general
- name: ansible.posix
# Install collections from requirements.yml
ansible-galaxy collection install -r requirements.yml
requirements.yml in your repository.
That way, anyone can recreate the environment in 10 seconds.
Where are roles and collections installed?
- Roles: often in
./roles(with-p roles), or viaroles_pathinansible.cfg. - Collections: often under
~/.ansible/collections(par défaut), or viacollections_paths.
# Example ansible.cfg (optional)
[defaults]
roles_path = ./roles
[galaxy]
# (depending on version and requirements, Galaxy servers can be configured here)
Creating a role with ansible-galaxy init
ansible-galaxy is not just for “downloading”.
It can also generate a properly organised role structure for you.
# Create a role skeleton
ansible-galaxy init roles/mon_role
This automatically creates:
roles/mon_role/
tasks/main.yml
handlers/main.yml
defaults/main.yml
vars/main.yml
templates/
files/
meta/main.yml
README.md
Best practices
- Pin versions (where possible) to avoid surprises.
- Prefer collections for external modules (cloud, systems, utilities).
- Use
requirements.ymlfor reproducibility. - Avoid putting everything in
vars/(otherwise users cannot easily override values).
Practical exercise: reproducible dependencies
- Create a
requirements.ymlfile at the root of your Ansible project. - Add a useful collection (e.g.
community.general) and, if you wish, a role (e.g.geerlingguy.nginx). - Install the dependencies.
- Write a short playbook that uses at least one module from an installed collection.
Example of requirements.yml
collections:
- name: community.general
# (optional) roles if you want to manage them through Galaxy
- name: geerlingguy.nginx
Commands (model answer)
# Install the collection
ansible-galaxy collection install -r requirements.yml
# Install the role in ./roles
ansible-galaxy role install -r requirements.yml -p roles
# List
ansible-galaxy collection list
ansible-galaxy role list --roles-path .
Minimal playbook using community.general.timezone
- name: Collection demonstration
hosts: all
become: true
tasks:
- name: Set the time zone
community.general.timezone:
name: Europe/Paris
QCM
ansible-galaxy is mainly used to:
- A. Run playbooks faster
- B. Install roles and collections
- C. Replace SSH
- A. Only playbooks
- B. Modules + plugins + roles
- C. Only templates
requirements.yml is to:
- A. Make dependency installation reproducible
- B. Replace
inventory.ini - C. Encrypt variables