AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Galaxy: Install, Create & Share Roles and Collections (Guide)

By Luca Berton · Published 2024-01-01 · Category: installation

Complete guide to Ansible Galaxy. Install roles and collections, create your own, publish to Galaxy, manage requirements.yml. Practical command-line examples.

Ansible Galaxy: Install, Create & Share Roles and Collections (Guide)

What Is Ansible Galaxy?

Ansible Galaxy is the community hub for finding, sharing, and downloading Ansible roles and collections. Think of it as the npm/PyPI for Ansible — a registry of reusable automation content.

See also: Ansible Galaxy: Install Collections & Roles — Complete Guide (2026)

Galaxy Website vs CLI

  • galaxy.ansible.com — Web interface to browse and search content
  • ansible-galaxy — CLI tool to install and manage content locally

Roles vs Collections

FeatureRolesCollections
ContainsTasks, handlers, vars, templates, filesModules, roles, plugins, docs
Namespaceauthor.role_namenamespace.collection
Installansible-galaxy role installansible-galaxy collection install
DistributionGalaxy, GitGalaxy, Automation Hub
Standard sinceAnsible 1.xAnsible 2.9+
Collections are the modern distribution format. They bundle modules, roles, and plugins together.

Installing Content from Galaxy

Install a Role

# Install from Galaxy
ansible-galaxy role install geerlingguy.docker

# Install specific version
ansible-galaxy role install geerlingguy.docker,6.1.0

# Install to custom path
ansible-galaxy role install geerlingguy.docker -p ./roles/

# Install from Git
ansible-galaxy role install git+https://github.com/user/repo.git

Install a Collection

# Install from Galaxy
ansible-galaxy collection install community.general

# Install specific version
ansible-galaxy collection install community.general:9.0.0

# Install from Automation Hub
ansible-galaxy collection install ansible.netcommon \
  --server https://console.redhat.com/api/automation-hub/

# Install from tarball
ansible-galaxy collection install community-general-9.0.0.tar.gz

Requirements File

Create requirements.yml for reproducible installs:

---
roles:
  - name: geerlingguy.docker
    version: "6.1.0"
  - name: geerlingguy.nginx
  - src: https://github.com/user/custom-role.git
    version: main

collections:
  - name: community.general
    version: ">=9.0.0"
  - name: ansible.posix
  - name: community.docker

Install everything:

ansible-galaxy install -r requirements.yml
ansible-galaxy collection install -r requirements.yml

See also: Ansible Creator CLI: Scaffold Collections, Roles & Projects (v26.4.0)

CollectionPurposeModules
community.generalGeneral-purpose modules200+
ansible.builtinCore modules (included)70+
ansible.posixPOSIX system management20+
community.dockerDocker management15+
amazon.awsAWS cloud modules100+
azure.azcollectionAzure modules80+
community.vmwareVMware management150+
community.windowsWindows modules50+
ansible.netcommonNetwork automation base20+
community.kubernetesK8s management10+
AuthorRolePurpose
geerlingguydockerInstall Docker
geerlingguynginxInstall/configure nginx
geerlingguyjavaInstall Java
geerlingguypostgresqlPostgreSQL server
geerlingguysecurityLinux security hardening
robertdebockbootstrapSystem bootstrapping

Creating Your Own Role

# Generate role skeleton
ansible-galaxy role init my_role

# Directory structure created:
my_role/
├── README.md
├── defaults/
│   └── main.yml      # Default variables (lowest priority)
├── files/             # Static files
├── handlers/
│   └── main.yml      # Handlers
├── meta/
│   └── main.yml      # Role metadata (dependencies, platforms)
├── tasks/
│   └── main.yml      # Main task list
├── templates/         # Jinja2 templates
├── tests/
│   ├── inventory
│   └── test.yml
└── vars/
    └── main.yml       # Role variables (high priority)

Using Your Role

---
- name: Apply my role
  hosts: webservers
  roles:
    - my_role
    - role: my_role
      vars:
        port: 8080

