Stage 4 · Provision
Roles & Collections
Ansible Galaxy
Installing, publishing, and requirements.yml pinning.
What Is Galaxy?
Ansible Galaxy is a repository of Ansible roles and collections. It hosts thousands of community-contributed roles for configuring common software — nginx, PostgreSQL, Docker, Kubernetes, and more. Galaxy is Ansible's equivalent of npm or PyPI.
Installing Roles
# Install a single role
ansible-galaxy role install geerlingguy.nginx
# Install to a specific directory
ansible-galaxy role install -p roles/ geerlingguy.docker
# Install a specific version
ansible-galaxy role install geerlingguy.nginx,3.1.0
# Install from a tarball
ansible-galaxy role install nginx-3.1.0.tar.gzRoles install to ~/.ansible/roles by default. Use -p to install to a project-local roles/ directory.
requirements.yml
requirements.yml pins role and collection versions for reproducible deployments. Store it alongside your playbooks and install dependencies before running.
---
roles:
- name: geerlingguy.nginx
version: "3.1.0"
- name: geerlingguy.docker
version: "6.1.0"
- name: geerlingguy.postgresql
version: "3.5.0"
collections:
- name: community.general
version: ">=7.0.0"
- name: community.postgresql
version: "3.4.0"Pin versions in production. Use version ranges for development. Run ansible-galaxy install -r requirements.yml to install all dependencies.
# Install all roles and collections
ansible-galaxy install -r requirements.yml
# Install roles only
ansible-galaxy role install -r requirements.yml
# Install collections only
ansible-galaxy collection install -r requirements.yml
# Force reinstall
ansible-galaxy install -r requirements.yml --forceAlways run this in CI before executing playbooks. This ensures all dependencies are available and at the correct versions.
Publishing Roles
# Create a GitHub token at https://github.com/settings/tokens
# Add it to Galaxy at https://galaxy.ansible.com/me/prefs/token/
# Import from GitHub
ansible-galaxy role import <github_user> <github_repo>
# Or push directly
ansible-galaxy role import --role-name my-role /path/to/roleGalaxy imports roles from GitHub repositories. Tag releases on GitHub and Galaxy will track versions automatically.
Galaxy CLI Commands
| Command | Purpose |
|---|---|
| ansible-galaxy role install | Install a role |
| ansible-galaxy collection install | Install a collection |
| ansible-galaxy role list | List installed roles |
| ansible-galaxy role info role_name | Show role details |
| ansible-galaxy role delete role_name | Remove an installed role |
| ansible-galaxy import user repo | Import from GitHub |
Community roles can change. Fork the repository before importing, then maintain your fork. This gives you control over updates and patches.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.