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?
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
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
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).
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
You can use:
ansible-galaxy install nodesource.node
and then on your playbook, add
roles:
- nodesource.node
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With