Skip to content

Commit

Permalink
vagrant done
Browse files Browse the repository at this point in the history
Signed-off-by: Your Name <[email protected]>
  • Loading branch information
Your Name committed Jan 6, 2024
1 parent 5e9ed74 commit 35c7972
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions iac/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vagrant
43 changes: 43 additions & 0 deletions iac/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Vagrant.configure("2") do |config|

# Do not pay attention to this parameter
if Vagrant.has_plugin?("vagrant-vbguest")
config.vm.provider :virtualbox do |vb|
config.vbguest.auto_update = false
end
end

config.ssh.insert_key = false

# Define the nodejs_server VM
config.vm.define "nodejs_server" do |server|
# Specify the Vagrant box to use
server.vm.box = "ubuntu/bionic64"
# Specify the VM ip address
config.vm.network "forwarded_port", guest: 3000, host: 3000
# Specify the VM specs when using the Virtualbox provisioner
server.vm.provider "virtualbox" do |vb|
vb.name = "nodejs.server.local"
# VM RAM in MB
vb.memory = 2048
# VM CPUs
vb.cpus = 1
end
config.vm.provider "vmware_desktop" do |vmware|
vmware.vmx["memsize"] = "2048"
vmware.vmx["numvcpus"] = "1"
end
end

# Use Vagrant Ansible provisioner
config.vm.provision "ansible_local" do |ansible|
# The path to the playbooks entry point
ansible.version = "2.10"
ansible.playbook = "ansible/playbooks/install.yml"
# Only run the roles with these tags
ansible.tags = "install"
end

end

59 changes: 59 additions & 0 deletions iac/ansible/playbooks/install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
- name: Install Node.js and MongoDB
hosts: all
become: true
tasks:
- name: Update apt cache
apt:
update_cache: yes

- name: Install Node.js and npm
apt:
name: "{{ item }}"
state: present
loop:
- nodejs
- npm

- name: Install MongoDB
apt:
name: mongodb
state: present

- name: Start MongoDB service
service:
name: mongodb
state: started

- name: Clone Node.js Application from GitHub
hosts: all
become: true
tasks:
- name: Clone the GitHub repository
git:
repo: https://github.com/deepanshu-yadav/devopsprojects23.git
dest: /opt/nodejs-app
register: git_clone

- name: Install npm dependencies
command: npm install
args:
chdir: /opt/nodejs-app

- name: Check Health of Node.js Application
hosts: all
become: true
tasks:
- name: Check if Node.js app is running
shell: ps aux | grep 'node /opt/nodejs-app/server.js' | grep -v grep
register: node_app_status
failed_when: node_app_status.rc != 0

- name: Check if MongoDB is running
service_facts:
register: services
- assert:
that:
- "'mongodb' in services.services"
- "'running' in services.services.mongodb.state"
fail_msg: "MongoDB is not running."

0 comments on commit 35c7972

Please sign in to comment.