Installing Nodered using Ansible

Recently I've been playing around with Vagrant and Ansible to configure my home server. The server runs a few local network services and I'm working on some home automation stuff which I hope to base on NodeRed, in this post I'll describe how I've used Ansible to install NodeRed with git.

Installation

Dependencies

Since Nodered is a NodeJS application, we need to install a few dependencies first, I'm using apt and npm to do this but the method can be easily modified to the appropriate package manager:

- name: Add node key
  sudo: yes
  apt_key: url=https://deb.nodesource.com/gpgkey/nodesource.gpg.key state=present

- name: Add repos
  sudo: yes
  apt_repository: repo='deb https://deb.nodesource.com/node precise main'
  
- name: Install packages
  sudo: yes
  apt: upgrade=dist
  apt: pkg= state=installed update_cache=yes
  with_items:
 
  - nodejs
  - git-core

- name: Setup Forever
  sudo: yes
  npm: name=forever global=yes
  

NodeRed

There are a few ways to install Nodered; from a downloaded archive, using git or plain npm, I've chosen git as it allows me to easily upgrade just by changing the version tag and rerunning Ansible. All the functionality to get the code is built into Ansible:

- name: GetNodeRed
  sudo: yes
  sudo_user: oliversmith
  git: repo=https://github.com/node-red/node-red.git dest=/home/oliversmith/NodeRed version=0.9.0

- name: Install NodeRed
  sudo: yes
  sudo_user: oliversmith
  npm: name=npm global=yes registry=http://registry.npmjs.org state=latest
  npm: path=/home/oliversmith/NodeRed/ production=yes

Running

Launching node applications from the command line is tricky as if they crash the application doesn't have a mechanism to recover, I've chosen to use forever to solve this issue, however it won't make the application start on boot so further configuration would be required to enable that.

Forever has already been installed using npm (above) so after installation we can dive straight into running it:

- name: Run NodeRed
  sudo: yes
  sudo_user: oliversmith
  shell: forever start red.js
  args:
    chdir: /home/oliversmith/NodeRed/