Install nodejs, and use npm to install n for node version management.

This commit is contained in:
Brian Lee 2023-08-13 08:54:05 -07:00
parent 6269782412
commit e835992ef4
5 changed files with 50 additions and 12 deletions

View File

@ -1,9 +1,11 @@
# Ansible Role: yarn
# Ansible Role: nodejs
Install yarn packages from the official yarn repositories.
Install nodejs, npm and yarn packages from the official yarn repositories. Optionally install n from npm repositories for version management.
Tested on:
* Archlinux
* Debian 11
* Ubuntu 22.04
## Requirements
@ -12,13 +14,18 @@ None.
## Role Variables
```yaml
node_version: node_20.x
n_enabled: yes
```
See the role [vars](vars/main.yml)
## Example Playbook
```yaml
- hosts: yarn
- hosts: nodejs
roles:
- role: bleetube.yarn
- role: bleetube.nodejs
become: true
```

3
defaults/main.yml Normal file
View File

@ -0,0 +1,3 @@
---
node_version: node_20.x
n_enabled: yes # https://github.com/tj/n

View File

@ -1,12 +1,18 @@
---
# https://www.jeffgeerling.com/blog/2022/aptkey-deprecated-debianubuntu-how-fix-ansible
- name: Add nodesource apt repository key.
ansible.builtin.get_url:
url: "{{ nodesource_repo_key_url }}"
dest: /usr/share/keyrings/nodesource-archive.asc
mode: '0644'
- name: Add yarn apt repository key.
ansible.builtin.get_url:
url: "{{ yarn_repo_key_url }}"
dest: /usr/share/keyrings/yarn-archive.asc
mode: '0644'
- name: Ensure the repository is added with the relevant trusted GPG key
- name: Ensure the yarn repository is added with the relevant trusted GPG key
ansible.builtin.lineinfile:
path: /etc/apt/sources.list.d/yarn.list
regexp: dl.yarnpkg.com
@ -14,13 +20,23 @@
create: true
register: yarn_repo
- name: Ensure the nodesource repository is added with the relevant trusted GPG key
ansible.builtin.lineinfile:
path: /etc/apt/sources.list.d/nodesource.list
regexp: deb.nodesource.com
line: "deb [arch=amd64 signed-by=/usr/share/keyrings/nodesource-archive.asc] https://deb.nodesource.com/{{ node_version }} {{ ansible_distribution_release }} main"
create: true
register: nodesource_repo
- name: Update the apt repository cache
ansible.builtin.apt:
update_cache: yes
when: yarn_repo.changed
when: nodesource_repo.changed or yarn_repo.changed
- name: Install the yarn package
- name: Install the nodejs and yarn packages
ansible.builtin.apt:
name: yarn
state: present
name:
- nodejs
- yarn
state: latest

View File

@ -1,10 +1,18 @@
---
- name: Install yarn (Debian)
- name: Install npm and yarn (Debian)
import_tasks: Debian.yml
when: ansible_os_family == 'Debian'
- name: Install yarn (non-Debian)
- name: Install npm and yarn (non-Debian)
ansible.builtin.package:
name: yarn
name:
- nodejs
- yarn
state: present
when: ansible_os_family != 'Debian'
- name: Install 'n' package
ansible.builtin.npm:
name: n
global: yes
when: n_enabled

View File

@ -1,2 +1,6 @@
---
# Nodesource key ID: 9FD3B784BC1C6FC31A8A0A1C1655A0AB68576280
nodesource_repo_key_url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
# Yarn key ID: 72ECF46A56B4AD39C907BBB71646B01B86E50310
yarn_repo_key_url: https://dl.yarnpkg.com/debian/pubkey.gpg