Skip to content

Commit d0858c0

Browse files
committed
Initial commit
0 parents  commit d0858c0

File tree

19 files changed

+383
-0
lines changed

19 files changed

+383
-0
lines changed

.ansible-lint

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use_default_rules: true
2+
3+
skip_list:
4+
- yaml
5+
- role-name
6+
7+
exclude_paths:
8+
- .cache
9+
- .github
10+
- requirements.yml
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
name: Code linting with ansible-lint
3+
4+
on:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
precheck-role-lint:
12+
name: Lint
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout the current branch
16+
uses: actions/checkout@main
17+
18+
- name: Run ansible-lint linting command
19+
uses: ansible/ansible-lint@main
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
name: Run role tests using Molecule
3+
4+
on:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
10+
schedule:
11+
- cron: "7 4 4 * *"
12+
13+
jobs:
14+
precheck-role-molecule-test:
15+
name: Build test instance
16+
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
include:
20+
- distro: ubuntu2404
21+
playbook: converge.yml
22+
- distro: ubuntu2204
23+
playbook: converge.yml
24+
25+
steps:
26+
- name: Checkout the current branch
27+
uses: actions/checkout@main
28+
29+
- name: Setup of Python 3
30+
uses: actions/setup-python@main
31+
with:
32+
python-version: "3.x"
33+
34+
- name: Install Ansible dependencies
35+
run: pip3 install ansible molecule molecule-plugins[docker] docker
36+
37+
- name: Run Molecule test commands
38+
run: molecule test
39+
env:
40+
MOLECULE_DISTRO: ${{ matrix.distro }}
41+
MOLECULE_PLAYBOOK: ${{ matrix.playbook }}
42+
PY_COLORS: "1"
43+
ANSIBLE_FORCE_COLOR: "1"

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Ansible files
2+
3+
*.retry
4+
5+
# Environment variables
6+
7+
*.env
8+
!.env-*

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) ansible-role-nvm
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ansible-role-nvm
2+
3+
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).
4+
5+
## Requirements
6+
7+
This role does not need any additional required packages.
8+
9+
## Quick start
10+
11+
1. First clone this repository and add into your project directory.
12+
2. Include the role in your [Ansible](https://github.com/ansible/ansible) playbook.
13+
14+
## Example playbook
15+
16+
Example use of a role, that will install the latest LTS version of [Node.js](https://github.com/nodejs).
17+
18+
```yml
19+
- hosts: all
20+
roles:
21+
- role: krudi.nvm
22+
# This option adds block code that loads the configuration from the Node Version Manager
23+
# 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**.
24+
nvm_nodejs_add_block: false
25+
26+
- role: krudi.nvm
27+
nodejs:
28+
install: true
29+
version: 22
30+
nvm:
31+
install: true
32+
add_block: true
33+
```
34+
35+
## Additional information
36+
37+
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.
38+
39+
More information can be found here: <https://github.com/nvm-sh/nvm#usage>.
40+
41+
## Issue
42+
43+
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.

defaults/main.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
nodejs:
3+
install: true
4+
version: 22
5+
6+
nvm:
7+
install: true
8+
script: https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh
9+
# This option adds block code that loads the configuration from the Node Version Manager.
10+
# 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
11+
add_block: false

files/.gitkeep

Whitespace-only changes.

handlers/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
- name: Reload shell
3+
ansible.builtin.command: /bin/bash -c "source ~/.bashrc"
4+
changed_when: false

meta/main.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
galaxy_info:
3+
role_name: nvm
4+
description: A role for Ansible that installs Node Version Manager, along with Node.js.
5+
license: MIT
6+
min_ansible_version: "2.9"
7+
author: krudi
8+
standalone: true
9+
platforms:
10+
- name: Ubuntu
11+
versions:
12+
- jammy
13+
- noble
14+
15+
galaxy_tags:
16+
- nodejs
17+
- node
18+
- nvm
19+
20+
dependencies: []

molecule/default/converge.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
- name: Converge
3+
hosts: all
4+
5+
pre_tasks:
6+
- name: Ensure APT package cache is updated
7+
ansible.builtin.apt:
8+
upgrade: yes
9+
update_cache: true
10+
cache_valid_time: 86400
11+
12+
- name: Ensure required packages are installed
13+
ansible.builtin.package:
14+
name: "{{ dependency }}"
15+
state: present
16+
loop: "{{ nvm_dependencies }}"
17+
loop_control:
18+
loop_var: dependency
19+
20+
roles:
21+
- role: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') | basename }}"
22+
nodejs:
23+
install: true
24+
version: 22
25+
nvm:
26+
install: true
27+
script: https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh
28+
add_block: false

molecule/default/molecule.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
driver:
3+
name: docker
4+
5+
dependency:
6+
name: galaxy
7+
options:
8+
ignore-errors: true
9+
ignore-certs: true
10+
11+
platforms:
12+
- name: instance-ubuntu2404
13+
image: "geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2404}-ansible:latest"
14+
command: ${MOLECULE_DOCKER_COMMAND:-""}
15+
volumes:
16+
- /sys/fs/cgroup:/sys/fs/cgroup:rw
17+
privileged: true
18+
pre_build_image: true
19+
cgroupns_mode: host
20+
21+
- name: instance-ubuntu2004
22+
image: "geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2004}-ansible:latest"
23+
command: ${MOLECULE_DOCKER_COMMAND:-""}
24+
volumes:
25+
- /sys/fs/cgroup:/sys/fs/cgroup:rw
26+
privileged: true
27+
pre_build_image: true
28+
cgroupns_mode: host
29+
30+
provisioner:
31+
name: ansible
32+
playbooks:
33+
converge: ${MOLECULE_PLAYBOOK:-converge.yml}
34+
35+
verifier:
36+
name: ansible
37+
38+
scenario:
39+
name: default
40+
test_sequence:
41+
- dependency
42+
- cleanup
43+
- destroy
44+
- syntax
45+
- create
46+
- prepare
47+
- converge
48+
- idempotence
49+
- side_effect
50+
- verify
51+
- cleanup
52+
- destroy

tasks/main.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
- name: Loading a task to install dependencies
3+
ansible.builtin.include_tasks: setup-dependencies.yml
4+
when: ansible_os_family == "Debian"
5+
6+
- name: Loading a task to install Node.js
7+
ansible.builtin.include_tasks: setup-node.yml
8+
when: ansible_os_family == "Debian"
9+
10+
- name: Loading a task to install Node Version Manager
11+
ansible.builtin.include_tasks: setup-nvm.yml
12+
when: ansible_os_family == "Debian"

tasks/setup-dependencies.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
- name: Ensure APT package cache is updated
3+
ansible.builtin.apt:
4+
update_cache: true
5+
cache_valid_time: 600
6+
7+
- name: Install required dependencies
8+
become: true
9+
ansible.builtin.package:
10+
name: "{{ dependency }}"
11+
state: present
12+
loop: "{{ nvm_dependencies }}"
13+
loop_control:
14+
loop_var: dependency

tasks/setup-node.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
- name: Ensure APT package cache is updated
3+
ansible.builtin.apt:
4+
update_cache: true
5+
cache_valid_time: 600
6+
7+
- name: Ensure the directory for storing APT keyrings exists and has the correct permissions
8+
ansible.builtin.file:
9+
path: /etc/apt/keyrings
10+
state: directory
11+
mode: "0755"
12+
when: nodejs.install | default(nodejs.install)
13+
14+
- name: Download the GPG key for Node.js from the specified URL to a temporary location
15+
ansible.builtin.get_url:
16+
url: https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key
17+
dest: /tmp/nodesource.gpg.key
18+
mode: "0644"
19+
when: nodejs.install | default(nodejs.install)
20+
21+
- name: Convert the downloaded Node.js GPG key to a format suitable for APT and add it to the keyring directory
22+
ansible.builtin.command: >
23+
gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg /tmp/nodesource.gpg.key
24+
args:
25+
creates: /etc/apt/keyrings/nodesource.gpg
26+
when: nodejs.install | default(nodejs.install)
27+
28+
- name: Ensure the Node.js GPG key in the keyring directory has the correct read permissions for all users
29+
ansible.builtin.file:
30+
path: /etc/apt/keyrings/nodesource.gpg
31+
mode: "0644"
32+
when: nodejs.install | default(nodejs.install)
33+
34+
- name: Add the Node.js repository to APT, allowing Node.js packages to be installed and updated from it
35+
ansible.builtin.apt_repository:
36+
repo: "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_{{ nodejs.version }}.x nodistro main"
37+
state: present
38+
when: nodejs.install | default(nodejs.install)
39+
40+
- name: Update the APT package list to include the latest information from all configured repositories, including Node.js
41+
ansible.builtin.apt:
42+
update_cache: yes
43+
when: nodejs.install | default(nodejs.install)
44+
45+
- name: Install the Node.js package using APT
46+
ansible.builtin.apt:
47+
name: nodejs
48+
state: present
49+
when: nodejs.install | default(nodejs.install)

tasks/setup-nvm.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
- name: Download NVM installation script
3+
ansible.builtin.get_url:
4+
url: "{{ nvm.script }}"
5+
dest: /tmp/install_nvm.sh
6+
mode: '0755'
7+
when: nvm.install | default(nvm.install)
8+
9+
- name: Install Node Version Manager
10+
ansible.builtin.shell: >
11+
/tmp/install_nvm.sh
12+
args:
13+
executable: /bin/bash
14+
creates: "~/.nvm/nvm.sh"
15+
when: nvm.install | default(nvm.install)
16+
17+
- name: Ensure .bashrc file exists in the user home directory
18+
ansible.builtin.file:
19+
path: "~/.bashrc"
20+
state: touch
21+
mode: "0644"
22+
register: bashrc_check
23+
24+
- name: Ensure .bashrc file has correct permissions
25+
ansible.builtin.file:
26+
path: "~/.bashrc"
27+
mode: "0644"
28+
when: bashrc_check.changed
29+
30+
- name: Check if Node Version Manager block is already present in .bashrc
31+
ansible.builtin.shell: grep -q 'export NVM_DIR="$HOME/.nvm"' ~/.bashrc
32+
register: nvm_block_present
33+
ignore_errors: true
34+
changed_when: false
35+
36+
- name: Add Node Version Manager initialization to .bashrc file for the user
37+
ansible.builtin.blockinfile:
38+
path: "~/.bashrc"
39+
block: |
40+
export NVM_DIR="$HOME/.nvm"
41+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
42+
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
43+
when: nvm.install | default(nvm.install) and nvm.add_block | default(nvm.add_block) and nvm_block_present.rc != 0
44+
notify: Reload shell

templates/.gitkeep

Whitespace-only changes.

tests/.gitkeep

Whitespace-only changes.

vars/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
nvm_dependencies:
3+
- apt-transport-https
4+
- gnupg2
5+
- curl

0 commit comments

Comments
 (0)