Creating a Collection

# Create collection skeleton
ansible-galaxy collection init my_namespace.my_collection

# Structure:
my_namespace/my_collection/
├── docs/
├── galaxy.yml          # Collection metadata
├── meta/runtime.yml
├── plugins/
│   ├── modules/        # Custom modules
│   ├── filter/         # Filter plugins
│   └── lookup/         # Lookup plugins
├── roles/              # Bundled roles
└── README.md

# Build the collection
ansible-galaxy collection build

# Publish to Galaxy
ansible-galaxy collection publish my_namespace-my_collection-1.0.0.tar.gz

See also: Ansible Bullhorn #223: ansible-core Releases, New Collections & AI Collaboration

Managing Installed Content

# List installed roles
ansible-galaxy role list

# List installed collections
ansible-galaxy collection list

# Remove a role
rm -rf ~/.ansible/roles/geerlingguy.docker

# Verify collection
ansible-galaxy collection verify community.general

Best Practices

  1. Pin versions in requirements.yml for reproducibility
  2. Use collections over standalone roles for new projects
  3. Check Galaxy ratings and downloads before installing
  4. Vendor dependencies for air-gapped environments
  5. Use Automation Hub for certified, supported content (AAP)
  6. Keep requirements.yml in version control
  7. Test roles with Molecule before publishing

FAQ

Is Ansible Galaxy free?

Yes. The Galaxy website and CLI are free to use. Automation Hub (certified content) requires an AAP subscription.

What is the difference between Galaxy and Automation Hub?

Galaxy is community-maintained (anyone can publish). Automation Hub provides Red Hat-certified and supported content for enterprise customers.

Where are Galaxy roles installed?

Default: ~/.ansible/roles/. Override with -p flag or roles_path in ansible.cfg.

How do I use a collection module in a playbook?

Use the FQCN: community.general.timezone instead of just timezone.

Conclusion

Ansible Galaxy is essential for productive Ansible automation. Don't reinvent the wheel — leverage the thousands of community roles and collections available.

For more tutorials, visit AnsiblePilot.

Install a Role

ansible-galaxy role install geerlingguy.docker
ansible-galaxy role install geerlingguy.nginx --roles-path ./roles

Install a Collection

ansible-galaxy collection install community.general
ansible-galaxy collection install amazon.aws
ansible-galaxy collection install community.docker --force

Requirements File

# requirements.yml
---
roles:
  - name: geerlingguy.docker
    version: "7.4.0"
  - name: geerlingguy.nginx

collections:
  - name: community.general
    version: ">=9.0.0"
  - name: amazon.aws
  - name: community.docker
ansible-galaxy install -r requirements.yml
ansible-galaxy collection install -r requirements.yml

Search Galaxy

ansible-galaxy role search nginx
ansible-galaxy role info geerlingguy.nginx
ansible-galaxy collection list  # Show installed

Use Roles in Playbooks

- hosts: webservers
  become: true
  roles:
    - geerlingguy.docker
    - role: geerlingguy.nginx
      vars:
        nginx_vhosts:
          - listen: "80"
            server_name: "example.com"

Use Collections

- hosts: all
  tasks:
    # FQCN (recommended)
    - community.general.timezone:
        name: UTC

    # Or with collections keyword
    - ansible.builtin.debug:
        msg: "Using builtin"

Create Your Own Role

ansible-galaxy role init my_role
my_role/
├── README.md
├── defaults/main.yml    # Default variables
├── files/               # Static files
├── handlers/main.yml    # Handlers
├── meta/main.yml        # Role metadata
├── tasks/main.yml       # Main tasks
├── templates/           # Jinja2 templates
├── tests/
└── vars/main.yml        # Role variables

Create a Collection

ansible-galaxy collection init myorg.mytools
myorg/mytools/
├── galaxy.yml          # Collection metadata
├── plugins/
│   ├── modules/        # Custom modules
│   ├── filters/        # Custom filters
│   └── lookup/         # Lookup plugins
├── roles/              # Bundled roles
├── playbooks/          # Playbooks
└── docs/

