Scenario / Questions

I’m using Ansible to create a pm2 service on a EC2 / Ubuntu instance. Below is the script. When I run it, PM2 is installed and the service is enabled. When I run pm2 list, I don’t the see the service, but I can grep it (ps aux | grep node) and see that it’s running. It also seems like a shadow copy of pm2 is running and loading the app, but I can’t seem to control it.

- hosts: comm
  sudo: yes
  tasks:
    - npm: name=pm2 global=yes
    - name: configure pm2 to restart on startup
      shell: pm2 startup ubuntu >& /dev/null chdir=~/ executable=/bin/bash
      sudo: yes
      sudo_user: root
    - command: sudo env PATH=$PATH:/usr/bin pm2 startup ubuntu -u ubuntu
      sudo: yes
    - command: /usr/bin/pm2 save
    - command: /usr/bin/pm2 start /home/ubuntu/something/app.js --name something

Find below all possible solutions or suggestions for the above questions..

Suggestion: 1

This playbook is not idempotent, the shell task is going to run every time the playbook is run, you need to supply a creates argument to that task.

In the current version of PM2:

var scriptFile = '/etc/init.d/pm2-init.sh',
  script = cst.UBUNTU_STARTUP_SCRIPT;

Also, the use of sudo is redundant, as it is already specified globally above, and the default is to sudo to root, so that’s redundant as well.

Additionally, once there’s an init script for the service, it would be better to use the service module to manage it.