This project provides an Ansible playbook for deploying a NestJS boilerplate application with PostgreSQL, RabbitMQ, and Nginx.
- Ansible installed on the control machine
- SSH access to the target host(s)
- Git installed on the control machine
- Generate an SSH key pair on the control machine:
ssh-keygen
- Install Ansible:
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
- Clone the repository:
git clone https://github.com/Rob-in-son/hng_boilerplate_nestjs.git
cd hng_boilerplate_nestjs
git checkout devops
cd ansible
- Configure the inventory file:
Create or edit
inventory.yml
with the following structure in the ansible dir
all:
hosts:
<host_name>:
ansible_host: <host_ip>
ansible_user: <username>
ansible_ssh_private_key_file: <private_key_path>
- main.yml: The main Ansible playbook
- nginx.conf.j2: Nginx configuration template
- nestjs.service.j2: Systemd service configuration template
To deploy the application, run:
ansible-playbook main.yml -i inventory.yml -v
- Sets up a new user for the application
- Installs necessary dependencies (Node.js, PostgreSQL, Nginx, RabbitMQ)
- Clones the application repository
- Sets up the PostgreSQL database
- Configures the application environment
- Sets up Nginx as a reverse proxy
- Configures UFW firewall
- Sets up the application as a systemd service
The playbook uses several variables that can be customized in the vars section of main.yml, including:
- Application port
- JWT settings
- And more
- /var/log/stage_5b/out.log
- /var/log/stage_5b/error.log
/var/log/nginx/stage_5b_access.log /var/log/nginx/stage_5b_error.log
- UFW is configured to allow only SSH (port 22) and HTTP (port 80) traffic
- The application runs on port 3000, which is not directly accessible from outside
- PostgreSQL credentials are stored in /var/secrets/pg_pw.txt
If you encounter issues with the deployment:
- Check the Ansible output for error messages
- Review the application and Nginx log files
- Ensure all required ports are open and services are running