roboshop_v1 Overview

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.

The browser calls the frontend (nginx), which forwards API calls to catalogue, user, cart, shipping and payment. catalogue and user use mongodb, user and cart use redis, shipping uses mysql, and payment and dispatch use rabbitmq. Web Browser customer frontend (nginx) frontend.manojkumar.blog /api/<service> Services catalogue nodejs user nodejs cart nodejs shipping java payment python dispatch golang Data mongodb products, users redis carts, sessions mysql shipping cities rabbitmq order queue Every box is one EC2 instance, reached by its name: <component>.manojkumar.blog
  • 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.


How a deployment runs

  1. Create the servers. Run the EC2 playbook once. It creates an instance per component and points <component>.manojkumar.blog to it.
  2. Databases first: mongodb, redis, mysql, rabbitmq. The services connect to them when they start, and catalogue loads its products into mongodb.
  3. Then the services: catalogue, user, cart, shipping, payment, dispatch.
  4. 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.blog are written in each playbook's vars, 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.yaml deploys 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.
← Back to portfolio

roboshop_ec2_instances.yaml

Creates the roboshop EC2 instances and a Route 53 DNS record for each one, like mongodb.manojkumar.blog. Run it with action=delete to remove everything. Runs on the control node itself (hosts: local).

Run it:

ansible-playbook -i inventory.ini roboshop_ec2_instances.yaml
ansible-playbook -i inventory.ini roboshop_ec2_instances.yaml -e action=delete
- name: roboshop-mongodb create ec2 instances
  connection: local
  hosts: local
  vars:
    ami_id: ami-0055977a6e828a4f4
    instance_type: t3.micro
    instances: ["mongodb", "redis", "mysql", "rabbitmq", "catalogue", "user", "cart", "shipping", "payment", "frontend"]
    domain_name: manojkumar.blog
    action: create
  tasks:
    # create one EC2 instance per component (when action is create)
    - name: create ec2 instances
      amazon.aws.ec2_instance:
        name: roboshop-{{item}}
        image_id: "{{ ami_id }}"
        instance_type: "{{instance_type}}"
        security_groups:
          - "roboshop-{{item}}"
          - "roboshop-common"
        region: us-east-1
      loop: "{{instances}}"
      when: action == "create"
      register: ec2_instance_creation_output

    # point <component>.manojkumar.blog to the new instance's IP
    - name: route 53 record creation
      amazon.aws.route53:
        state: present
        zone: "{{domain_name}}"
        record: "{{item.item}}.{{domain_name}}"
        type: A
        ttl: 1
        value: "{{item.instances[0].public_ip_address}}"
        overwrite: true
      loop: "{{ec2_instance_creation_output.results}}"
      when: action == "create"

    # delete the DNS records (when action is delete)
    - name: route 53 record deletion
      amazon.aws.route53:
        state: absent
        zone: "{{domain_name}}"
        record: "{{item}}.{{domain_name}}"
        type: A
        ttl: 1
      loop: "{{instances}}"
      when: action == "delete"

    # terminate the EC2 instances (when action is delete)
    - name: delete ec2 instances
      amazon.aws.ec2_instance:
        name: roboshop-{{item}}
        state: absent
        region: us-east-1
        wait: false
      loop: "{{instances}}"
      when: action == "delete"
← Back to portfolio

ec2_instance_creation_output.yaml

A saved copy of what the create task registers. I used it to find the path to the IP address: results → instances[0] → public_ip_address. This is a short excerpt of the real file (744 lines), with IDs and IPs masked.

results:
-   ansible_loop_var: item
    changed: false
    failed: false
    instance_ids:
    - i-*****
    instances:
    -   image_id: ami-0055977a6e828a4f4
        instance_id: i-*****
        instance_type: t3.micro
        platform_details: Red Hat Enterprise Linux
        private_ip_address: *****
        public_ip_address: *****
        ...
    item: mongodb
-   ansible_loop_var: item
    ...
    item: redis
...
← Back to portfolio

inventory.ini

The list of servers. Each component has its own group, and servers are listed by DNS name, so the file doesn't change when an instance gets a new IP.

# variables for the frontend group
[frontend:vars]
appname=roboshop
version=v1