Publish to Galaxy

# Build collection
ansible-galaxy collection build

# Publish
ansible-galaxy collection publish myorg-mytools-1.0.0.tar.gz --api-key YOUR_KEY

Private Repositories

# ansible.cfg
[galaxy]
server_list = private_hub, galaxy

[galaxy_server.private_hub]
url = https://hub.internal.com/api/galaxy/
token = your-token

[galaxy_server.galaxy]
url = https://galaxy.ansible.com/
CollectionPurpose
community.generalGeneral utilities
amazon.awsAWS management
community.dockerDocker automation
ansible.posixPOSIX systems
ansible.windowsWindows management
community.mysqlMySQL/MariaDB
community.postgresqlPostgreSQL
ansible.netcommonNetwork common

FAQ

Roles vs Collections?

Roles bundle tasks/templates for a single purpose. Collections bundle roles, modules, plugins, and playbooks as a distribution package.

How do I pin versions?

In requirements.yml: version: "==2.0.0" for exact, version: ">=2.0.0,<3.0.0" for range.

Offline installation?

# Download
ansible-galaxy collection download community.general -p ./offline/
# Install from local
ansible-galaxy collection install ./offline/community-general-9.0.0.tar.gz

Install a Role

ansible-galaxy role install geerlingguy.docker
ansible-galaxy role install geerlingguy.nginx

Install a Collection

ansible-galaxy collection install community.docker
ansible-galaxy collection install amazon.aws
ansible-galaxy collection install community.general

Requirements File

# requirements.yml
---
roles:
  - name: geerlingguy.docker
    version: "7.4.0"
  - name: geerlingguy.nginx

collections:
  - name: community.docker
    version: ">=4.0.0"
  - name: amazon.aws
  - name: community.general
  - name: ansible.posix
ansible-galaxy install -r requirements.yml
ansible-galaxy collection install -r requirements.yml

List Installed

ansible-galaxy role list
ansible-galaxy collection list

Use in Playbook

# Use a role
- hosts: all
  roles:
    - geerlingguy.docker

# Use a collection module
- hosts: all
  tasks:
    - community.docker.docker_container:
        name: nginx
        image: nginx:latest

Create Your Own Role

ansible-galaxy role init my_role
# Creates:
# my_role/
# ├── defaults/main.yml
# ├── handlers/main.yml
# ├── meta/main.yml
# ├── tasks/main.yml
# ├── templates/
# ├── files/
# └── vars/main.yml

Create Your Own Collection

ansible-galaxy collection init myorg.mycollection
# Creates:
# myorg/mycollection/
# ├── galaxy.yml
# ├── plugins/
# ├── roles/
# └── docs/

Publish to Galaxy

# Build collection
ansible-galaxy collection build

# Publish
ansible-galaxy collection publish myorg-mycollection-1.0.0.tar.gz --api-key $GALAXY_API_KEY

Private Galaxy (Automation Hub)

# ansible.cfg
[galaxy]
server_list = automation_hub, galaxy

[galaxy_server.automation_hub]
url = https://hub.example.com/api/galaxy/content/published/
token = my-token

[galaxy_server.galaxy]
url = https://galaxy.ansible.com/

Install from Git

# requirements.yml
roles:
  - src: https://github.com/myorg/my-role.git
    version: main
    name: my-role

collections:
  - name: https://github.com/myorg/my-collection.git
    type: git
    version: main

FAQ

roles vs collections?

Roles are bundles of tasks/templates/handlers. Collections are broader packages that include roles, modules, plugins, and playbooks. Collections are the modern standard.

Where are they installed?

Roles: ~/.ansible/roles/. Collections: ~/.ansible/collections/. Override with ANSIBLE_ROLES_PATH / COLLECTIONS_PATHS.

How to pin versions?

Always pin in requirements.yml for reproducible builds. Use version: ">=4.0.0,<5.0.0" for flexible pinning.

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home