From e835992ef4099c5c3c82fb952e4db909fefcf1b6 Mon Sep 17 00:00:00 2001 From: Brian Lee Date: Sun, 13 Aug 2023 08:54:05 -0700 Subject: [PATCH] Install nodejs, and use npm to install n for node version management. --- README.md | 15 +++++++++++---- defaults/main.yml | 3 +++ tasks/Debian.yml | 26 +++++++++++++++++++++----- tasks/main.yml | 14 +++++++++++--- vars/main.yml | 4 ++++ 5 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 defaults/main.yml diff --git a/README.md b/README.md index abebd3d..c3d35d8 100644 --- a/README.md +++ b/README.md @@ -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 ``` \ No newline at end of file diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..31ac946 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,3 @@ +--- +node_version: node_20.x +n_enabled: yes # https://github.com/tj/n \ No newline at end of file diff --git a/tasks/Debian.yml b/tasks/Debian.yml index 2af7473..f05d404 100644 --- a/tasks/Debian.yml +++ b/tasks/Debian.yml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml index d909ab5..4818c1c 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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 diff --git a/vars/main.yml b/vars/main.yml index 9412980..e3119f8 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -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 \ No newline at end of file