# the control node itself, used by the EC2 playbook
[local]
localhost

# one group per component, each pointing to its DNS name
[mongodb]
mongodb.manojkumar.blog

[redis]
redis.manojkumar.blog

[mysql]
mysql.manojkumar.blog

[rabbitmq]
rabbitmq.manojkumar.blog

[catalogue]
catalogue.manojkumar.blog

[user]
user.manojkumar.blog

[cart]
cart.manojkumar.blog

[shipping]
shipping.manojkumar.blog

[payment]
payment.manojkumar.blog

[dispatch]
dispatch.manojkumar.blog

[frontend]
frontend.manojkumar.blog

# login details used for every server
[all:vars]
ansible_user=ec2-user
ansible_password=*****
mysql_root_password=*****
← Back to portfolio

ansible.cfg

My ansible.cfg is the full sample config, with almost every line commented out. These are the only settings that are switched on:

[defaults]
# print task results with the standard Ansible output
stdout_callback = ansible.builtin.default
# show results as YAML, which is easier to read than JSON
callback_result_format = yaml
← Back to portfolio

ms-roboshop-mongodb.yaml

Installs MongoDB and lets the other servers connect to it. catalogue and user store their data here.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-mongodb.yaml
- name: Roboshop MongoDB deployment script using ansible
  hosts: mongodb
  become: true
  tasks:
    # add the MongoDB repo so dnf can find the packages
    - name: setup mongodb repo file
      copy:
        src: mongo.repo
        dest: /etc/yum.repos.d/mongo.repo

    # install the MongoDB server
    - name: install mongodb
      ansible.builtin.package:
        name: mongodb-org
        state: present

    # start mongod and start it on every boot
    - name: start mongodb
      ansible.builtin.service:
        name: mongod
        state: started
        enabled: yes

    # check that the service is running
    - name: verify mongodb status
      ansible.builtin.command: systemctl is-active mongod
      register: mongodb_status

    # print the status (active / inactive)
    - name: display mongodb status
      debug:
        var: mongodb_status.stdout

    # listen on all addresses so other servers can connect
    - name: configure mongodb bind_ip
      replace:
        path: /etc/mongod.conf
        regexp: "127.0.0.1"
        replace: "0.0.0.0"

    # restart so the new bind_ip takes effect
    - name: restart mongodb
      ansible.builtin.service:
        name: mongod
        state: restarted

    # check the service again after the restart
    - name: verify mongodb status after restart
      ansible.builtin.command: systemctl is-active mongod
      register: mongodb_status_after_restart

    # print the status after the restart
    - name: display mongodb status after restart
      debug:
        var: mongodb_status_after_restart.stdout

    # check that MongoDB is listening on port 27017
    - name: verify mongodb listening port
      shell: netstat -tulnp | grep 27017
      register: mongodb_port_status

    # print the port check result
    - name: display mongodb listening port status
      debug:
        var: mongodb_port_status.stdout

    # done
    - name: success message
      debug:
        msg: "MongoDB deployment completed successfully."
← Back to portfolio

mongo.repo

Tells dnf where to download MongoDB 7.0. Copied to the mongodb server, and to catalogue for the mongosh client.

