Sobes.tech
Junior — Middle

What is the difference between a role object and a collection in Ansible within infrastructure automation?

sobes.tech AI

Answer from AI

In Ansible, a "role" object is a structured set of tasks, files, templates, and variables designed for reuse and code organization. A role encapsulates the logic for configuring a specific component or service and can be easily included in a playbook.

A collection is a larger unit that can contain multiple roles, modules, plugins, and other resources. Collections serve to distribute and manage a set of related Ansible components, simplifying their installation and updating.

In other words:

  • A role is a reusable unit of code for configuration.
  • A collection is a package containing roles and other Ansible extensions.

Example: you can install a collection using ansible-galaxy collection install, and then use roles from it in your playbooks.