From 803ebb6bf2a7caa0e2c79e5f55b4477a1d24eeee Mon Sep 17 00:00:00 2001 From: Jesus Paz Date: Thu, 6 Feb 2025 20:58:06 -0500 Subject: [PATCH] Task to clean the environment Signed-off-by: Jesus Paz --- Makefile | 13 ---- README.md | 64 ++++++++++++------- defaults/main.yml | 1 + meta/main.yml | 24 +++---- playbook.yml | 8 ++- role.yml | 9 --- tasks/main.yml | 47 +++++++++++--- .../deb_nodesource_com_node.pref.2 | 5 -- templates/nodesource.list.j2 | 2 +- templates/preferences.d/nodejs.j2 | 2 +- tests/localhosts | 2 - tests/test_default.py | 12 ---- vars/main.yml | 3 +- 13 files changed, 101 insertions(+), 91 deletions(-) delete mode 100644 Makefile delete mode 100644 role.yml delete mode 100644 templates/etc/apt/preferences.d/deb_nodesource_com_node.pref.2 delete mode 100644 tests/localhosts delete mode 100644 tests/test_default.py diff --git a/Makefile b/Makefile deleted file mode 100644 index 48ceac1..0000000 --- a/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -# Makefile -venv: venv/bin/activate - -venv/bin/activate: requirements.txt - test -d venv || virtualenv -p `which python` venv - . venv/bin/activate; pip install -r requirements.txt - touch venv/bin/activate - -.PHONY:clean - -clean: - $(RM) -rf venv - find . -name "*.pyc" -exec $(RM) -rf {} \; diff --git a/README.md b/README.md index db24caa..5effffd 100644 --- a/README.md +++ b/README.md @@ -1,60 +1,76 @@ # ansible-nodejs-role -This is an Ansible role which adds the the NodeSource APT repository and installs Node.js. +This is an Ansible role that adds the NodeSource APT repository and installs Node.js. -Currently this role supports the following operating systems and releases. +## Supported Operating Systems -* **Ubuntu 14.04 LTS** (Trusty Tahr) -* **Ubuntu 16.04 LTS** (Xenial Xerus) +Currently, this role supports the following operating systems and releases: + +- **Ubuntu 20.04 LTS** (Focal Fossa) +- **Ubuntu 22.04 LTS** (Jammy Jellyfish) +- **Ubuntu 24.04 LTS** (Noble Numbat) +- **Debian 10** (Buster) +- **Debian 11** (Bullseye) +- **Debian 12** (Bookworm) ## Usage -You can either: +### Install and Run Locally + +To install and test this role locally, run: + +```sh +ANSIBLE_ROLES_PATH=../ ansible-playbook playbook.yml --ask-become-pass +``` + +```` + +### Install via Ansible Galaxy -* Install the playbook via Ansible Galaxy: +You can install this role directly from Ansible Galaxy: -```text -$ ansible-galaxy install nodesource.node +```sh +ansible-galaxy install nodesource.node ``` -* Install the using [requirements.yml via Ansible Galaxy](http://docs.ansible.com/ansible/galaxy.html#installing-multiple-roles-from-a-file): +Alternatively, use a `requirements.yml` file: -```yml +```yaml - src: https://github.com/nodesource/ansible-nodejs-role ``` -```text -$ ansible-galaxy install -r requirements.txt +Then install it using: + +```sh +ansible-galaxy install -r requirements.yml ``` -## Configure +## Configuration -Then configure it as follows: +Example playbook configuration: ```yaml - hosts: servers roles: - - nodesource.node + - nodesource.node ``` ## Role Variables - `nodejs_nodesource_pin_priority`: Pin-Priority of the NodeSource repository (default: `500`). -- `nodejs_version`: Set Node version (options: `0.10` or `0.12` or `4.6`, default: `4.6`) +- `nodejs_version`: Set the Node.js version (options: `18.x`, `20.x`, `22.x`, `23.x`, default: `22.x`). ## Testing -To test this role using [molecule](https://github.com/metacloud/molecule): - -``` -$ make -$ molecule test -``` +This role is automatically tested using GitHub Actions. To see how the CI/CD pipeline is configured, check the `.github/workflows/ci.yml` file. ## Author -Mark Wolfe +Originally developed by Mark Wolfe. +Maintained by NodeSource. ## License -This code is Copyright (c) 2014 NodeSource and Mark Wolfe and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included [LICENSE.md](./LICENSE.md) file for more details. +This code is licensed under the MIT License. See the included [LICENSE.md](./LICENSE.md) file for more details. + +```` diff --git a/defaults/main.yml b/defaults/main.yml index 4f5c514..476f82b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -10,3 +10,4 @@ nodejs_supported_versions: - "18.x" - "20.x" - "22.x" + - "23.x" diff --git a/meta/main.yml b/meta/main.yml index 157b0e0..a668a27 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -2,7 +2,7 @@ galaxy_info: author: NodeSource namespace: nodesource - role_name: ansible_nodejs_role + role_name: ansible-nodejs-role description: Installs the NodeSource Node.js binary packages company: NodeSource license: MIT @@ -31,15 +31,15 @@ galaxy_info: - name: GenericLinux versions: - all -categories: - - development - - networking - - packaging - - web - - system - - infrastructure - - performance - - security - - databases - - cloud + categories: + - development + - networking + - packaging + - web + - system + - infrastructure + - performance + - security + - databases + - cloud dependencies: [] diff --git a/playbook.yml b/playbook.yml index 07f879d..3478cc2 100644 --- a/playbook.yml +++ b/playbook.yml @@ -1,4 +1,10 @@ --- -- hosts: all +- name: Install Node.js using ansible-nodejs-role + hosts: localhost + connection: local + become: true roles: - role: ansible-nodejs-role + vars: + nodejs_version: "22.x" + nodejs_nodesource_pin_priority: 600 diff --git a/role.yml b/role.yml deleted file mode 100644 index eaa476f..0000000 --- a/role.yml +++ /dev/null @@ -1,9 +0,0 @@ ---- -- name: Install Node.js using ansible-nodejs-role - hosts: localhost - connection: local - become: yes - roles: - - role: ansible-nodejs-role - vars: - nodejs_version: "18.x" diff --git a/tasks/main.yml b/tasks/main.yml index a41549d..2dafc25 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -20,23 +20,39 @@ state: directory mode: "0755" -- name: Remove old NodeSource GPG key (if exists) - ansible.builtin.file: - path: /usr/share/keyrings/nodesource.gpg +- name: Uninstall existing Node.js and clean dependencies + ansible.builtin.apt: + name: nodejs state: absent + autoremove: yes + register: node_uninstall + changed_when: node_uninstall.changed -- name: Remove old NodeSource repository list (if exists) +- name: Remove old NodeSource files (if exist) ansible.builtin.file: - path: /etc/apt/sources.list.d/nodesource.list + path: "{{ item }}" state: absent + loop: + - /usr/share/keyrings/nodesource.gpg + - /etc/apt/sources.list.d/nodesource.list + - /etc/apt/preferences.d/nodejs -- name: Download and import NodeSource GPG key +- name: Download NodeSource GPG key ansible.builtin.get_url: url: https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key - dest: /usr/share/keyrings/nodesource.gpg + dest: /tmp/nodesource-repo.gpg + mode: "0644" + +- name: Convert GPG key to binary format and store in keyrings + ansible.builtin.command: + cmd: gpg --dearmor -o /usr/share/keyrings/nodesource.gpg /tmp/nodesource-repo.gpg + creates: /usr/share/keyrings/nodesource.gpg + become: yes + +- name: Set correct permissions for the GPG key + ansible.builtin.file: + path: /usr/share/keyrings/nodesource.gpg mode: "0644" - register: gpg_download - failed_when: gpg_download.failed - name: Detect system architecture ansible.builtin.command: dpkg --print-architecture @@ -69,3 +85,16 @@ ansible.builtin.apt: pkg: nodejs state: present + +- name: Verify Node.js installation + ansible.builtin.command: + cmd: node -v + register: node_version_output + changed_when: false + +- name: Validate installed Node.js version + ansible.builtin.assert: + that: + - "node_version_output.stdout is search(nodejs_version | regex_replace('x', ''))" + success_msg: "Node.js is correctly installed with version {{ node_version_output.stdout }}" + fail_msg: "Expected Node.js {{ nodejs_version }}, but found {{ node_version_output.stdout }}" diff --git a/templates/etc/apt/preferences.d/deb_nodesource_com_node.pref.2 b/templates/etc/apt/preferences.d/deb_nodesource_com_node.pref.2 deleted file mode 100644 index c845e44..0000000 --- a/templates/etc/apt/preferences.d/deb_nodesource_com_node.pref.2 +++ /dev/null @@ -1,5 +0,0 @@ -# {{ ansible_managed }} - -Package: * -Pin: release o=Node Source -Pin-Priority: {{ nodejs_nodesource_pin_priority }} diff --git a/templates/nodesource.list.j2 b/templates/nodesource.list.j2 index 57111fc..083dcb3 100644 --- a/templates/nodesource.list.j2 +++ b/templates/nodesource.list.j2 @@ -1 +1 @@ -deb [arch={{ system_arch.stdout }} signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_{{ nodejs_version }} {{ ansible_distribution_release }} main +deb [arch={{ system_arch.stdout }} signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_{{ nodejs_version }} nodistro main diff --git a/templates/preferences.d/nodejs.j2 b/templates/preferences.d/nodejs.j2 index 908ed47..119a3f0 100644 --- a/templates/preferences.d/nodejs.j2 +++ b/templates/preferences.d/nodejs.j2 @@ -1,3 +1,3 @@ Package: nodejs Pin: origin deb.nodesource.com -Pin-Priority: 600 +Pin-Priority: {{ nodejs_nodesource_pin_priority }} diff --git a/tests/localhosts b/tests/localhosts deleted file mode 100644 index 5f86e9c..0000000 --- a/tests/localhosts +++ /dev/null @@ -1,2 +0,0 @@ -[local] -localhost \ No newline at end of file diff --git a/tests/test_default.py b/tests/test_default.py deleted file mode 100644 index 26a8190..0000000 --- a/tests/test_default.py +++ /dev/null @@ -1,12 +0,0 @@ -import pytest -from testinfra.utils.ansible_runner import AnsibleRunner - -testinfra_hosts = AnsibleRunner('.molecule/ansible_inventory').get_hosts('all') - - -@pytest.mark.parametrize('f', ['/usr/bin/node', - '/usr/bin/npm']) -def test_etcd_installed(File, f): - file = File(f) - - assert file.exists diff --git a/vars/main.yml b/vars/main.yml index f352c6c..6422ced 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,3 +1,2 @@ --- -# Node.js version (override if needed) -nodejs_version: "22.x" +# vars file for nodejs