[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://*****
enabled=1
gpgcheck=0
← Back to portfolio

ms-roboshop-redis.yaml

Installs Redis 7 and lets user and cart connect to it.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-redis.yaml
- name: configure redis server
  hosts: redis
  become: yes
  tasks:
    # choose Redis 7 from the dnf modules
    - name: enable redis 7
      command: dnf module enable redis:7 -y

    # install Redis
    - name: install redis
      package:
        name: redis
        state: present

    # listen on all addresses so other servers can connect
    - name: allow remote connections
      replace:
        path: /etc/redis/redis.conf
        regexp: '127.0.0.1'
        replace: '0.0.0.0'

    # turn off protected mode, which blocks remote clients
    - name: disable protected mode
      lineinfile:
        path: /etc/redis/redis.conf
        regexp: '^#?protected-mode yes'
        line: 'protected-mode no'

    # restart Redis with the new config and start it on every boot
    - name: enable and start redis
      service:
        name: redis
        state: restarted
        enabled: yes
← Back to portfolio

ms-roboshop-mysql.yaml

Installs MySQL 8.4 and sets the root password. shipping loads its data here.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-mysql.yaml
- name: configure mysql server
  hosts: mysql
  become: yes
  vars:
    mysql_root_password: "*****"
  tasks:
    # choose MySQL 8.4 from the dnf modules
    - name: enable mysql 8.4
      command: dnf module enable mysql:8.4 -y

    # install the MySQL server
    - name: install mysql server
      package:
        name: mysql-server
        state: present

    # start mysqld and start it on every boot
    - name: enable and start mysql
      service:
        name: mysqld
        state: started
        enabled: yes

    # set the MySQL root password
    - name: setup mysql root password
      command: mysql_secure_installation --set-root-pass "{{ mysql_root_password }}"
← Back to portfolio

ms-roboshop-rabbitmq.yaml

Installs RabbitMQ and creates the roboshop user that payment and dispatch log in with.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-rabbitmq.yaml
- name: configure rabbitmq server
  hosts: rabbitmq
  become: yes
  tasks:
    # add the RabbitMQ and Erlang repos
    - name: copy rabbitmq repo
      copy:
        src: rabbitmq.repo
        dest: /etc/yum.repos.d/rabbitmq.repo

    # install Erlang (needed by RabbitMQ) and the RabbitMQ server
    - name: install rabbitmq server
      dnf:
        name:
          - erlang
          - rabbitmq-server
        state: present

    # start RabbitMQ and start it on every boot
    - name: start and enable rabbitmq server
      service:
        name: rabbitmq-server
        state: started
        enabled: yes

    # create the roboshop user that payment and dispatch log in with
    - name: create rabbitmq user
      community.rabbitmq.rabbitmq_user:
        user: roboshop
        password: "*****"
        vhost: /
        configure_priv: .*
        read_priv: .*
        write_priv: .*
        state: present
← Back to portfolio

rabbitmq.repo

Tells dnf where to download Erlang and RabbitMQ.

[modern-erlang]
name=modern-erlang-el9
baseurl=https://*****
        https://*****
repo_gpgcheck=1
enabled=1
gpgkey=https://*****
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md

[modern-erlang-noarch]
name=modern-erlang-el9-noarch
baseurl=https://*****
        https://*****
repo_gpgcheck=1
enabled=1
gpgkey=https://*****
       https://*****
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md

[rabbitmq-el9]
name=rabbitmq-el9
baseurl=https://*****
        https://*****
repo_gpgcheck=1
enabled=1
gpgkey=https://*****
       https://*****
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md

[rabbitmq-el9-noarch]
name=rabbitmq-el9-noarch
baseurl=https://*****
        https://*****
repo_gpgcheck=1
enabled=1
gpgkey=https://*****
       https://*****
gpgcheck=1
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
pkg_gpgcheck=1
autorefresh=1
type=rpm-md
← Back to portfolio

ms-roboshop-catalogue.yaml

Installs Node.js, deploys the catalogue app, loads the products into MongoDB (only once) and starts the service.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-catalogue.yaml
- name: configure catalogue microservice
  hosts: catalogue
  become: yes
  vars:
    catalogue_app_user: roboshop
    mongo_host: mongodb.manojkumar.blog
  tasks:
    # turn off the default Node.js version
    - name: disable default nodejs
      command: dnf module disable nodejs -y

    # choose Node.js 24
    - name: enable nodejs version 24
      command: dnf module enable nodejs:24 -y

    # install Node.js
    - name: install nodejs
      package:
        name: nodejs
        state: present

    # read the installed Node.js version
    - name: verify nodejs version
      command: node -v
      register: nodejs_version

    # print the Node.js version
    - name: print nodejs version
      debug:
        msg: "Nodejs version is {{ nodejs_version.stdout }}"

    # create the roboshop system user that runs the app
    - name: create roboshop user
      user:
        name: roboshop
        create_home: no
        system: yes
        home: /app
        comment: "roboshop system user"
        shell: /sbin/nologin

    # remove any old copy of the app
    - name: remove default application directory
      file:
        path: /app
        state: absent

    # create an empty /app folder
    - name: create application directory
      file:
        path: /app
        state: directory

    # download the catalogue code
    - name: download catalogue microservice
      get_url:
        url: https://*****
        dest: /tmp/catalogue.zip

    # unzip the code into /app
    - name: unzip catalogue microservice
      unarchive:
        src: /tmp/catalogue.zip
        dest: /app
        remote_src: yes

    # install the app's Node.js packages
    - name: install nodejs dependencies
      community.general.npm:
        path: /app

    # create the systemd service file from the template
    - name: copy systemd service file
      template:
        src: catalogue.service.j2
        dest: /etc/systemd/system/catalogue.service

    # add the MongoDB repo, needed for the mongosh client
    - name: copy mongo repo to destination folder
      copy:
        src: mongo.repo
        dest: /etc/yum.repos.d/mongo.repo

    # install mongosh to talk to MongoDB
    - name: install mongodb client
      package:
        name: mongodb-mongosh
        state: present

    # count the products already in MongoDB
    - name: check products loaded in mongodb
      command: mongosh --host "{{ mongo_host }}" --quiet --eval 'db.getSiblingDB("catalogue").products.countDocuments()'
      register: products_count

    # print the product count
    - name: print product_count
      debug:
        msg: "{{ products_count }}"

    # load the products only if there are none yet
    - name: load schema for catalogue microservice
      shell: "mongosh --host {{ mongo_host }} < /app/db/master-data.js"
      when: products_count.stdout|int == 0

    # make systemd read the new service file
    - name: daemon reload systemd
      systemd_service:
        daemon_reload: true

    # start catalogue and start it on every boot
    - name: Ensure the application is enabled and running
      ansible.builtin.service:
        name: catalogue
        state: started
        enabled: true
← Back to portfolio

catalogue.service.j2

The catalogue systemd service. Ansible fills in {{ mongo_host }}.

[Unit]
Description=Catalogue Service
After=network-online.target

[Service]
User=roboshop
Environment=MONGO_URL="mongodb://{{ mongo_host }}:27017/catalogue"
ExecStart=/bin/node /app/server.js
SyslogIdentifier=catalogue
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
← Back to portfolio

ms-roboshop-user.yaml

Installs Node.js, deploys the user app and starts the service.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-user.yaml
- name: User microservice
  hosts: user
  become: yes
  vars:
    user_app_user: roboshop
    mysql_host: mysql.manojkumar.blog
    mongo_host: mongodb.manojkumar.blog
    redis_host: redis.manojkumar.blog
  tasks:
    # turn off the default Node.js version
    - name: disable default nodejs
      command: dnf module disable nodejs -y

    # choose Node.js 24
    - name: enable nodejs version 24
      command: dnf module enable nodejs:24 -y

    # install Node.js
    - name: install nodejs
      package:
        name: nodejs
        state: present

    # read the installed Node.js version
    - name: verify nodejs version
      command: node -v
      register: nodejs_version

    # print the Node.js version
    - name: print nodejs version
      debug:
        msg: "Nodejs version is {{ nodejs_version.stdout }}"

    # create the roboshop system user that runs the app
    - name: create roboshop user
      user:
        name: roboshop
        create_home: no
        system: yes
        home: /app
        comment: "roboshop system user"
        shell: /sbin/nologin

    # remove any old copy of the app
    - name: remove default application directory
      file:
        path: /app
        state: absent

    # create an empty /app folder
    - name: create application directory
      file:
        path: /app
        state: directory

    # download the user code
    - name: download user microservice
      get_url:
        url: https://*****
        dest: /tmp/user.zip

    # unzip the code into /app
    - name: unzip user microservice
      unarchive:
        src: /tmp/user.zip
        dest: /app
        remote_src: yes

    # install the app's Node.js packages
    - name: install nodejs dependencies
      community.general.npm:
        path: /app

    # create the systemd service file from the template
    - name: copy user service
      template:
        src: user.service.j2
        dest: /etc/systemd/system/user.service

    # make systemd read the new service file
    - name: daemon reload
      systemd_service:
        daemon_reload: true

    # restart user and start it on every boot
    - name: start and enable user
      service:
        name: user
        state: restarted
        enabled: yes
← Back to portfolio

user.service.j2

The user systemd service. Ansible fills in {{ redis_host }} and {{mongo_host}}.

[Unit]
Description=User Service
After=network-online.target

[Service]
User=roboshop
Environment=REDIS_URL="redis://{{ redis_host }}:6379"
Environment=MONGO_URL="mongodb://{{mongo_host}}:27017/users"
ExecStart=/bin/node /app/server.js
SyslogIdentifier=user
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
← Back to portfolio

ms-roboshop-cart.yaml

Installs Node.js, deploys the cart app and starts the service.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-cart.yaml
- name: cart microservice
  hosts: cart
  become: yes
  vars:
    cart_app_user: roboshop
    mysql_host: mysql.manojkumar.blog
    mongo_host: mongodb.manojkumar.blog
    cart_host: cart.manojkumar.blog
    catalogue_host: catalogue.manojkumar.blog
    redis_host: redis.manojkumar.blog
  tasks:
    # turn off the default Node.js version
    - name: disable default nodejs
      command: dnf module disable nodejs -y

    # choose Node.js 24
    - name: enable nodejs version 24
      command: dnf module enable nodejs:24 -y

    # install Node.js
    - name: install nodejs
      package:
        name: nodejs
        state: present

    # read the installed Node.js version
    - name: verify nodejs version
      command: node -v
      register: nodejs_version

    # print the Node.js version
    - name: print nodejs version
      debug:
        msg: "Nodejs version is {{ nodejs_version.stdout }}"

    # create the roboshop system user that runs the app
    - name: create roboshop user
      user:
        name: roboshop
        create_home: no
        system: yes
        home: /app
        comment: "roboshop system user"
        shell: /sbin/nologin

    # remove any old copy of the app
    - name: remove default application directory
      file:
        path: /app
        state: absent

    # remove an old download
    - name: remove cart.zip
      file:
        path: /tmp/cart.zip
        state: absent

    # create an empty /app folder
    - name: create application directory
      file:
        path: /app
        state: directory

    # download the cart code
    - name: download cart microservice
      get_url:
        url: https://*****
        dest: /tmp/cart.zip

    # unzip the code into /app
    - name: unzip cart microservice
      unarchive:
        src: /tmp/cart.zip
        dest: /app
        remote_src: yes

    # install the app's Node.js packages
    - name: install nodejs dependencies
      community.general.npm:
        path: /app

    # create the systemd service file from the template
    - name: update systemd service file
      template:
        src: cart.service.j2
        dest: /etc/systemd/system/cart.service

    # make systemd read the new service file
    - name: reload daemon
      systemd_service:
        daemon_reload: true

    # restart cart and start it on every boot
    - name: start cart service
      service:
        name: cart
        state: restarted
        enabled: yes
← Back to portfolio

cart.service.j2

The cart systemd service. Ansible fills in {{redis_host}} and {{catalogue_host}}.

[Unit]
Description=Cart Service
After=network-online.target

[Service]
User=roboshop
Environment=REDIS_HOST={{redis_host}}
Environment=CATALOGUE_HOST={{catalogue_host}}
Environment=CATALOGUE_PORT=8080
ExecStart=/bin/node /app/server.js
SyslogIdentifier=cart
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
← Back to portfolio

ms-roboshop-shipping.yaml

Installs Java and Maven, builds the shipping app, loads the cities into MySQL (only once) and starts the service. Tasks tagged mysql can be run on their own with --tags mysql.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-shipping.yaml
- name: configure shipping server
  hosts: shipping
  become: yes
  vars:
    shipping_app_user: roboshop
    mysql_host: mysql.manojkumar.blog
    cart_host: cart.manojkumar.blog
  tasks:
    # choose Maven 3.9 from the dnf modules
    - name: enable maven 3.9
      command: dnf module enable maven:3.9 -y

    # install Maven, Java 25 and the MySQL client
    - name: install maven and java25
      dnf:
        name:
          - maven
          - java-25-openjdk-devel
          - maven-openjdk25 # installing this so that we no need to set JAVA_HOME again
          - mysql
        state: present

    # create the roboshop system user that runs the app
    - name: create roboshop system user
      user:
        name: roboshop
        create_home: false
        home: /app
        system: true
        shell: /sbin/nologin
        comment: "roboshop system user"

    # remove any old copy of the app
    - name: remove existing app directory
      file:
        path: /app
        state: absent

    # remove an old download
    - name: remove shipping.zip
      file:
        path: /tmp/shipping.zip
        state: absent

    # create an empty /app folder
    - name: create app directory
      file:
        path: /app
        state: directory

    # download the shipping code
    - name: download shipping application
      get_url:
        url: https://*****
        dest: /tmp/shipping.zip

    # unzip the code into /app
    - name: unzip shipping
      unarchive:
        src: /tmp/shipping.zip
        dest: /app
        remote_src: yes

    # check which Java version Maven uses
    - name: mvn version without JAVA_HOME
      command: mvn --version
      args:
        chdir: /app
      register: mvn_default

    # print the Maven and Java versions
    - name: print mvn version without JAVA_HOME
      debug:
        msg: "{{ mvn_default.stdout_lines }}"

    # build the app into a .jar file
    - name: build shipping
      command: mvn clean package
      args:
        chdir: /app

    # move the .jar to /app/shipping.jar
    - name: move shipping jar file
      command: mv target/shipping.jar shipping.jar
      args:
        chdir: /app

    # count the cities already in MySQL
    - name: check cities data exist or not
      command: mysql -h "{{ mysql_host }}" -u root -p{{ mysql_root_password }} -N -B -e "SELECT COUNT(*) FROM cities.cities;"
      register: cities_count
      ignore_errors: true
      tags:
        - mysql

    # print the cities count
    - name: print cities_count
      debug:
        msg: "{{ cities_count }}"
      tags:
        - mysql

    # load the cities only if they aren't there yet
    - name: load cities data into mysql
      shell: mysql -h "{{ mysql_host }}" -u root -p{{ mysql_root_password }} < /app/db/master-data.sql
      when: cities_count.rc != 0 or cities_count.stdout | int == 0
      tags:
        - mysql

    # create the MySQL user the app logs in with
    - name: create app user in mysql
      shell: mysql -h "{{ mysql_host }}" -u root -p{{ mysql_root_password }} < /app/db/app-user.sql
      tags:
        - mysql

    # create the systemd service file from the template
    - name: copy shipping service
      template:
        src: shipping.service.j2
        dest: /etc/systemd/system/shipping.service

    # make systemd read the new service file
    - name: daemon reload
      systemd_service:
        daemon_reload: true

    # restart shipping and start it on every boot
    - name: start and enable shipping
      service:
        name: shipping
        state: restarted
        enabled: yes
← Back to portfolio

shipping.service.j2

The shipping systemd service. Ansible fills in {{ cart_host }} and {{ mysql_host }}. Password masked.

[Unit]
Description=Shipping Service
After=network-online.target

[Service]
User=roboshop
Environment=CART_ENDPOINT={{ cart_host }}:8080
Environment=DB_HOST={{ mysql_host }}
Environment=DB_USER=shipping
Environment=DB_PASSWORD=*****
ExecStart=/bin/java -jar /app/shipping.jar
SyslogIdentifier=shipping
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
← Back to portfolio

ms-roboshop-payment.yaml

Installs Python 3.14, deploys the payment app into a virtual environment and starts it with gunicorn.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-payment.yaml
- name: Payment service deployment using ansible script.
  hosts: payment
  become: true
  vars:
    rabbitmq_host: rabbitmq.manojkumar.blog
    cart_host: cart.manojkumar.blog
    user_host: user.manojkumar.blog
  tasks:
    # install Python 3.14 and pip
    - name: Install Python version
      package:
        name: "{{item}}"
        state: present
      loop:
        - python3.14
        - python3.14-pip

    # create the roboshop system user that runs the app
    - name: add system user for payment service
      user:
        name: roboshop
        system: true
        shell: /sbin/nologin
        create_home: no
        home: /app
        comment: "roboshop system user"

    # remove any old copy of the app
    - name: Remove app directory if exist
      file:
        path: /app
        state: absent

    # remove an old download
    - name: remove zip file if exist
      file:
        path: /tmp/payment.zip
        state: absent

    # create an empty /app folder
    - name: create app directory
      file:
        path: /app
        state: directory

    # download the payment code
    - name: download source code
      get_url:
        url: https://*****
        dest: /tmp/payment.zip

    # unzip the code into /app
    - name: unzip the source file
      unarchive:
        src: /tmp/payment.zip
        dest: /app
        remote_src: yes

    # install the Python packages into a virtual environment in /app/.venv
    - name: install dependencies in virtual environment
      ansible.builtin.pip:
        requirements: /app/requirements.txt
        virtualenv: /app/.venv
        virtualenv_command: python3.14 -m venv

    # create the systemd service file from the template
    - name: copy payment service
      template:
        src: payment.service.j2
        dest: /etc/systemd/system/payment.service

    # make systemd read the new service file
    - name: daemon reload
      systemd_service:
        daemon_reload: true

    # restart payment and start it on every boot
    - name: start and enable payment
      service:
        name: payment
        state: restarted
        enabled: yes
← Back to portfolio

payment.service.j2

The payment systemd service. Ansible fills in the cart, user and rabbitmq host names. Password masked.

[Unit]
Description=Payment Service
After=network-online.target

[Service]
User=roboshop
WorkingDirectory=/app
Environment=CART_HOST={{cart_host}}
Environment=CART_PORT=8080
Environment=USER_HOST={{user_host}}
Environment=USER_PORT=8080
Environment=AMQP_HOST={{rabbitmq_host}}
Environment=AMQP_USER=roboshop
Environment=AMQP_PASS=*****
ExecStart=/app/.venv/bin/gunicorn --config gunicorn.conf.py --no-control-socket payment:app
SyslogIdentifier=payment
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
← Back to portfolio

ms-roboshop-dispatch.yaml

Installs Go, builds the dispatch program and starts the service.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-dispatch.yaml
- name: dispatch microservice deployment script using ansible
  hosts: dispatch
  become: yes
  vars:
    rabbitmq_host: rabbitmq.manojkumar.blog
  tasks:
    # install Go
    - name: install go lang
      package:
        name: golang
        state: present

    # read the installed Go version
    - name: golang version
      command: go version
      register: go_version

    # print the Go version
    - name: print go version
      debug:
        msg: "{{go_version}}"

    # create the roboshop system user that runs the app
    - name: create go system user
      user:
        name: roboshop
        home: /app
        system: true
        shell: /sbin/nologin
        comment: "roboshop system user"
        create_home: no

    # remove any old copy of the app
    - name: remove app directory if exist
      file:
        path: /app
        state: absent

    # remove an old download
    - name: remove zip file if exist
      file:
        path: /tmp/dispatch.zip
        state: absent

    # create an empty /app folder
    - name: add application home directory
      file:
        path: /app
        state: directory

    # download the dispatch code
    - name: download source code
      get_url:
        url: https://*****
        dest: /tmp/dispatch.zip

    # unzip the code into /app
    - name: unarchive source code to app directory
      unarchive:
        src: /tmp/dispatch.zip
        dest: /app
        remote_src: yes

    # build the Go code into the /app/dispatch program
    - name: build dispatch project
      command: go build -o dispatch
      args:
        chdir: /app

    # create the systemd service file from the template
    - name: copy dispatch service
      template:
        src: dispatch.service.j2
        dest: /etc/systemd/system/dispatch.service

    # make systemd read the new service file
    - name: daemon reload
      systemd_service:
        daemon_reload: true

    # restart dispatch and start it on every boot
    - name: start service
      service:
        name: dispatch
        state: restarted
        enabled: true
← Back to portfolio

dispatch.service.j2

The dispatch systemd service. Ansible fills in {{rabbitmq_host}}. Password masked.

[Unit]
Description=Dispatch Service
After=network-online.target

[Service]
User=roboshop
Environment=AMQP_HOST={{rabbitmq_host}}
Environment=AMQP_USER=roboshop
Environment=AMQP_PASS=*****
ExecStart=/app/dispatch
SyslogIdentifier=dispatch
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
← Back to portfolio

ms-roboshop-frontend.yaml

Installs nginx, replaces the default web page with the roboshop website and adds the API routing config.

Run it:

ansible-playbook -i inventory.ini ms-roboshop-frontend.yaml
- name: Deploying frontend application using ansible script
  hosts: frontend
  become: yes
  vars:
    cart_host: cart.manojkumar.blog
    catalogue_host: catalogue.manojkumar.blog
    shipping_host: shipping.manojkumar.blog
    user_host: user.manojkumar.blog
    payment_host: payment.manojkumar.blog
  tasks:
    # turn off the default nginx version
    - name: disable nginx default version
      command: dnf module disable nginx -y

    # choose nginx 1.26
    - name: enable nginx 1.26 version
      command: dnf module enable nginx:1.26 -y

    # install nginx
    - name: install nginx 1.26 version
      package:
        name: nginx
        state: present

    # list everything in the default web folder
    - name: Find all contents inside the directory
      ansible.builtin.find:
        paths: /usr/share/nginx/html/
        recurse: true
        hidden: true
        file_type: any
      register: directory_contents

    # print what was found
    - name: print directory content
      debug:
        msg: "{{ directory_contents }}"

    # delete the default nginx web page files
    - name: Delete the discovered contents
      ansible.builtin.file:
        path: "{{ item.path }}"
        state: absent
      loop: "{{ directory_contents.files }}"

    # download the frontend code
    - name: download the source code
      get_url:
        url: https://*****
        dest: /tmp/frontend.zip

    # unzip the website into the nginx web folder
    - name: unzip frontend
      unarchive:
        src: /tmp/frontend.zip
        dest: /usr/share/nginx/html/
        remote_src: yes

    # create the nginx config from the template
    - name: copy roboshop config
      template:
        src: roboshop.conf.j2
        dest: /etc/nginx/default.d/roboshop.conf

    # restart nginx so it loads the new site and config
    - name: restart frontend
      service:
        name: nginx
        state: restarted
← Back to portfolio

roboshop.conf.j2

The nginx config for frontend. It sends each /api/<service> call to the right server and serves the website.

server_tokens off;

# nginx gives every request a unique id ($request_id). It is sent to the
# services and back to the browser, so one click can be traced on all servers
add_header X-Request-Id $request_id always;
add_header X-Content-Type-Options nosniff always;
add_header X-Frame-Options SAMEORIGIN always;
add_header Referrer-Policy strict-origin-when-cross-origin always;

gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;
gzip_min_length 1024;

# common settings for all backend calls
proxy_http_version 1.1;
proxy_set_header Host              $host;
proxy_set_header X-Real-IP         $remote_addr;
proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Request-Id      $request_id;
# the services send the id back too, nginx already adds it, so hide theirs
proxy_hide_header X-Request-Id;
proxy_connect_timeout 5s;
proxy_read_timeout    30s;   # 504 if a service takes longer than this

# the trailing / removes the /api/<service> part:
# /api/catalogue/products -> https://*****
location /api/catalogue/ { proxy_pass http://{{catalogue_host}}:8080/; }
location /api/user/      { proxy_pass http://{{user_host}}:8080/; }
location /api/cart/      { proxy_pass http://{{cart_host}}:8080/; }
location /api/shipping/  { proxy_pass http://{{shipping_host}}:8080/; }
location /api/payment/   { proxy_pass http://{{payment_host}}:8080/; }

# when nginx itself cannot reach a service, answer in JSON like the services do.
# errors a service returns on its own (its 404, 503...) pass through untouched
error_page 502 = @bad_gateway;
error_page 504 = @gateway_timeout;
location @bad_gateway {
    default_type application/json;
    return 502 '{"message":"service not reachable (502 Bad Gateway from nginx)"}';
}
location @gateway_timeout {
    default_type application/json;
    return 504 '{"message":"service did not answer in time (504 Gateway Timeout from nginx)"}';
}

# nginx's own connection counters
location = /health {
    stub_status on;
    access_log off;
}

# product images, missing ones fall back to the placeholder
location /images/ {
    expires 1h;
    try_files $uri /images/placeholder.jpg;
}

# file names in /assets contain a hash of their content, so they can be cached forever
location /assets/ {
    expires max;
    try_files $uri =404;
}

# React handles all other paths (/product/RMC, /cart ...) in the browser,
# so every unknown path returns index.html
location / {
    expires -1;
    try_files $uri $uri/ /index.html;
}
← Back to portfolio