Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing NodeJS LTS for Ansible

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?

like image 200
Janshair Khan Avatar asked Aug 23 '17 13:08

Janshair Khan


4 Answers

Here is the working example:

---
- hosts: all
  gather_facts: yes
  become: yes
  vars:
    NODEJS_VERSION: "8"
  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

like image 76
Arbab Nazar Avatar answered Nov 19 '22 06:11

Arbab Nazar


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
like image 30
Edward Maxedon Avatar answered Nov 19 '22 07:11

Edward Maxedon


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
like image 31
JL Peyret Avatar answered Nov 19 '22 06:11

JL Peyret


You can use:

ansible-galaxy install nodesource.node

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

like image 1
fritz_da_silva Avatar answered Nov 19 '22 07:11

fritz_da_silva