roboshop_v1 is my first version of deploying roboshop with Ansible. Every component has its own playbook, and I run the playbooks one by one. It doesn't use roles yet; that comes in roboshop_ansible_v2.
The project is built in two steps:
- v1: make it work. The aim is to get working deployment scripts for every component, so the whole shop runs end to end.
- v2: make it best. Once everything works, the same deployment is rewritten using Ansible best practices: roles, reusable code, cleaner variables and secured credentials.
The Ansible features used here (variables, conditions, loops, inventory, ansible.cfg) are explained on the Ansible Concepts page.
Source code: github.com/MadireddyManojKumar/ansible_roboshop/roboshop_v1
Architecture
roboshop is a small online shop split into separate components. Each component runs on its own EC2
instance, and every instance has a DNS name like catalogue.manojkumar.blog, so the
components find each other by name instead of by IP address.
- frontend runs nginx. It serves the website and forwards
/api/<service>calls to the right service. - catalogue lists products from mongodb. user handles logins with mongodb and redis.
- cart keeps carts in redis and asks catalogue for product details.
- shipping reads cities from mysql and talks to cart. payment talks to cart and user, then puts the order on rabbitmq.
- dispatch picks orders up from rabbitmq.
What's in the repository
Every file has its own section in the sidebar, cleaned up for reading, with a comment on each task. Passwords and download URLs are masked.
roboshop_ec2_instances.yaml: creates the EC2 instances and their Route 53 DNS records, or deletes them withaction=delete.inventory.ini: one group per component, using the DNS names, plus the SSH user.ansible.cfg: Ansible settings for this folder, such as YAML-formatted output.ms-roboshop-<component>.yaml: one playbook per component, 11 in total, for examplems-roboshop-mongodb.yaml.<service>.service.j2: systemd service templates. Ansible fills in the host names, for example{{ mongo_host }}incatalogue.service.j2.roboshop.conf.j2: the nginx config template for frontend.mongo.repo,rabbitmq.repo: package repository files copied to the servers.ec2_instance_creation_output.yaml: a saved copy of the EC2 creation output, which I used to find where the IP address sits in the result.
How a deployment runs
- Create the servers. Run the EC2 playbook once. It creates an instance per component and points
<component>.manojkumar.blogto it. - Databases first: mongodb, redis, mysql, rabbitmq. The services connect to them when they start, and catalogue loads its products into mongodb.
- Then the services: catalogue, user, cart, shipping, payment, dispatch.
- frontend last, once everything it forwards to is running.
ansible-playbook -i inventory.ini roboshop_ec2_instances.yaml
ansible-playbook -i inventory.ini ms-roboshop-mongodb.yaml
ansible-playbook -i inventory.ini ms-roboshop-redis.yaml
ansible-playbook -i inventory.ini ms-roboshop-mysql.yaml
ansible-playbook -i inventory.ini ms-roboshop-rabbitmq.yaml
ansible-playbook -i inventory.ini ms-roboshop-catalogue.yaml
ansible-playbook -i inventory.ini ms-roboshop-user.yaml
ansible-playbook -i inventory.ini ms-roboshop-cart.yaml
ansible-playbook -i inventory.ini ms-roboshop-shipping.yaml
ansible-playbook -i inventory.ini ms-roboshop-payment.yaml
ansible-playbook -i inventory.ini ms-roboshop-dispatch.yaml
ansible-playbook -i inventory.ini ms-roboshop-frontend.yaml
To remove everything, run the EC2 playbook again with action set to delete.
A value passed with -e overrides the one in the playbook:
ansible-playbook -i inventory.ini roboshop_ec2_instances.yaml -e action=delete
Inside one playbook: catalogue
Each service playbook follows the same pattern. For catalogue, the steps are: install Node.js 24, create
the roboshop system user, download the code into /app, install its dependencies, copy the
systemd service file, load the products into mongodb and start the service.
The service file is a template. The mongo_host variable from the playbook is filled in when
Ansible copies it:
- name: copy systemd service file
template:
src: catalogue.service.j2
dest: /etc/systemd/system/catalogue.service
Environment=MONGO_URL="mongodb://{{ mongo_host }}:27017/catalogue"
Loading products should happen only once. The playbook counts the products already in mongodb and
loads the data only when the count is 0, so running it again doesn't add duplicates:
- name: check products loaded in mongodb
command: mongosh --host "{{ mongo_host }}" --quiet --eval 'db.getSiblingDB("catalogue").products.countDocuments()'
register: products_count
- name: print product_count
debug:
msg: "{{ products_count }}"
- name: load schema for catalogue microservice
shell: "mongosh --host {{ mongo_host }} < /app/db/master-data.js"
when: products_count.stdout|int == 0
From working (v1) to best (v2)
v1 met its aim: every component deploys and the shop works. Getting there also showed what makes the scripts hard to maintain, and that is what v2 improves:
- Repeated code. The service playbooks repeat the same steps: create the roboshop user, prepare
/app, download the code and set up systemd. - Host names in every playbook. Names like
mongodb.manojkumar.blogare written in each playbook'svars, so a domain change means editing many files. - Many commands in the right order. Deploying means running 11 playbooks by hand, in order.
- Plain-text passwords. Passwords sit in the inventory, playbooks and templates, where anyone who can read the repository can see them.
roboshop_ansible_v2 fixes this in two ways:
- Roles. The shared steps are written once, and a single
main.yamldeploys any component by its name. - Secured credentials, in two ways. Secrets are encrypted with Ansible Vault, so the repository only holds the encrypted text. Secrets are also kept in an AWS secret store, so they live in AWS, not in the code, and the playbook fetches them only when it runs.