What is Ansible Galaxy and how do you reuse community roles?
Learn what Ansible Galaxy is, how to install community roles with requirements.yml, override role variables, and distinguish roles from collections.
Expected Interview Answer
Ansible Galaxy is the official public hub and command-line tool for finding, sharing, and installing reusable Ansible content — roles and collections — so you don't have to write common automation from scratch.
You reuse community roles by declaring them in a requirements.yml file and installing them with `ansible-galaxy install -r requirements.yml`, which downloads them into your roles path. A role packages tasks, handlers, variables, templates, and defaults in a standard directory layout, so once installed you simply reference it in a playbook's `roles:` list. Modern Galaxy content is increasingly distributed as collections (namespace.collection) that bundle roles, modules, and plugins together.
- Avoids reinventing common automation like installing nginx or configuring users
- Standard directory structure makes roles portable and predictable
- Version pinning in requirements.yml keeps builds reproducible
- Collections bundle modules and plugins alongside roles
- Community review and popularity signals help you pick trustworthy content
AI Mentor Explanation
Ansible Galaxy is like a shared library of pre-set field placements and bowling plans that captains from every club upload. Instead of drafting a slip cordon from scratch before each match, you download a proven plan by its author's name and version, drop it into your team sheet, and adapt only the parts unique to today's pitch — reusing collective expertise rather than rebuilding it.
Step-by-Step Explanation
Step 1
Search Galaxy
Browse galaxy.ansible.com or run `ansible-galaxy search` to find a role or collection that fits your need.
Step 2
Declare requirements
List the role or collection with its source and version in a requirements.yml file for reproducibility.
Step 3
Install content
Run `ansible-galaxy install -r requirements.yml` (or `collection install`) to download it into your roles/collections path.
Step 4
Reference in a play
Add the role under a play's `roles:` key or call `import_role`/`include_role`, passing variables to customize behavior.
Step 5
Override with variables
Set your own values in group_vars, host_vars, or the play to adapt the role without editing its files.
Step 6
Pin and commit
Keep requirements.yml under version control with pinned versions so every environment installs the same content.
What Interviewer Expects
- Knows Galaxy is both a website and the ansible-galaxy CLI
- Can explain requirements.yml and version pinning
- Understands the standard role directory layout
- Distinguishes roles from collections
- Mentions overriding role defaults with variables rather than editing the role
Common Mistakes
- Confusing Ansible Galaxy with Ansible Tower/AWX
- Editing installed role files directly instead of overriding variables
- Not pinning versions, leading to non-reproducible builds
- Thinking Galaxy only serves roles and missing collections
- Ignoring the roles path so installed roles aren't found at runtime
Best Answer (HR Friendly)
“Ansible Galaxy is like an app store for automation — a public place where people share ready-made building blocks called roles. Instead of writing everything yourself, you download a trusted role, plug it into your setup, and adjust a few settings, which saves a lot of time.”
Code Example
# requirements.yml
roles:
- name: geerlingguy.nginx
version: "3.1.4"
collections:
- name: community.general
version: ">=8.0.0"
# Install everything declared above:
# ansible-galaxy install -r requirements.yml
# ansible-galaxy collection install -r requirements.yml- hosts: web
become: true
roles:
- role: geerlingguy.nginx
vars:
nginx_worker_processes: "2"
nginx_vhosts:
- listen: "80"
server_name: "example.com"
root: "/var/www/example"Follow-up Questions
- What is the standard directory structure of an Ansible role?
- How do collections differ from roles on Galaxy?
- How do you pin and update role versions safely?
- What is the difference between import_role and include_role?
- How would you host a private Galaxy server for internal roles?
MCQ Practice
1. Which command installs roles listed in a requirements file?
`ansible-galaxy install -r requirements.yml` reads the file and downloads each declared role into the roles path.
2. What is a primary benefit of pinning a role version in requirements.yml?
Pinning a version ensures every environment installs the exact same role code, keeping builds reproducible.
3. Galaxy collections bundle roles together with what else?
A collection packages roles, modules, and plugins under a namespace.collection identifier.
Flash Cards
What is Ansible Galaxy? — The public hub and ansible-galaxy CLI for finding, sharing, and installing reusable roles and collections.
How do you install multiple roles at once? — Declare them in requirements.yml and run `ansible-galaxy install -r requirements.yml`.
Role vs collection? — A role packages tasks/handlers/vars/templates; a collection bundles roles plus modules and plugins under a namespace.
How do you customize a community role? — Override its default variables in the play, group_vars, or host_vars — never edit the role's files directly.