Jacob Lyles Jacob Lyles - 1 year ago 74
Node.js Question

Ansible hangs when starting node.js server

I would like to start my node.js app in an ansible playbook. Right now, the final directive looks like this:

- name: start node server
shell: chdir=${app_path} npm start&

The problem is that ansible never returns from this. How can I make it continue?

Answer Source

Forever seems to be the simplest and easiest way of starting and daemonizing Node.js apps. Currently, there's no Ansible module for forever, but you can still use the following plays to install forever and run your app:

- name: "Install forever (to run Node.js app)."
  npm: name=forever global=yes state=present

- name: "Check list of Node.js apps running."
  command: forever list
  register: forever_list
  changed_when: false

- name: "Start example Node.js app."
  command: forever start /path/to/app.js
  when: "forever_list.stdout.find('/path/to/app.js') == -1"

This is completely idempotent, and works great for me. You could program a little forever module for Ansible to do this stuff for you (like the service module), but this works for now.

I have a complete example of how to start a Node.js app with Forever and Ansible on Server Check.in's blog.