Roboshop is a cloud-native, microservices-based e-commerce application. This project demonstrates how to automate the deployment and configuration of all Roboshop components using Ansible roles, following best practices for modularity, reusability, and maintainability.
- π§© For a beginner-friendly explanation of how each Roboshop component connects and why, see
AboutProject.txt
. - π οΈ For step-by-step Ansible role implementation, variable flow, and troubleshooting, see
douments.txt
.
These files provide:
- ποΈ Clear explanations of each microservice and its database connections
- πΌοΈ Visual diagrams and request/response flows
- π§βπ» Common error fixes and best practices
- ποΈ Detailed Ansible role structure and variable usage
For a comprehensive, step-by-step breakdown of how every part of this project is implemented, see douments.txt
. This document covers:
- π Beginner-friendly Ansible role creation and directory structure
- π οΈ Implementation steps for each component (MongoDB, Catalogue, Redis, User, Cart, Frontend, MySQL, Shipping, Payment)
- π Variable flow, templates, and handlers explained visually
- π Common mistakes, troubleshooting, and real error messages
- π§© Modular code structure and DRY best practices
- π Visual charts, request/response flows, and emoji-enhanced instructions
If you want to understand exactly how to build, debug, and extend this project, start with douments.txt
!
- Microservices Architecture: Each business function (cart, user, catalogue, shipping, payment, etc.) is a separate service, deployed and managed independently.
- Ansible Roles: All automation is organized using Ansible roles, making the code modular, DRY (Don't Repeat Yourself), and easy to extend.
- Database Diversity: Uses MongoDB (NoSQL), MySQL (relational), and Redis (in-memory) to match each service's needs.
- Best Practices: Variables, templates, handlers, and common roles are used for safe, efficient, and scalable automation.
- Ansible: βοΈ Automation engine for configuration management and deployment.
- Roles: π¦ Each service (cart, user, catalogue, shipping, payment, frontend, redis, mongodb, mysql) has its own role under
roles/
. - Common Role: π Shared tasks (app setup, systemd, maven build, etc.) are placed in
roles/common/
and included as needed. - Templates: π Jinja2 templates for systemd service files and configs, with variables injected at deploy time.
- Handlers: π Used to safely restart services only when configuration changes.
- Databases:
- MongoDB: π Used by catalogue and user services for flexible, document-based storage.
- MySQL: π¬ Used by shipping and payment services for structured, transactional data.
- Redis: π§ Used by cart service for fast, temporary session storage.
- RabbitMQ: π (Optional) For asynchronous messaging between services.
- Maven: β Used to build Java-based services (e.g., shipping).
flowchart TD
A["inventory.ini ποΈ"] --> B["main.yaml βΆοΈ"]
B --> C{"component variable π§©"}
C --> D["roles/<component>/tasks/main.yaml π"]
D --> E["include_role: common/tasks/appsetup.yaml π οΈ"]
D --> F["import_role: common/tasks/deployment.yaml π·οΈ"]
D --> G["template: roles/<component>/templates/*.j2 π§©"]
D --> H["vars: roles/<component>/vars/main.yaml ποΈ"]
D --> I["handlers: roles/<component>/handlers/main.yaml π"]
%% Variables influence Templates and Handlers
H --> G
H --> I
G --> I
%% Handlers restart services when notified
I --> K["Service Restarted if Needed π¦"]
E --> J["handlers: roles/common/handlers/main.yaml π"]
F --> J
%% Tags apply on role inclusion to support selective runs
F -.-> F_TAG["Tags: deployment, setup, etc. π·οΈ"]
E -.-> E_TAG["Tags: deployment, setup, etc. π·οΈ"]
D -.-> D_TAG["Tags: deployment, setup, etc. π·οΈ"]
%% Secrets accessed by vars/tasks
H --> L["SSM Parameter Store π"]
H --> M["Ansible Vault (legacy) ποΈ"]
β¨componentπ§°
- mongodb , mysql , rabbitmq ,redis , user , cart, catalogue , shipping , payment and frontend .
Explanation of the Flow:
- Inventory (inventory.ini): Defines hosts and groups.
- Main Playbook (main.yaml): Runs with the component name as a variable.
- Component Role Tasks: Selected role executes component-specific tasks.
- Common Roles: Included or imported within the component tasks:
- appsetup.yaml (included dynamically for setup helpers)
- deployment.yaml (imported statically for deployment tasks, also tagged)
- Templates: Jinja2 templates rendered with variables from vars/main.yaml.
- Vars: Contains variables, key/secret lookups from AWS SSM or Vault.
- Handlers: Handle service restarts, triggered when templates or tasks notify of changes.
- Service Restarts: Are only done when necessary.
- Tags: Applied on include_role and import_role for selective execution depending on playbook tags.
This structure ensures:
- π§© Every component is modular and easy to find.
- π Shared logic is reused via the
common
role. - ποΈ Variables, templates, and handlers are organized for clarity and maintainability.
- β¨ You can add new components by copying the folder structure and updating inventory/vars/templates as needed.
- Inventory: ποΈ Define all your hosts and groups in
inventory.ini
. - Roles: π¦ Each service has its own role with tasks, templates, vars, and handlers.
- Common Role: π Shared logic (like app setup, maven build, systemd) is reused via
include_role
. - Variables: π Each role's
vars/main.yaml
defines hostnames, credentials, and config values. - Templates: π¨οΈ Jinja2 templates use variables to generate correct configs for each service.
- Handlers: π Services are restarted only when configs change, preventing unnecessary downtime.
- Playbook:
βΆοΈ The genericmain.yaml
playbook can deploy any component by passing thecomponent
variable.
- Frontend: π₯οΈ User interface, talks to backend services via API.
- Cart Service: π Manages cart, connects to Redis (for sessions) and Catalogue (for product info).
- Catalogue Service: π Stores product info in MongoDB.
- User Service: π€ Manages users, connects to MongoDB.
- Shipping & Payment: ππ³ Both use MySQL for structured data.
- Redis: π§ Fast, in-memory storage for cart sessions.
- MongoDB: π Flexible, document-based storage for products and users.
- MySQL: π¬ Relational storage for orders, shipping, and payments.
- RabbitMQ: π (Optional) For asynchronous messaging between services.
For detailed flows and real-world request examples, see AboutProject.txt
.
- To deploy the catalogue service:
ansible-playbook -i inventory.ini -e "component=catalogue" main.yaml
- To deploy the shipping service:
ansible-playbook -i inventory.ini -e "component=shipping" main.yaml
- Replace
catalogue
orshipping
with any other component name as needed.
- Clone the Repository:
git clone <your-repo-url> cd <repo-directory>
- Install Ansible:
# On RHEL/CentOS sudo dnf install ansible -y # Or on Ubuntu sudo apt-get install ansible -y
- Install Required Collections:
ansible-galaxy collection install community.mysql
- Configure Inventory:
- Edit
inventory.ini
to add your target hosts.
- Edit
- Set Variables:
- Edit each role's
vars/main.yaml
to set hostnames, credentials, etc.
- Edit each role's
- Run the Playbook:
ansible-playbook -i inventory.ini -e "component=<component-name>" main.yaml # Example: ansible-playbook -i inventory.ini -e "component=frontend" main.yaml
[inventory.ini] β [main.yaml] β [roles/<component>/tasks/main.yaml]
β β β
[roles/common/tasks/*] [roles/<component>/templates/*] [roles/<component>/vars/main.yaml]
β β β
[handlers] (restart services if needed)
- Modular: π§© Each service is independent and easy to update or scale.
- Reusable: π Common logic is shared, reducing duplication.
- Maintainable: π οΈ Clear structure makes troubleshooting and extending easy.
- Production-Ready: π Follows DevOps and microservices best practices.
π My Ongoing Ansible Learning Journey
As I advanced in my DevOps path, I unlocked several powerful Ansible concepts that made my automation smarter and more reliable:
-
π·οΈ Tags: I discovered how tags let me run only the tasks I need, making deployments faster and troubleshooting more focused. This saves time and reduces risk during updatesβno more running everything when you only want to update code!
-
π include_role vs import_role: Understanding the difference was a game-changer.
include_role
is dynamic and flexible for conditions and loops, whileimport_role
is static and applies tags and conditions to all tasks at parse time. Choosing the right one helps me build playbooks that are both flexible and robust. -
π‘οΈ Error Handling: I improved reliability by using
ignore_errors
and smart conditional logic withregister
andwhen
. Now, my playbooks are more robust, idempotent, and ready for real-world surprises. -
π Secrets Management: Managing secrets was a real challenge! I started with Ansible Vault, but as the project grew, I migrated to AWS SSM Parameter Store for easier, more secure automation. I also learned the importance of installing Python dependencies like
boto3
on the control node, not the target serversβthis fixed some tricky errors. -
π¦ File Operations: I clarified the difference between copying files from my local machine versus working with files already on the remote server. Using
remote_src
andget_url
helped me streamline deployments and avoid common pitfalls.
β¨ These lessons, combined with my earlier mistake journey, have made my automation more modular, secure, and production-ready. Every new project benefits from these practical insightsβand I hope sharing them helps you too!
π Real Mistakes & How I Solved Them:
-
β YAML Structure Errors: I put playbook-level keys (like
hosts:
andvars:
) inside role task files, which made Ansible fail.- π οΈ Fix: I moved those keys to the playbook and kept only tasks in the role files.
-
β Variable Scoping Issues: Some services couldn't connect because variables (like
CATALOGUE_HOST
,REDIS_HOST
) were missing or in the wrong place.- ποΈ Fix: I defined all service-specific variables in each role's
vars/main.yaml
and used them in templates.
- ποΈ Fix: I defined all service-specific variables in each role's
-
β Service Connectivity Problems: Services failed to start due to missing or wrong hostnames in systemd files.
- π Fix: I used Jinja2 templates and Ansible variables to inject the correct hostnames, and tested each service separately.
-
β Handler Misuse: I restarted services too often, causing downtime.
- π Fix: I set up handlers to restart only when configs changed, improving uptime.
-
β 404s & Missing Files: I got errors because files were missing or paths were wrong during deployment.
- π Fix: I double-checked file paths, used
ansible.builtin.copy
andtemplate
, and ranansible-playbook --check
before real runs.
- π Fix: I double-checked file paths, used
-
β Documentation Gaps: My early docs were too technical and hard to follow.
- π Fix: I added step-by-step guides, emojis, diagrams, and real-world flows to make everything beginner-friendly and interview-ready.
-
β Tags Not Working: I tried to run only deployment tasks with tags, but nothing happened because I tagged only the include_role line, not the tasks themselves.
- π·οΈ Fix: I learned to use import_role when I want tags to apply to all tasks, or to tag each task directly if using include_role.
-
β import_role vs include_role Confusion: I used include_role when I needed tags and conditions to apply to all tasks, but it didn't work as expected.
- π Fix: I switched to import_role for static inclusion and full tag/when coverage, and used include_role only for dynamic, conditional, or looped tasks.
-
β Vault Password Hassles: I used Ansible Vault for secrets, but had to type the password every time and it was hard to automate.
- π Fix: I moved secrets to AWS SSM Parameter Store for easier, more secure, and automated password management.
-
β Error Handling Oversights: I forgot to use ignore_errors or didn't check task results, so my playbook stopped on predictable errors.
- π‘οΈ Fix: I used ignore_errors and registered outputs, then added when conditions to handle errors gracefully and keep the playbook running.
β¨ Every mistake was a learning opportunity! Now my project is easier to use, more reliable, and much more fun to share.
Below is a detailed, emoji-rich directory structure and flowchart for everything under the rolesAnsibleRoboshop
folder. Every major file and folder is shown with an emoji, so you can instantly see where to find and place each part of your project!
rolesAnsibleRoboshop/ ποΈ
βββ inventory.ini ποΈ # Inventory of all hosts/groups
βββ main.yaml βΆοΈ # Generic playbook to deploy any component
βββ AboutProject.txt π # Beginner-friendly architecture and flows
βββ douments.txt π # In-depth implementation guide
βββ README.md π # This documentation file
βββ roles/ π¦ # All Ansible roles live here
β βββ cart/ π
β β βββ tasks/ π
β β β βββ main.yaml π
β β βββ templates/ π
β β β βββ cart.service.j2 π§©
β β βββ vars/ ποΈ
β β β βββ main.yaml π
β β βββ handlers/ π
β β βββ main.yaml (optional) π
β βββ user/ π€
β β βββ tasks/ π
β β β βββ main.yaml π
β β βββ templates/ π
β β β βββ user.service.j2 π§©
β β βββ vars/ ποΈ
β β β βββ main.yaml π
β β βββ handlers/ π
β β βββ main.yaml (optional) π
β βββ catalogue/ π
β β βββ tasks/ π
β β β βββ main.yaml π
β β βββ templates/ π
β β β βββ catalogue.service.j2 π§©
β β βββ vars/ ποΈ
β β β βββ main.yaml π
β β βββ handlers/ π
β β βββ main.yaml (optional) π
β βββ shipping/ π
β β βββ tasks/ π
β β β βββ main.yaml π
β β βββ templates/ π
β β β βββ shipping.service.j2 π§©
β β βββ vars/ ποΈ
β β β βββ main.yaml π
β β βββ handlers/ π
β β βββ main.yaml (optional) π
β βββ payment/ π³
β β βββ tasks/ π
β β β βββ main.yaml π
β β βββ templates/ π
β β β βββ payment.service.j2 π§©
β β βββ vars/ ποΈ
β β β βββ main.yaml π
β β βββ handlers/ π
β β βββ main.yaml (optional) π
β βββ frontend/ π₯οΈ
β β βββ tasks/ π
β β β βββ main.yaml π
β β βββ templates/ π
β β β βββ nginx.conf.j2 π§©
β β βββ vars/ ποΈ
β β β βββ main.yaml π
β β βββ handlers/ π
β β βββ main.yaml π
β βββ redis/ π§
β β βββ tasks/ π
β β β βββ main.yaml π
β β βββ handlers/ π
β β βββ main.yaml (optional) π
β βββ mongodb/ π
β β βββ tasks/ π
β β β βββ main.yaml π
β β βββ files/ π
β β β βββ mongo.repo π
β β βββ handlers/ π
β β βββ main.yaml (optional) π
β βββ mysql/ π¬
β β βββ tasks/ π
β β β βββ main.yaml π
β β βββ handlers/ π
β β βββ main.yaml (optional) π
β βββ common/ π # Shared logic for all roles
β βββ tasks/ π
β β βββ appsetup.yaml π οΈ
β β βββ maven.yaml β
β β βββ python.yaml π
β β βββ systemd.yaml βοΈ
β β βββ ...
β βββ handlers/ π
β βββ main.yaml (optional) π
βββ ...
Explore my continuous learning adventure across these repositories, each showcasing different facets of my Ansible mastery:
- π learnAnsible β Hands-on tutorials and practical playbooks to build your Ansible foundation.
- π οΈ ansibleRoboshop β Real-world microservices automation project applying modular Ansible practices.
- π¦ rolesAnsibleRoboshop β Deep dive into reusable Ansible roles and advanced troubleshooting techniques.
Feel free to dive in, contribute, or ask questions anytime! Letβs collaborate and level-up automation skills together! π‘π€β¨
- Inspired by the innovative Roboshop microservices architecture and design pattern.
- Automation and documentation craft by Mahalakshmi π»