# Collections and Ansible Galaxy — Ansible: Automate Servers with Playbooks

Source: https://www.geekswithgeeks.com/en/ansible/role-collections-galaxy

> Install shared content and pin versions with a requirements file.

## Do not reinvent

Modern Ansible content is distributed as **collections**: bundles of modules, plugins and roles, named `namespace.collection` (for example `community.general` or `amazon.aws`). **Ansible Galaxy** and Red Hat's Automation Hub host them. List what your project needs in **`requirements.yml`** with versions and install with `ansible-galaxy collection install -r requirements.yml`, so every developer and CI job uses the same content. Review third-party roles before using them, since they run with your privileges.

## requirements.yml

Pinning versions makes runs reproducible. Version numbers here are examples.

```yaml
---
collections:
  - name: community.general
    version: ">=9.0.0,<10.0.0"
  - name: ansible.posix
roles:
  - name: geerlingguy.nginx
    version: "3.2.0"
```

## Check module docs locally

`ansible-doc ansible.builtin.copy` shows documentation for the exact module version you have installed, including examples, with no internet needed.

**Quiz:** Why pin collection versions in requirements.yml?

- [ ] Collections cannot be installed otherwise
- [ ] Pinning makes modules faster
- [x] So every run uses the same, reproducible content
- [ ] It hides the code

*Answer:* So every run uses the same, reproducible content. Unpinned updates can change behaviour unexpectedly between runs.
