Skip to content

Instantly share code, notes, and snippets.

@kbariotis
Last active November 27, 2023 21:02
Show Gist options
  • Star 46 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save kbariotis/58d6ed6d634a8f2ae2bfa4a558d3113c to your computer and use it in GitHub Desktop.
Save kbariotis/58d6ed6d634a8f2ae2bfa4a558d3113c to your computer and use it in GitHub Desktop.
Ansible playbook for deploying a Node.js app to DigitalOcean
- name: DO
hosts: localhost
vars:
project_name: "PUT A NAME FOR YOUR PROJECT HERE"
do_token: "PUT YOUR DIGITAL OCEAN API KEY HERE ==> https://cloud.digitalocean.com/settings/api/tokens"
repository: "PUT YOUR REPOSITORY URL HERE"
tasks:
- name: LOCAL | Generate SSH key
shell: ssh-keygen -b 2048 -t rsa -f ~/.ssh/{{project_name}} -q -N ""
args:
creates: ~/.ssh/{{project_name}}
- name: DO | Create DigitalOcean SSH Key
digital_ocean:
state: present
command: ssh
name: home
ssh_pub_key: "{{ lookup('file', '~/.ssh/{{project_name}}.pub') }}"
api_token: "{{do_token}}"
register: digital_ocean_key
- name: DO | Create DigitalOcean Droplet
digital_ocean:
state: present
command: droplet
name: "{{project_name}}"
api_token: "{{do_token}}"
size_id: 512mb
region_id: ams2
image_id: centos-7-2-x64
ssh_key_ids: "{{ digital_ocean_key.ssh_key.id }}"
wait_timeout: 500
unique_name: yes
when: digital_ocean_key.ssh_key is defined
register: digital_ocean_droplet
- name: ANSIBLE | Add new host to our inventory.
add_host:
name: "{{ digital_ocean_droplet.droplet.ip_address }}"
groups: do
when: digital_ocean_droplet.droplet is defined
- name: SERVER
hosts: do
remote_user: root
gather_facts: false
vars:
node_version: 6
tasks:
- name: SERVER | Wait for port 22 to become available.
local_action: "wait_for port=22 host={{ inventory_hostname }}"
- name: SERVER | Download the nvm(node version manager) install script
get_url: url=https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh dest=/tmp/install.sh
- name: SERVER | Install dependencies
yum: name={{ item }} state=present
with_items:
- git
- curl
- name: SERVER | Execute the nvm install script
shell: bash install.sh chdir=/tmp executable=/bin/bash
- name: SERVER | Register the NVM_DIR
shell: echo $NVM_DIR
register: nvm_dir
- name: SERVER | Install the specified node version using the nvm command and set it as default
shell: . {{ nvm_dir.stdout }}/nvm.sh && nvm install {{ node_version }} && nvm run {{node_version}} --version && nvm alias default {{node_version}}
creates=~/.nvm/versions/node/v{{ node_version }}
- name: SERVER | Installing NGINX repo rpm
yum:
name: http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- name: NGINX | Installing NGINX
yum:
name: nginx
state: latest
- name: NGINX | Configure site
template: src={{project_name}}.j2 dest=/etc/nginx/conf.d/default.conf
- name: NGINX | Starting NGINX
service:
name: nginx
state: started
- name: NODE | Clone/Pull repo
git:
repo: "{{repository}}"
dest: /var/www
register: git_finished
- name: NODE | Install npm deps
shell: npm i
register: npm_finished
failed_when: '"ERR!" in npm_finished.stderr'
when: git_finished.changed
args:
chdir: /var/www
- name: NODE | Install pm2
npm:
name: pm2
global: yes
production: yes
state: present
- name: NODE | Stop APP
shell: pm2 stop app
args:
chdir: /var/www
ignore_errors: yes
- name: NODE | Start APP
shell: pm2 start server.js --name server
args:
chdir: /var/www
ignore_errors: yes
when: npm_finished.changed
@maximtop
Copy link

maximtop commented Sep 1, 2017

Hi. Nice playbook. Can you share how looks your template src={{project_name}}.j2

@TafkaMax
Copy link

TafkaMax commented May 4, 2021

.bashrc doesnt load in non-interactive shell mode. cant find NVM_DIR variable

@TafkaMax
Copy link

TafkaMax commented May 5, 2021

If you install using nvm then you need to specify
environment:
PATH: "/location/to/node/bin:{{ ansible_env.PATH }}"

Because npm scripts try to look for node, but if you are not using package manager installed (APT) node that throws its path to /usr/bin/ then the scripts cant find node nor npm.

@7flash
Copy link

7flash commented Mar 19, 2022

Thanks, my first deployment with ansible and I love it

@kbariotis
Copy link
Author

Thanks @TafkaMax , unfortunately this gist is quite old. Check out my repo with a full ansible setup with Digital Ocean. https://github.com/kbariotis/ansible-nodejs-digitalocean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment