Deploying a Nodejs app using Ansible

Deploying a Nodejs app using Ansible

When you need to automatically create a machine on DigitalOcean for projects, provision it and then upload an app and run it. Then you may want to be able to deploy it again later on or make changes to the infrastructure in a more controlled way.

bootstrapping the projects looked like this:

  • I would create a DigitalOcean droplet
  • I would ssh into it after I have added my SSH key
  • I would put a simple configuration, install dependencies, node.js runtime, etc
  • Copy and unpack tar file on the remote server
  • Install node and npm for the node.js application
  • Run it

Before diving deep in the project let see some definitions.

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast and scalable network applications.

Ansible is an automation tool for running commands on a remote server. Ansible can do anything that you would do from the command line.

Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.

A module is a command or set of similar Ansible commands meant to be executed on the client-side

A task is a section that consists of a single procedure to be completed

Inventory refers File containing data about the ansible client servers.


1: Create a droplet

https://docs.digitalocean.com/products/droplets/how-to/create/

2: Create a hosts file

In our project folder we create a host file were will use ansikey (keypair) used to log to our server as root user

178.62.225.101 ansible_ssh_private_key_file=~/.shh/ansikey ansible_user=root        

3: Let's start writing ansible-playbook

--
- name: Install node and npm
  hosts: 178.62.225.101
  tasks:
    - name: Update apt repo and cache
      apt: update_cache=yes force_apt_get=yes cache_valid_time=3600
    - name: Install nodejs and npm
      apt:
        pkg:
          - nodejs
          - npm
        

Best practice recommend to create new user for new service

- name: Create new linux user for node app
  hosts: 178.62.225.101
  tasks:
    - name: Create linux user
      user:
        name: antoine
        comment: Node User
        group: admin        

Since logging in as root to launch the app will present some vulnerability to are app we will use the user created to deploy

- name: Deploy nodejs app
  hosts: 178.62.225.101
  become: True
  become_user: antoine
tasks:
    - name: Unpack the nodejs file
      unarchive:
        src: /home/laptopuser/Downloads/node-app-c-main/nodejs-app-1.0.0.tgz
        dest: /home/antoine
    - name: Install dependencies
      npm:
        path: /home/antoine/package
    - name: Start the application
      command:
        chdir: /home/antoine/package/app
        cmd: node server
      async: 1000
      poll: 0
    - name: Ensure app is running
      shell: ps aux | grep node
      register: app_status
    - debug: msg={{app_status.stdout_lines}}-
        

To get the tar file you can clone it at https://github.com/kevAnto/node-app-c an personalize the source directory.

On executing the commend

ansible-playbook -i hosts deploy-node.yaml        

the result will be as followed

No alt text provided for this image
No alt text provided for this image

Despite the fact that app_status give us a view of the app running after executing ansible-playbook we can still verify if the node app we installed by executing the command

ps aux | grep node        
No alt text provided for this image

Whenever we wanted to release a new version, we would have logged into the droplet copy and unpack tar file on the remote server and restart the node app. The same procedure would be for every other stack.

Conclusion

You can actually describe the above procedure in a single file and run it as many times as you like. Ansible will make sure that every time only the needed changes would run. You can run it once and build the application Alternatively, you can run it with an existing droplet and it will just pull your application and rerun it.



Evgeny Dudko

DevOps Engineer @ Matrix | Docker , k8s , Helm , MloPs, Linux.

2 年

Thank you! very clear and useful example!

回复

要查看或添加评论,请登录

Antoine CHOULA的更多文章

社区洞察

其他会员也浏览了