Project 4: Reusable Role and Galaxy Packaging
Refactor monolithic playbooks into a role with a clear contract and predictable behavior.
Quick Reference
| Attribute | Value |
|---|---|
| Difficulty | Level 2 |
| Time Estimate | 12-18 hours |
| Main Programming Language | YAML (roles) |
| Coolness | Level 3 |
| Business Potential | 3. Service & Support |
| Prerequisites | P02, P03 |
| Key Topics | Role architecture, variable contract, packaging |
1. Learning Objectives
- Build a role from existing web tier tasks.
- Define role input/output contract clearly.
- Preserve behavior while improving maintainability.
- Prepare role for shared reuse and versioning.
2. All Theory Needed (Per-Concept Breakdown)
2.1 Reuse Through Contracts
Fundamentals Reusable roles succeed when interfaces are explicit: what variables they accept, which defaults exist, and what state they guarantee.
Deep Dive into the concept Without role contracts, reuse becomes guesswork. Callers override internal values, side effects become unpredictable, and upgrades become breaking changes. Contract-first role design keeps platform ownership clear and helps teams evolve safely.
Mental model diagram
caller vars -> role defaults/inputs -> tasks/templates/handlers -> guaranteed host state
Where you’ll apply it P04 directly; reused in P07 and capstone.
3. Project Specification
3.1 What You Will Build
A web_base role containing:
- package/service tasks
- template deployment
- reload handler
- documented defaults and required vars
3.2 Functional Requirements
- Role runs from a minimal
site.ymlcaller. - Defaults produce working baseline with no override.
- Explicit overrides are supported for selected inputs.
- Role behavior matches pre-refactor outputs.
3.4 Example Output
$ ansible-playbook -i inventory.ini site.yml
PLAY RECAP
node-a : ok=6 changed=1 failed=0
node-b : ok=6 changed=1 failed=0
3.7 Real World Outcome
- Role directory follows standard structure.
- One-page role contract exists.
site.ymlstays short and readable.
4. Solution Architecture
site.yml -> role(web_base)
-> tasks/main.yml
-> templates/
-> handlers/
-> defaults/
5. Implementation Guide
5.3 The Core Question You’re Answering
“How do I package working automation so other teams can use it safely?”
5.4 Concepts You Must Understand First
- Role directory conventions.
- Variable precedence and default ownership.
- Backward-compatible refactoring.
5.5 Questions to Guide Your Design
- Which variables are public API and which are internal?
- How do you prevent accidental breaking changes?
5.6 Thinking Exercise
Write a change log entry for a hypothetical role update and decide if it is major/minor/patch.
5.7 Interview Questions
- What makes role APIs stable?
- Where should defaults live and why?
- How do you test role compatibility across callers?
5.8 Hints in Layers
- Hint 1: Refactor structure before behavior.
- Hint 2: Namespace role variables.
- Hint 3: Add assertions for required inputs.
- Hint 4: Compare old/new run recaps.
6. Testing Strategy
- Role run with defaults.
- Role run with selected overrides.
- Two-pass idempotency check.
7. Common Pitfalls & Debugging
| Pitfall | Symptom | Solution |
|---|---|---|
| generic var names | conflicts across roles | prefix role vars |
| hidden assumptions | works only in one repo | follow standard role paths |
| undocumented API | misuse by callers | publish contract table |
8. Extensions & Challenges
- Publish role metadata and semantic versioning policy.
- Add Molecule scenario for role validation.
- Add cross-distro test matrix.
9. Real-World Connections
Shared roles are the basis for platform teams that support many app teams without copy/paste automation.
10. Resources
- Ansible roles docs
- Ansible Galaxy docs
- Clean Architecture (contract design mindset)
11. Self-Assessment Checklist
- I can explain role inputs/outputs without reading tasks.
- I can run the role from a minimal caller playbook.
- I can show no regression from pre-refactor behavior.
12. Submission / Completion Criteria
- Minimum: role structure + successful run.
- Full: contract documentation + override tests.
- Excellence: semantic versioning + compatibility test matrix.