Ansible Roles vs Collections

Roles organize tasks within a project into reusable units (tasks/handlers/templates/vars/defaults/files). Collections are distributable packages containing roles + modules + plugins + playbooks + documentation. Role = small reusable component inside a project. Collection = complete automation package shared across projects and teams.

Overview

Two levels of reuse

Ansible provides two levels of reuse: Roles within a project and Collections across projects and teams. The hierarchy is simple: Collections contain Roles, which contain Tasks. A collection such as company.infra is versioned, and beside roles like webserver it can also carry plugins (modules and filters), playbooks and docs.

Key concepts

  • Role
  • Collection
  • ansible-galaxy
  • tasks/main.yml
  • defaults vs vars
  • FQCN (Fully Qualified Collection Name)

Interview questions

  • What is the difference between an Ansible Role and a Collection?
  • How do you structure an Ansible project for a multi-tier application?

Sign up free to read the full topic

More DevOps topics

Start free on Cariara · Coding problems · System design studio