Skip to content

A modular, role-based Ansible setup to automate the deployment of Roboshop microservices. πŸ“¦ Ready-made roles for every service (cart, catalogue, user, payment, shipping, frontend) πŸ—„οΈ Database & queue roles (MongoDB, MySQL, Redis, RabbitMQ) πŸ”„ Common tasks for fast, repeatable deployments πŸ” Reusable roles⚑Fast automationπŸ› οΈ Easy custom

Notifications You must be signed in to change notification settings

MAHALAKSHMImahalakshmi/rolesAnsibleRoboshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

70 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Roboshop Microservices Deployment with Ansible Roles πŸš€

Overview

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.


πŸ“– Learn More About the Project

  • 🧩 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

πŸ“š In-Depth Implementation Guide: douments.txt

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!


Project Highlights ✨

  • 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.

Components & Technologies 🧰

  • 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).

πŸ› οΈ Updated Ansible Playbook Flow with Tags, Roles, and Secrets

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) πŸ—οΈ"]

Loading

✨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.

How It Works ⚑

  1. Inventory: πŸ—’οΈ Define all your hosts and groups in inventory.ini.
  2. Roles: πŸ“¦ Each service has its own role with tasks, templates, vars, and handlers.
  3. Common Role: πŸ” Shared logic (like app setup, maven build, systemd) is reused via include_role.
  4. Variables: πŸ“ Each role's vars/main.yaml defines hostnames, credentials, and config values.
  5. Templates: πŸ–¨οΈ Jinja2 templates use variables to generate correct configs for each service.
  6. Handlers: πŸ”„ Services are restarted only when configs change, preventing unnecessary downtime.
  7. Playbook: ▢️ The generic main.yaml playbook can deploy any component by passing the component variable.

How the Architecture Works (Beginner-Friendly) πŸ—οΈ

  • 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.


Example: Deploying a Component 🚦

  • 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 or shipping with any other component name as needed.

Step-by-Step: How to Run the Project πŸƒβ€β™‚οΈ

  1. Clone the Repository:
    git clone <your-repo-url>
    cd <repo-directory>
  2. Install Ansible:
    # On RHEL/CentOS
    sudo dnf install ansible -y
    # Or on Ubuntu
    sudo apt-get install ansible -y
  3. Install Required Collections:
    ansible-galaxy collection install community.mysql
  4. Configure Inventory:
    • Edit inventory.ini to add your target hosts.
  5. Set Variables:
    • Edit each role's vars/main.yaml to set hostnames, credentials, etc.
  6. 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

Visual Flow (How Everything Connects) πŸ”—

[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)

Why This Approach? πŸ€”

  • 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, while import_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 with register and when. 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 and get_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: and vars:) 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.
  • ❌ 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 and template, and ran ansible-playbook --check before real runs.
  • ❌ 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.

Full Visual Directory Structure & Flowchart πŸ—‚οΈβœ¨

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) πŸ“
└── ...

πŸ“– My Ansible Learning Journey πŸš€βœ¨

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! πŸ’‘πŸ€βœ¨


πŸ™ Credits & Contact πŸ’¬πŸ€—

  • Inspired by the innovative Roboshop microservices architecture and design pattern.
  • Automation and documentation craft by Mahalakshmi πŸ’»

About

A modular, role-based Ansible setup to automate the deployment of Roboshop microservices. πŸ“¦ Ready-made roles for every service (cart, catalogue, user, payment, shipping, frontend) πŸ—„οΈ Database & queue roles (MongoDB, MySQL, Redis, RabbitMQ) πŸ”„ Common tasks for fast, repeatable deployments πŸ” Reusable roles⚑Fast automationπŸ› οΈ Easy custom

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages