This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
forked from khanmaster/eng89_ansible_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeapp_latest_config.yml
91 lines (77 loc) · 2.39 KB
/
nodeapp_latest_config.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# On the web server we would like to install nodejs with required dependencies so we could launch the nodeapp on web server's IP:3000
# Then moving on to configuring reverse proxy with nginx so we could launch the app on port 80 instead of 3000
---
# name of the hosts - hosts is to define the name of your host of all
- hosts : web
# find the facts about the host
gather_facts : yes
# admin access
become: true
# Variables we're going to be using multiple times
vars:
git_repo_url: https://github.com/monotiller/engineering89_multi_server_automation.git
repo_path: /app
nginx_default: /etc/nginx/sites-available/default
tasks:
- name: Install nginx
apt: pkg=nginx state=present update_cache=yes
- name: Delete current nginx default
file:
path: "{{ nginx_default }}"
state: absent
- name: Create nginx default
file:
path: "{{ nginx_default }}"
state: touch
mode: 0644
- name: Change nginx default
blockinfile:
path: "{{ nginx_default }}"
block: |
server {
listen 80;
server_name _;
location / {
include proxy_params;
proxy_pass http://192.168.33.10:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
- name: Restart nginx
service:
name: nginx
state: restarted
- name: Install nodejs and npm
apt: pkg=nodejs state=present update_cache=yes
apt: pkg=npm state=present update_cache=yes
- name: Install pm2
npm:
name: pm2
global: yes
# Copy the app code to web server (you clone it from your repo, copy from your controller to web or os to web)
- name: Cloning repository
git:
repo: "{{ git_repo_url }}"
dest: "{{ repo_path }}"
accept_hostkey: yes
# Configuration so we can connect to the database
- name: Exporting the DB_HOST
become_user: root
blockinfile:
path: ~/.bashrc
backup: yes
block: |
"DB_HOST=192.168.33.11:27017/posts"
- name: Installing node prerequisites and seeding from the database
shell: |
cd {{ repo_path }}/app
npm install
node seeds/seed.js
- name: Starting the app
shell: |
pm2 kill
pm2 start app.js