Introduction to Ansible ANSIBLE course material · Practical exercises + MCQs · 19/02/2026
Chapter 09 · Ansible Galaxy

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).

Key idea: a playbook orchestrates. A role provides structure. A collection supplies modules + plugins + roles.
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.
All of this can be accomplished without having to write the numerous Ansible tasks yourself.


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
Good practice: put 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 via roles_path in ansible.cfg.
  • Collections: often under ~/.ansible/collections (par défaut), or via collections_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.yml for reproducibility.
  • Avoid putting everything in vars/ (otherwise users cannot easily override values).

Practical exercise: reproducible dependencies

  1. Create a requirements.yml file at the root of your Ansible project.
  2. Add a useful collection (e.g. community.general) and, if you wish, a role (e.g. geerlingguy.nginx).
  3. Install the dependencies.
  4. 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

1) ansible-galaxy is mainly used to:
  • A. Run playbooks faster
  • B. Install roles and collections
  • C. Replace SSH
2) A collection can contain:
  • A. Only playbooks
  • B. Modules + plugins + roles
  • C. Only templates
3) The purpose of requirements.yml is to:
  • A. Make dependency installation reproducible
  • B. Replace inventory.ini
  • C. Encrypt variables
Answer key
1) B · 2) B · 3) A