Ansible Galaxy is the package manager for Ansible content. Instead of writing everything from scratch, install community roles and collections.
Roles vs Collections
| Concept | Contains | Example |
|---|---|---|
| Role | Tasks, handlers, templates, vars for one purpose | geerlingguy.docker |
| Collection | Multiple roles, modules, plugins, playbooks | community.general |
Collections are the modern standard. Roles still work but collections offer more.
Installing Roles
# Install from Galaxy
ansible-galaxy role install geerlingguy.docker
ansible-galaxy role install geerlingguy.nginx
# Install specific version
ansible-galaxy role install geerlingguy.docker,6.1.0
# Install to custom path
ansible-galaxy role install geerlingguy.docker -p ./roles/
# List installed roles
ansible-galaxy role listUse in Playbook
---
- hosts: webservers
roles:
- geerlingguy.docker
- role: geerlingguy.nginx
vars:
nginx_vhosts:
- listen: "80"
server_name: "app.example.com"
extra_parameters: |
location / {
proxy_pass http://localhost:3000;
}Master this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →Installing Collections
# Install collection
ansible-galaxy collection install community.general
ansible-galaxy collection install community.docker
ansible-galaxy collection install amazon.aws
# Specific version
ansible-galaxy collection install community.general:>=8.0.0
# List installed
ansible-galaxy collection listUse Collection Modules
---
- hosts: all
tasks:
- name: Manage Docker container
community.docker.docker_container:
name: my-app
image: my-app:latest
state: started
ports:
- "3000:3000"
- name: Create S3 bucket
amazon.aws.s3_bucket:
name: my-backup-bucket
state: present
region: eu-west-1Requirements Files
roles/requirements.yml
---
roles:
- name: geerlingguy.docker
version: "6.1.0"
- name: geerlingguy.nginx
version: "3.2.0"
- name: geerlingguy.certbot
# From Git
- name: my-custom-role
src: https://github.com/myorg/ansible-role-custom.git
version: v1.2.0
scm: gitcollections/requirements.yml
---
collections:
- name: community.general
version: ">=8.0.0"
- name: community.docker
version: ">=3.0.0"
- name: amazon.aws
version: ">=7.0.0"
- name: ansible.posix
# From Galaxy
- name: my_namespace.my_collection
source: https://galaxy.ansible.comInstall All
ansible-galaxy role install -r roles/requirements.yml
ansible-galaxy collection install -r collections/requirements.yml
# Force reinstall
ansible-galaxy collection install -r collections/requirements.yml --forcePopular Collections
| Collection | Purpose |
|---|---|
community.general | 1000+ modules for everything |
community.docker | Docker container management |
community.kubernetes | K8s resource management |
amazon.aws | AWS services |
azure.azcollection | Azure services |
google.cloud | GCP services |
ansible.posix | POSIX system tasks |
ansible.builtin | Core modules (included by default) |
community.postgresql | PostgreSQL management |
community.mysql | MySQL management |
Get weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Creating a Role
ansible-galaxy role init my_rolemy_role/
defaults/
main.yml # Default variables (lowest priority)
files/ # Static files to copy
handlers/
main.yml # Handlers (restart services, etc.)
meta/
main.yml # Role metadata, dependencies
tasks/
main.yml # Main task list
templates/ # Jinja2 templates
tests/
vars/
main.yml # Variables (higher priority)tasks/main.yml
---
- name: Install packages
ansible.builtin.package:
name: "{{ my_role_packages }}"
state: present
- name: Deploy configuration
ansible.builtin.template:
src: config.conf.j2
dest: /etc/myapp/config.conf
owner: root
group: root
mode: "0644"
notify: Restart myapp
- name: Ensure service is running
ansible.builtin.service:
name: myapp
state: started
enabled: truedefaults/main.yml
---
my_role_packages:
- myapp
- myapp-utils
my_role_port: 8080
my_role_log_level: infometa/main.yml
---
galaxy_info:
author: Your Name
description: Install and configure MyApp
license: MIT
min_ansible_version: "2.14"
platforms:
- name: Ubuntu
versions:
- jammy
- name: EL
versions:
- "9"
dependencies:
- role: geerlingguy.dockerProject Structure
ansible-project/
ansible.cfg
inventory/
production/
hosts.yml
group_vars/
staging/
hosts.yml
group_vars/
playbooks/
site.yml
webservers.yml
roles/
requirements.yml
my_custom_role/
collections/
requirements.ymlansible.cfg
[defaults]
roles_path = ./roles
collections_path = ./collectionsWhat's Next?
Our Ansible Automation in 30 Minutes course covers Galaxy roles, collections, and building reusable automation. First lesson is free.
---
Ready to go deeper? Check out our hands-on course: Ansible Quickstart — practical exercises you can follow along on your own machine.
Ready to learn by doing?
Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.
Related Articles
Ansible Roles Reusable Automation
Structure Ansible automation with roles for reusability. Directory layout, defaults, handlers, Galaxy integration, and testing with Molecule.
Ansible vs Terraform When to Use
Ansible and Terraform solve different infrastructure problems. Learn when to use each, when to use both together, and how they complement each other.
Ansible Inventory: Static and Dynamic
Master Ansible inventory in INI and YAML formats. Learn host groups, variables, dynamic inventory plugins for AWS and Azure, and patterns for scaling.
Ansible Handlers and Notifications
Master Ansible handlers for triggered actions. Learn to restart services, flush handlers, use handler chains, and implement idempotent notification patterns.
Ansible Infrastructure as Code Guide
Use Ansible as your Infrastructure as Code tool. Learn how playbooks define, version, and automate your entire infrastructure.
Ansible Inventory Management Guide
Master Ansible inventory management with static and dynamic inventories, host groups, variables, patterns, and seamless cloud provider integration.
Explore topics
Browse more articles on the topics covered here.