TExcelTAD
Home › Configuration Management: Ansible › Ansible Galaxy & requirements.yml

Ansible Galaxy & requirements.yml

Ansible Galaxy (galaxy.ansible.com) is the public hub for collections and roles. The ansible-galaxy CLI installs from it, from private servers (Red Hat Automation Hub, a self-hosted Galaxy NG or Pulp), from Git repositories and from tarballs.

Pin your dependencies in requirements.yml

Never install roles and collections by hand on a laptop: list them, with versions, in a requirements.yml in your repository, so CI and every colleague get the same code.

yaml
# requirements.yml
collections:
  - name: community.general
    version: ">=13.0.0,<14.0.0"
  - name: ansible.posix
    version: 2.2.2
  - name: https://github.com/acme/platform-collection.git   # straight from Git
    type: git
    version: v1.4.0                                         # a tag, branch or commit
  - name: acme.internal
    source: https://hub.example.com/api/galaxy/             # a private server

roles:
  - name: acme.nginx                  # a standalone role published on Galaxy as namespace.role
    version: 3.1.0
  - name: acme_hardening
    src: git+https://github.com/acme/ansible-role-hardening.git
    version: 2.0.3

Install them (a sample session; it needs the network):

bash
$ ansible-galaxy install -r requirements.yml -p ./roles          # roles
$ ansible-galaxy collection install -r requirements.yml -p ./collections
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/ansible-posix-2.2.2.tar.gz to /home/ada/.ansible/tmp/ansible-local-1234/tmp/ansible-posix-2.2.2-abcd
Installing 'ansible.posix:2.2.2' to '/home/ada/project/collections/ansible_collections/ansible/posix'
ansible.posix:2.2.2 was installed successfully

Without -p, collections go to the first folder in collections_path (~/.ansible/collections). Point collections_path = ./collections in the project's ansible.cfg so the project's pinned copies are found first.

When the requirements are already satisfied, nothing is downloaded, so this part works in the offline sandbox:

bash · try it yourself
cat > requirements.yml <<'YML'
collections:
  - name: community.general
    version: ">=13.0.0"
  - name: ansible.posix
YML
ansible-galaxy collection install -r requirements.yml
echo "=== the installed version"
ansible-galaxy collection list community.general 2>/dev/null | tail -1
Starting galaxy collection install process
Nothing to do. All requested collections are already installed. If you want to reinstall them, consider using `--force`.
=== the installed version
community.general 13.5.0

Add --upgrade to move to the newest version that matches the constraint, and --force to reinstall.

Choosing what to depend on

Galaxy content ranges from Red Hat-certified collections to someone's weekend role. Before you depend on something:

Check Why
maintained by a vendor, the Ansible community (community.*) or a known author abandoned roles break on the next Ubuntu release
recent releases, open issues answered same
tests (Molecule, ansible-test) in CI a role without tests is a role you test
a pinned version, never a branch main changes under you
read the tasks it runs as root on all your servers

For something small (a dozen tasks), writing your own role is often cheaper than understanding someone else's general-purpose one.

Vendoring

Some teams commit the installed collections and roles into the repository (collections/, roles/), or build an execution environment image with them (see the Toolkit). Runs then never depend on Galaxy being up, which matters for incident fixes at 3 a.m.

Integrity

ansible-galaxy collection verify compares an installed collection with the files listed in its MANIFEST.json. With --offline it checks the local files only:

bash · try it yourself
ansible-galaxy collection verify ansible.posix --offline 2>/dev/null | grep -v hash | head -4
echo "files reported: $(ansible-galaxy collection verify ansible.posix --offline 2>/dev/null | grep -c '^    ')"
Verifying 'ansible.posix:2.2.2'.
Installed collection found at '/opt/uv-tools/ansible-core/lib/python3.13/site-packages/ansible_collections/ansible/posix'
Collection ansible.posix contains modified content in the following files:
    .azure-pipelines/scripts/aggregate-coverage.sh
files reported: 166

The "modified content" here is not tampering: the ansible package on PyPI ships each collection without its tests and CI files, to save space, and verify notices that files listed in the collection's FILES.json are not on disk. On collections installed with ansible-galaxy collection install, a clean result prints only the Verifying/Installed collection found lines, and any listed file is a real local modification. Online, it also compares against the server's copy, and Galaxy servers can serve signed collections (--required-valid-signature-count, GPG keyrings) for supply-chain checks.

Test yourself

Why list dependencies in requirements.yml with versions?

  1. Galaxy requires it
  2. So CI and every colleague install exactly the same roles and collections
  3. It makes runs faster
  4. It encrypts them

How do you install a collection straight from a Git tag?

  1. Impossible
  2. name: <git URL>, type: git, version: <tag> in requirements.yml
  3. git clone into /etc/ansible
  4. pip install

What does collections_path = ./collections in the project's ansible.cfg achieve?

  1. Disables Galaxy
  2. The project's pinned collections are installed to and found in the project first
  3. Uploads collections
  4. Nothing