15

I'm looking for an appropriate Ansible Role or Ansible YAML file for installing NodeJS LTS on a Ubuntu 16.04.3 xenial system. I tried more than 10 Ansible roles from Galaxy but didn't find any of them working (throws error such as potentially dangerous to add this PPA etc..

Can anyone provide any Ansible playbook or suggest me a role to install NodeJS LTS on Ubuntu 16.04?

Janshair Khan
  • 213
  • 1
  • 2
  • 11
  • Do you know what are the steps to install it without Ansible? If so, why don't you create your own role? – JorgeeFG Aug 23 '17 at 13:19

4 Answers4

23

Here is the working example:

---
- hosts: all
  gather_facts: yes
  become: yes
  vars:
    NODEJS_VERSION: "8"
    ansible_distribution_release: "xenial" #trusty
  tasks:
    - name: Install the gpg key for nodejs LTS
      apt_key:
        url: "https://deb.nodesource.com/gpgkey/nodesource.gpg.key"
        state: present

    - name: Install the nodejs LTS repos
      apt_repository:
        repo: "deb https://deb.nodesource.com/node_{{ NODEJS_VERSION }}.x {{ ansible_distribution_release }} main"
        state: present
        update_cache: yes

    - name: Install the nodejs
      apt:
        name: nodejs
        state: present

Hope it will help you

hris.to
  • 5,777
  • 3
  • 42
  • 52
Arbab Nazar
  • 18,514
  • 8
  • 66
  • 74
  • it somehow works but shows the error `"msg": "unsupported parameter for module: filename"`. Any idea? – Janshair Khan Aug 24 '17 at 04:38
  • try now, which version of ansible are you using? – Arbab Nazar Aug 24 '17 at 05:38
  • I'm using `ansible 2.0.0.2` – Janshair Khan Aug 24 '17 at 05:52
  • Thanks, I upgraded ansible to `2.3.2.0` and it worked. – Janshair Khan Aug 24 '17 at 06:27
  • 5
    what are the possible values for the var `ansible_distribution_release`? – user1623521 Jan 22 '18 at 04:24
  • the values of `ansible_distribution_release` might be `trusty` `xenial` etc – Arbab Nazar Feb 20 '18 at 19:50
  • I got the following error message, "The repository 'https://deb.nodesource.com/node_8.* xenial Release' does not have a Release file., W:Data fro m such a repository can't be authenticated and is therefore potentially dangerous to use". I "fixed" it by adding allow_unauthenticated: yes to my apt task. Is there a better way of fixing this? – Tim Stewart Apr 12 '18 at 17:33
  • @TimStewart updated the answer, can you please try that. also tell me the version of ansible that you are using. I have tested it with ansible 2.4.x – Arbab Nazar Apr 12 '18 at 18:06
  • 1
    You don't need to set the `ansible_distribution_release` variable- Ansible should feed that variable in without the line there at all – Matt Fletcher Jan 13 '19 at 19:44
  • How ansible wil know that we need to install the node repo for trusty or xenial etc? – Arbab Nazar Jan 14 '19 at 10:55
  • The following task should be added - the installation of "transport-https" - name: Ensure apt-transport-https is installed. apt: name: apt-transport-https state: present – RtmY May 12 '19 at 22:49
6

Not that I am really happy with having had to do this, but...

(env: Ubuntu 18.04, ansible 2.6.1, host: macOS )

from https://github.com/nodesource/distributions/blob/master/README.md#debinstall

- name: install node 
  shell: |
    curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - && sudo apt-get install -y nodejs

result:

> vagrant@vagrant:~$ node --version
v10.15.2

and npm must have come along too:

vagrant@vagrant:~$ npm --version
6.4.1

At the time I ran this, https://www.npmjs.com/package/npm was showing 6.8.0 as latest, with 6.4.1 being from 6 months before. Node was showing 10.15.2 as latest in 10.x series, dating 5 days before.

btw, I also tried apt-get but that ended with node 8.x rather than 10.x

And the reason I didn't use an ansible galaxy role is that I didn't see any nodejs ones that seemed to come from well-known authors and with lots of stars and downloads (I am cautious and suspicious).

updating npm

My dev machine had 6.8.0 so I added this:

vars.yml:

versions:
  npm: "6.8.0"

playbook.yml:

- name: npm self-update
  command: npm install npm@{{ versions.npm }} -g

which got me all the way to:

vagrant@vagrant:~$ npm --version
6.8.0
Community
  • 1
  • 1
JL Peyret
  • 7,549
  • 2
  • 34
  • 48
  • Can you explain why you selected this approach over the selected answer? – RtmY May 13 '19 at 07:59
  • 1
    Less installation of new stuff and it’s after all from the node’s team own instructions. I am not saying the accepted answer is bad, and I am not that strong in Ansible, but I considered it a simple shell command so I did it and moved on. Not that concerned about idempotence either, this server will be rebuilt rather than maintained. I would still strongly avoid, for security, any third-tier Galaxy role or random PPA though that is not an issue with the accepted answer. – JL Peyret May 13 '19 at 15:03
5

The accepted answer is good, but if you want you can use discovered variables for the distro codename (namely ansible_lsb.codename). Also, ensuring that gcc g++ make are installed on the host machine ensures native addons for nodejs will work properly.

Just replace node version 12 with what you'd like.

---
- name: Install nodejs
  hosts: all
  become: true
  tasks:
    - name: install nodejs prerequisites
      apt:
        name:
          - apt-transport-https
          - gcc
          - g++
          - make
        state: present
    - name: add nodejs apt key
      apt_key:
        url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
        state: present
    - name: add nodejs repository
      apt_repository:
        repo: deb https://deb.nodesource.com/node_12.x {{ ansible_lsb.codename }} main
        state: present
        update_cache: yes
    - name: install nodejs
      apt:
        name: nodejs
        state: present
Edward Maxedon
  • 801
  • 2
  • 10
  • 17
1

You can use:

ansible-galaxy install nodesource.node

and then on your playbook, add roles: - nodesource.node

Andrey Tyukin
  • 38,712
  • 4
  • 38
  • 75
  • For all who puts "-", please describe why. I don't see any problems with this suggestion. It does not work or what? – Sergey Yarotskiy Jul 23 '18 at 18:01
  • @SergeyYarotskiy For start, it appears to be incomplete. Question asked about Ansible playbook and here there is a manual unexplained installation step (`ansible-galaxy`). Maybe it is obvious what it is doing, but I am here because I am na Ansible newbie. – reducing activity Aug 26 '20 at 21:07