Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
krudi committed May 20, 2024
0 parents commit 0b2304c
Show file tree
Hide file tree
Showing 19 changed files with 363 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use_default_rules: true

skip_list:
- yaml
- role-name

exclude_paths:
- .cache
- .github
- requirements.yml
19 changes: 19 additions & 0 deletions .github/workflows/precheck-role-lint.yml
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions .github/workflows/precheck-role-molecule-test.yml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ansible files

*.retry

# Environment variables

*.env
!.env-*
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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: <https://github.com/nvm-sh/nvm#usage>.
## 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.
11 changes: 11 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -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
Empty file added files/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- name: Reload shell
ansible.builtin.command: /bin/bash -c "source ~/.bashrc"
changed_when: false
20 changes: 20 additions & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -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: []
28 changes: 28 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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"
14 changes: 14 additions & 0 deletions tasks/setup-dependencies.yml
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions tasks/setup-node.yml
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions tasks/setup-nvm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
- name: Install Node Version Manager

Check failure on line 2 in tasks/setup-nvm.yml

View workflow job for this annotation

GitHub Actions / Lint

command-instead-of-module

curl used in place of get_url or uri module

Check failure on line 2 in tasks/setup-nvm.yml

View workflow job for this annotation

GitHub Actions / Lint

risky-shell-pipe

Shells that use pipes should set the pipefail option.
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
Empty file added templates/.gitkeep
Empty file.
Empty file added tests/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
nvm_dependencies:
- apt-transport-https
- gnupg2
- curl

0 comments on commit 0b2304c

Please sign in to comment.