ADR-0002: Ansible Role-Based Modular Architectureยถ
Statusยถ
Accepted
Contextยถ
The project manages complex KVM host setup, edge host validation, and LVM configuration tasks. These operations involve multiple interconnected components including virtualization, networking, storage, and system configuration that need to be organized in a maintainable and reusable way.
Without proper organization, these tasks would become a monolithic, hard-to-maintain codebase that would be difficult to test, debug, and extend.
Decisionยถ
Adopt a modular Ansible role-based architecture to organize all automation tasks into discrete, reusable roles. Each role encapsulates related functionality with standardized directory structure including tasks, handlers, variables, defaults, templates, and documentation.
The project implements nine specialized roles:
kvmhost_setup: Main orchestration role coordinating all KVM host setup
kvmhost_base: Foundation system configuration and package management
kvmhost_networking: Network bridge configuration and validation
kvmhost_libvirt: Libvirt daemon and virtualization setup
kvmhost_storage: Storage pool management and optimization
kvmhost_cockpit: Web interface installation and configuration
kvmhost_user_config: User environment and shell configuration
edge_hosts_validate: System validation and compliance checking
swygue_lvm: Advanced LVM management and configuration
Alternatives Consideredยถ
Monolithic playbook structure - All tasks in single large playbooks without role separation
Single large playbook with all tasks inline - Would be difficult to maintain and test
Separate standalone scripts for each configuration task - Would lose Ansible benefits and create inconsistent execution patterns
External tooling integration without Ansible abstraction - Would require managing multiple tool ecosystems
Consequencesยถ
Positiveยถ
Improved code organization through encapsulated, reusable components
Enhanced maintainability with clear separation of concerns
Better reusability - roles can be used across multiple playbooks and projects
Simplified testing - each role can be tested independently using Molecule
Parallel development - multiple developers can work on different roles simultaneously
Selective execution - users can run only the roles they need
Clear dependencies - explicit role dependencies make relationships obvious
Negativeยถ
Increased complexity in role coordination and dependency management
Additional overhead in maintaining role interfaces and contracts
Learning curve for developers unfamiliar with modular Ansible patterns
Potential over-engineering for simple use cases
Implementationยถ
Role Structure Standardยถ
Each role follows the standard Ansible role directory structure:
roles/<role_name>/
โโโ defaults/main.yml # Default variables
โโโ handlers/main.yml # Event handlers
โโโ meta/main.yml # Role metadata and dependencies
โโโ tasks/main.yml # Main task entry point
โโโ tasks/ # Additional task files
โโโ templates/ # Jinja2 templates
โโโ vars/main.yml # Role-specific variables
โโโ files/ # Static files
โโโ README.md # Role documentation
Dependency Managementยถ
Roles declare explicit dependencies in meta/main.yml
:
dependencies:
- role: kvmhost_base
when: kvmhost_base_required | default(true)
Interface Contractsยถ
Each role defines clear input/output interfaces:
Input: Required and optional variables
Output: Facts set and services configured
Side Effects: System changes made
Testing Strategyยถ
Each role includes comprehensive Molecule testing:
Unit tests: Individual role functionality
Integration tests: Role interaction validation
System tests: End-to-end functionality verification
Evidenceยถ
Implementation Artifactsยถ
Role Directory Structure: Standardized across all roles
Dependency Declarations: Explicit in meta/main.yml files
Testing Framework: Molecule scenarios for each role
Documentation: README.md for each role with interface documentation
Success Metricsยถ
Code Reusability: Roles used across multiple playbooks
Testing Coverage: Each role has comprehensive test coverage
Maintenance Efficiency: Isolated changes donโt affect other roles
Developer Productivity: Parallel development on different roles
Referencesยถ
This ADR established the foundational architecture for the collection. For understanding how this architecture is implemented, see Modular Role Design.