Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using pm2 with ansible

I am trying to start a node program using pm2 via ansible. The problem is that the pm2 start command is not idempotent under ansible. It gives error when run again.

This is my ansible play

- name: start the application
  become_user: ubuntu
  command: pm2 start app.js -i max
  tags: 
    - app

Now if i run this the first time then it runs properly but when i run this again then i get the error telling me that the script is already running.

What would be the correct way to get around this error and handle pm2 properly via ansible.

like image 445
shivams Avatar asked Jan 30 '23 23:01

shivams


1 Answers

Before starting the script you should delete previous, like this:

 - name: delete existing pm2 processes if running
   command: "pm2 delete {{ server_id }}"
   ignore_errors: True
   become: yes
   become_user: rw_user


 - name: start pm2 process
   command: 'pm2 start -x -i 4 --name "{{server_id}}" server.js'
   become: yes
   become_user: rw_user
   environment:
    NODE_ENV: "{{server_env}}"
like image 136
Łukasz Szewczak Avatar answered Feb 02 '23 04:02

Łukasz Szewczak