Ansible: Automate Everything from First Playbook to Production · Roles and Ansible Galaxy
Ansible Galaxy: Install and Use Community Roles and Collections
Pull tested community automation from Galaxy, pin versions in a requirements file, and use collection-namespaced modules for reproducible builds.
What you'll build
A requirements.yml that pins community roles and collections, then a playbook that uses modules from community.general and community.docker to manage real infrastructure.
The idea
Ansible Galaxy (galaxy.ansible.com) hosts thousands of roles and collections shared by the community. Instead of writing every task from scratch, you install proven automation and wire it together.
Roles are the classic unit: one concern (nginx, PostgreSQL, Docker). Collections are the modern packaging: a bundle of modules, roles, plugins and documentation under a namespace like community.docker or ansible.posix.
Since Ansible 2.10, most modules live in collections. The built-in ansible.builtin namespace covers basics (copy, template, service, apt), but Docker, MySQL, AWS, GCP and Kubernetes modules are in separate collections.
Always pin versions in a requirements.yml checked into your repository — the same principle as package-lock.json or poetry.lock. Without pinning, ansible-galaxy install pulls the latest version, which might introduce breaking changes.
The code
mkdir -p galaxy-demo
cat > galaxy-demo/requirements.yml <<'EOF'
roles:
- name: geerlingguy.docker
version: "7.4.1"
- name: geerlingguy.nginx
version: "3.2.0"
collections:
- name: community.general
version: ">=9.0.0,<10.0.0"
- name: community.docker
version: "3.12.2"
- name: ansible.posix
version: "1.6.2"
EOF
cat galaxy-demo/requirements.yml
echo "---"
echo "To install: ansible-galaxy install -r requirements.yml"
echo "To install collections: ansible-galaxy collection install -r requirements.yml"
Output
roles:
- name: geerlingguy.docker
version: "7.4.1"
- name: geerlingguy.nginx
version: "3.2.0"
collections:
- name: community.general
version: ">=9.0.0,<10.0.0"
- name: community.docker
version: "3.12.2"
- name: ansible.posix
version: "1.6.2"
---
To install: ansible-galaxy install -r requirements.yml
To install collections: ansible-galaxy collection install -r requirements.ymlRun this on your computer — see “Get Started” for setup.
Use collection-namespaced modules in a playbook:
cat > galaxy-demo/playbook.yml <<'EOF'
- name: Demonstrate collection modules
hosts: localhost
connection: local
gather_facts: false
tasks:
# community.general — manage systemd timers, ini files, etc.
- name: Show ini_file module from community.general
ansible.builtin.debug:
msg: >-
community.general.ini_file can manage INI config files:
set a key in a section without touching the rest of the file
# ansible.posix — POSIX utilities
- name: Show sysctl module from ansible.posix
ansible.builtin.debug:
msg: >-
ansible.posix.sysctl manages kernel parameters:
ansible.posix.sysctl: name=net.ipv4.ip_forward value=1 state=present
# community.docker — full Docker management
- name: Show docker_container module
ansible.builtin.debug:
msg: >-
community.docker.docker_container manages containers:
name=redis image=redis:7-alpine state=started ports=["6379:6379"]
EOF
cd galaxy-demo
ansible-playbook playbook.yml 2>/dev/null
Output
Run this on your computer — see “Get Started” for setup.
List installed collections:
ansible-galaxy collection list
# Collection Version
# -------------------- -------
# community.general 9.5.0
# community.docker 3.12.2
# ansible.posix 1.6.2
Common mistakes
Not pinning versions — ansible-galaxy install geerlingguy.docker without a version grabs the latest release. If 8.0 introduces breaking changes, your deploy breaks on the next CI run. Always pin in requirements.yml.
Using short module names after migrating to collections — docker_container worked before Ansible 2.10, but now the correct name is community.docker.docker_container. Short names may still resolve but produce deprecation warnings and can collide.
Installing collections globally instead of per-project — use ansible-galaxy collection install -r requirements.yml -p ./collections to install into the project directory, then set collections_paths = ./collections in ansible.cfg for isolation.
Key points
- Roles handle one concern; collections bundle modules, roles and plugins under a namespace.
- Always pin versions in
requirements.ymlfor reproducible builds. - Use fully qualified collection names (
community.docker.docker_container, notdocker_container). - Install per-project with
-p ./collectionsfor isolation between projects.
Practice questions
What is the difference between an Ansible role and a collection?
Answer
A role handles one concern (tasks, templates, handlers). A collection is a larger package that bundles modules, roles, plugins, and documentation under a namespace like community.docker.
Why should you pin versions in requirements.yml?
Answer
Without pinning, ansible-galaxy install pulls the latest version, which may introduce breaking changes. Pinning ensures every team member and CI run uses the same tested versions.
What is the fully qualified name for the Docker container module?
Answer
community.docker.docker_container — using the short name docker_container triggers deprecation warnings and may collide with other collections.
Open in the app to mark lessons complete, take notes and track your progress.