From 0b2304cbcfe25c0f2da511cda99cad8a7a373525 Mon Sep 17 00:00:00 2001 From: Patryk Kudlik Date: Mon, 20 May 2024 22:33:38 +0200 Subject: [PATCH] Initial commit --- .ansible-lint | 10 ++++ .github/workflows/precheck-role-lint.yml | 19 +++++++ .../workflows/precheck-role-molecule-test.yml | 43 +++++++++++++++ .gitignore | 8 +++ LICENSE | 21 ++++++++ README.md | 43 +++++++++++++++ defaults/main.yml | 11 ++++ files/.gitkeep | 0 handlers/main.yml | 4 ++ meta/main.yml | 20 +++++++ molecule/default/converge.yml | 28 ++++++++++ molecule/default/molecule.yml | 52 +++++++++++++++++++ tasks/main.yml | 12 +++++ tasks/setup-dependencies.yml | 14 +++++ tasks/setup-node.yml | 49 +++++++++++++++++ tasks/setup-nvm.yml | 24 +++++++++ templates/.gitkeep | 0 tests/.gitkeep | 0 vars/main.yml | 5 ++ 19 files changed, 363 insertions(+) create mode 100644 .ansible-lint create mode 100644 .github/workflows/precheck-role-lint.yml create mode 100644 .github/workflows/precheck-role-molecule-test.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 files/.gitkeep create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 molecule/default/converge.yml create mode 100644 molecule/default/molecule.yml create mode 100644 tasks/main.yml create mode 100644 tasks/setup-dependencies.yml create mode 100644 tasks/setup-node.yml create mode 100644 tasks/setup-nvm.yml create mode 100644 templates/.gitkeep create mode 100644 tests/.gitkeep create mode 100644 vars/main.yml diff --git a/.ansible-lint b/.ansible-lint new file mode 100644 index 0000000..fc0cee2 --- /dev/null +++ b/.ansible-lint @@ -0,0 +1,10 @@ +use_default_rules: true + +skip_list: + - yaml + - role-name + +exclude_paths: + - .cache + - .github + - requirements.yml diff --git a/.github/workflows/precheck-role-lint.yml b/.github/workflows/precheck-role-lint.yml new file mode 100644 index 0000000..88c1e48 --- /dev/null +++ b/.github/workflows/precheck-role-lint.yml @@ -0,0 +1,19 @@ +--- +name: Code linting with ansible-lint + +on: + pull_request: + push: + branches: + - main + +jobs: + precheck-role-lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Checkout the current branch + uses: actions/checkout@main + + - name: Run ansible-lint linting command + uses: ansible/ansible-lint@main diff --git a/.github/workflows/precheck-role-molecule-test.yml b/.github/workflows/precheck-role-molecule-test.yml new file mode 100644 index 0000000..224a68d --- /dev/null +++ b/.github/workflows/precheck-role-molecule-test.yml @@ -0,0 +1,43 @@ +--- +name: Run role tests using Molecule + +on: + pull_request: + push: + branches: + - main + + schedule: + - cron: "7 4 4 * *" + +jobs: + precheck-role-molecule-test: + name: Build test instance + runs-on: ubuntu-latest + strategy: + matrix: + include: + - distro: ubuntu2404 + playbook: converge.yml + - distro: ubuntu2204 + playbook: converge.yml + + steps: + - name: Checkout the current branch + uses: actions/checkout@main + + - name: Setup of Python 3 + uses: actions/setup-python@main + with: + python-version: "3.x" + + - name: Install Ansible dependencies + run: pip3 install ansible molecule molecule-plugins[docker] docker + + - name: Run Molecule test commands + run: molecule test + env: + MOLECULE_DISTRO: ${{ matrix.distro }} + MOLECULE_PLAYBOOK: ${{ matrix.playbook }} + PY_COLORS: "1" + ANSIBLE_FORCE_COLOR: "1" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4567083 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Ansible files + +*.retry + +# Environment variables + +*.env +!.env-* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7a1aaff --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) ansible-role-nvm + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e349930 --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +# ansible-role-nvm + +A role for [Ansible](https://github.com/ansible/ansible), that installs [Node Version Manager](https://github.com/nvm-sh/nvm), along with [Node.js](https://github.com/nodejs). + +## Requirements + +This role does not need any additional required packages. + +## Quick start + +1. First clone this repository and add into your project directory. +2. Include the role in your [Ansible](https://github.com/ansible/ansible) playbook. + +## Example playbook + +Example use of a role, that will install the latest LTS version of [Node.js](https://github.com/nodejs). + +```yml +- hosts: all + roles: + - role: krudi.nvm + # This option adds block code that loads the configuration from the Node Version Manager + # If you don't have any Node Version Manager loading from files such as **.bashrc**, **.zsh** or **.profile**, this option should be set to **true**. + nvm_nodejs_add_block: false + + - role: krudi.nvm + nodejs: + install: true + version: 22 + nvm: + install: true + add_block: true +``` + +## Additional information + +To change the version of Node Version Manager after installing this role, just use the official [Node Version Manager](https://github.com/nvm-sh/nvm) install command. + +More information can be found here: . + +## Issue + +Have you found a bug in this project or have a suggestion for a new feature? Create a new ticket for the bug or feature, which can be found on the [GitHub](https://github.com/krudi/ansible-role-nvm/issues) page. diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..86ec2cc --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,11 @@ +--- +nodejs: + install: true + version: 22 + +nvm: + install: true + script: https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh + # This option adds block code that loads the configuration from the Node Version Manager. + # If you don't have any Node Version Manager loading from files such as .bashrc, .zsh or .profile, this option should be set to true + add_block: false diff --git a/files/.gitkeep b/files/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..2031dd2 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,4 @@ +--- +- name: Reload shell + ansible.builtin.command: /bin/bash -c "source ~/.bashrc" + changed_when: false diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..775a280 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,20 @@ +--- +galaxy_info: + role_name: nvm + description: A role for Ansible that installs Node Version Manager, along with Node.js. + license: MIT + min_ansible_version: "2.9" + author: krudi + standalone: true + platforms: + - name: Ubuntu + versions: + - jammy + - noble + + galaxy_tags: + - nodejs + - node + - nvm + +dependencies: [] diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 0000000..82c51b4 --- /dev/null +++ b/molecule/default/converge.yml @@ -0,0 +1,28 @@ +--- +- name: Converge + hosts: all + + pre_tasks: + - name: Ensure APT package cache is updated + ansible.builtin.apt: + upgrade: yes + update_cache: true + cache_valid_time: 86400 + + - name: Ensure required packages are installed + ansible.builtin.package: + name: "{{ dependency }}" + state: present + loop: "{{ nvm_dependencies }}" + loop_control: + loop_var: dependency + + roles: + - role: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}" + nodejs: + install: true + version: 22 + nvm: + install: true + script: https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh + add_block: false diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..678ba5b --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,52 @@ +--- +driver: + name: docker + +dependency: + name: galaxy + options: + ignore-errors: true + ignore-certs: true + +platforms: + - name: instance-ubuntu2404 + image: "geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2404}-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + privileged: true + pre_build_image: true + cgroupns_mode: host + + - name: instance-ubuntu2004 + image: "geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2004}-ansible:latest" + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + privileged: true + pre_build_image: true + cgroupns_mode: host + +provisioner: + name: ansible + playbooks: + converge: ${MOLECULE_PLAYBOOK:-converge.yml} + +verifier: + name: ansible + +scenario: + name: default + test_sequence: + - dependency + - cleanup + - destroy + - syntax + - create + - prepare + - converge + - idempotence + - side_effect + - verify + - cleanup + - destroy diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..9c97cf1 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,12 @@ +--- +- name: Loading a task to install dependencies + ansible.builtin.include_tasks: setup-dependencies.yml + when: ansible_os_family == "Debian" + +- name: Loading a task to install Node.js + ansible.builtin.include_tasks: setup-node.yml + when: ansible_os_family == "Debian" + +- name: Loading a task to install Node Version Manager + ansible.builtin.include_tasks: setup-nvm.yml + when: ansible_os_family == "Debian" diff --git a/tasks/setup-dependencies.yml b/tasks/setup-dependencies.yml new file mode 100644 index 0000000..859bde8 --- /dev/null +++ b/tasks/setup-dependencies.yml @@ -0,0 +1,14 @@ +--- +- name: Ensure APT package cache is updated + ansible.builtin.apt: + update_cache: true + cache_valid_time: 600 + +- name: Install required dependencies + become: true + ansible.builtin.package: + name: "{{ dependency }}" + state: present + loop: "{{ nvm_dependencies }}" + loop_control: + loop_var: dependency diff --git a/tasks/setup-node.yml b/tasks/setup-node.yml new file mode 100644 index 0000000..5f19f03 --- /dev/null +++ b/tasks/setup-node.yml @@ -0,0 +1,49 @@ +--- +- name: Ensure APT package cache is updated + ansible.builtin.apt: + update_cache: true + cache_valid_time: 600 + +- name: Ensure the directory for storing APT keyrings exists and has the correct permissions + ansible.builtin.file: + path: /etc/apt/keyrings + state: directory + mode: "0755" + when: nodejs.install | default(nodejs.install) + +- name: Download the GPG key for Node.js from the specified URL to a temporary location + ansible.builtin.get_url: + url: https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key + dest: /tmp/nodesource.gpg.key + mode: "0644" + when: nodejs.install | default(nodejs.install) + +- name: Convert the downloaded Node.js GPG key to a format suitable for APT and add it to the keyring directory + ansible.builtin.command: > + gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg /tmp/nodesource.gpg.key + args: + creates: /etc/apt/keyrings/nodesource.gpg + when: nodejs.install | default(nodejs.install) + +- name: Ensure the Node.js GPG key in the keyring directory has the correct read permissions for all users + ansible.builtin.file: + path: /etc/apt/keyrings/nodesource.gpg + mode: "0644" + when: nodejs.install | default(nodejs.install) + +- name: Add the Node.js repository to APT, allowing Node.js packages to be installed and updated from it + ansible.builtin.apt_repository: + repo: "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_{{ nodejs.version }}.x nodistro main" + state: present + when: nodejs.install | default(nodejs.install) + +- name: Update the APT package list to include the latest information from all configured repositories, including Node.js + ansible.builtin.apt: + update_cache: yes + when: nodejs.install | default(nodejs.install) + +- name: Install the Node.js package using APT + ansible.builtin.apt: + name: nodejs + state: present + when: nodejs.install | default(nodejs.install) diff --git a/tasks/setup-nvm.yml b/tasks/setup-nvm.yml new file mode 100644 index 0000000..a457512 --- /dev/null +++ b/tasks/setup-nvm.yml @@ -0,0 +1,24 @@ +--- +- name: Install Node Version Manager + ansible.builtin.shell: > + curl -o- {{ nvm.script }} | bash + args: + executable: /bin/bash + creates: "~/.nvm/nvm.sh" + when: nvm.install | default(nvm.install) + +- name: Ensure .bashrc file exists in the user home directory + ansible.builtin.file: + path: "~/.bashrc" + state: touch + mode: "0755" + +- name: Add Node Version Manager initialization to .bashrc file for the user + ansible.builtin.blockinfile: + path: "~/.bashrc" + block: | + export NVM_DIR="/usr/local/nvm" + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" + when: nvm.install | default(nvm.install) and nvm.add_block | default(nvm.add_block) + notify: Reload shell diff --git a/templates/.gitkeep b/templates/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/.gitkeep b/tests/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..d34ad03 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,5 @@ +--- +nvm_dependencies: + - apt-transport-https + - gnupg2 + - curl