├── .gitignore ├── .ansible-lint ├── .github ├── FUNDING.yml └── workflows │ ├── release.yml │ ├── stale.yml │ └── ci.yml ├── .yamllint ├── templates └── npm.sh.j2 ├── molecule └── default │ ├── converge.yml │ ├── molecule.yml │ └── playbook-latest.yml ├── meta └── main.yml ├── tasks ├── setup-RedHat.yml ├── setup-Debian.yml └── main.yml ├── LICENSE ├── defaults └── main.yml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.retry 2 | */__pycache__ 3 | *.pyc 4 | .cache 5 | 6 | -------------------------------------------------------------------------------- /.ansible-lint: -------------------------------------------------------------------------------- 1 | skip_list: 2 | - 'yaml' 3 | - 'role-name' 4 | - 'fqcn-builtins' 5 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | --- 3 | github: geerlingguy 4 | patreon: geerlingguy 5 | -------------------------------------------------------------------------------- /.yamllint: -------------------------------------------------------------------------------- 1 | --- 2 | extends: default 3 | 4 | rules: 5 | line-length: 6 | max: 220 7 | level: warning 8 | 9 | ignore: | 10 | .github/workflows/stale.yml 11 | -------------------------------------------------------------------------------- /templates/npm.sh.j2: -------------------------------------------------------------------------------- 1 | export PATH=$PATH:{{ npm_config_prefix }}/bin 2 | export NPM_CONFIG_PREFIX={{ npm_config_prefix }} 3 | export NODE_PATH=$NODE_PATH:{{ npm_config_prefix }}/lib/node_modules 4 | -------------------------------------------------------------------------------- /molecule/default/converge.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Converge 3 | hosts: all 4 | #become: true 5 | 6 | vars: 7 | nodejs_install_npm_user: root 8 | npm_config_prefix: /root/.npm-global 9 | npm_config_unsafe_perm: "true" 10 | nodejs_npm_global_packages: 11 | - node-sass 12 | - name: jslint 13 | version: 0.12.0 14 | - name: yo 15 | 16 | pre_tasks: 17 | - name: Update apt cache. 18 | apt: update_cache=true cache_valid_time=600 19 | when: ansible_facts.os_family == 'Debian' 20 | 21 | roles: 22 | - role: geerlingguy.nodejs 23 | -------------------------------------------------------------------------------- /molecule/default/molecule.yml: -------------------------------------------------------------------------------- 1 | --- 2 | role_name_check: 1 3 | dependency: 4 | name: galaxy 5 | options: 6 | ignore-errors: true 7 | driver: 8 | name: docker 9 | platforms: 10 | - name: instance 11 | image: "geerlingguy/docker-${MOLECULE_DISTRO:-rockylinux9}-ansible:latest" 12 | command: ${MOLECULE_DOCKER_COMMAND:-""} 13 | volumes: 14 | - /sys/fs/cgroup:/sys/fs/cgroup:rw 15 | cgroupns_mode: host 16 | privileged: true 17 | pre_build_image: true 18 | provisioner: 19 | name: ansible 20 | playbooks: 21 | converge: ${MOLECULE_PLAYBOOK:-converge.yml} 22 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | dependencies: [] 3 | 4 | galaxy_info: 5 | role_name: nodejs 6 | author: geerlingguy 7 | description: Node.js installation for Linux 8 | company: "Midwestern Mac, LLC" 9 | license: "license (BSD, MIT)" 10 | min_ansible_version: 2.10 11 | platforms: 12 | - name: Debian 13 | versions: 14 | - all 15 | - name: Ubuntu 16 | versions: 17 | - trusty 18 | - xenial 19 | - bionic 20 | galaxy_tags: 21 | - development 22 | - web 23 | - javascript 24 | - js 25 | - node 26 | - npm 27 | - nodejs 28 | -------------------------------------------------------------------------------- /molecule/default/playbook-latest.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Converge 3 | hosts: all 4 | #become: true 5 | 6 | vars: 7 | nodejs_version: "16.x" 8 | nodejs_install_npm_user: root 9 | npm_config_prefix: /root/.npm-global 10 | npm_config_unsafe_perm: "true" 11 | nodejs_npm_global_packages: 12 | - slugify 13 | - name: jslint 14 | version: 0.12.0 15 | - name: yo 16 | 17 | pre_tasks: 18 | - name: Update apt cache. 19 | apt: update_cache=true cache_valid_time=600 20 | when: ansible_facts.os_family == 'Debian' 21 | 22 | roles: 23 | - role: geerlingguy.nodejs 24 | -------------------------------------------------------------------------------- /tasks/setup-RedHat.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Import Nodesource RPM key. 3 | rpm_key: 4 | key: https://rpm.nodesource.com/gpgkey/nodesource.gpg.key 5 | state: present 6 | 7 | - name: Add Nodesource repositories for Node.js. 8 | yum: 9 | name: "https://rpm.nodesource.com/pub_{{ nodejs_version }}/nodistro/repo/nodesource-release-nodistro-1.noarch.rpm" 10 | state: present 11 | when: ansible_facts.distribution_major_version | int >= 7 12 | register: node_repo 13 | 14 | - name: Update package cache if repo was added. 15 | yum: 16 | update_cache: true 17 | when: node_repo is changed 18 | tags: ['skip_ansible_lint'] 19 | 20 | - name: Ensure Node.js AppStream module is disabled (CentOS 8+). 21 | command: yum module disable -y nodejs 22 | register: module_disable 23 | changed_when: "'Nothing to do.' not in module_disable.stdout" 24 | when: ansible_facts.distribution_major_version | int >= 8 25 | 26 | - name: Ensure Node.js and npm are installed. 27 | yum: 28 | name: "nodejs-{{ nodejs_version | regex_replace('x', '') }}*" 29 | state: present 30 | enablerepo: nodesource 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Jeff Geerling 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | 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, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # This workflow requires a GALAXY_API_KEY secret present in the GitHub 3 | # repository or organization. 4 | # 5 | # See: https://github.com/marketplace/actions/publish-ansible-role-to-galaxy 6 | # See: https://github.com/ansible/galaxy/issues/46 7 | 8 | name: Release 9 | 'on': 10 | push: 11 | tags: 12 | - '*' 13 | 14 | defaults: 15 | run: 16 | working-directory: 'geerlingguy.nodejs' 17 | 18 | jobs: 19 | 20 | release: 21 | name: Release 22 | runs-on: ubuntu-latest 23 | steps: 24 | - name: Check out the codebase. 25 | uses: actions/checkout@v4 26 | with: 27 | path: 'geerlingguy.nodejs' 28 | 29 | - name: Set up Python 3. 30 | uses: actions/setup-python@v5 31 | with: 32 | python-version: '3.13' # Can't go to 3.14+ until Ansible 13.x 33 | 34 | - name: Install Ansible. 35 | run: pip3 install ansible-core 36 | 37 | - name: Trigger a new import on Galaxy. 38 | run: >- 39 | ansible-galaxy role import --api-key ${{ secrets.GALAXY_API_KEY }} 40 | $(echo ${{ github.repository }} | cut -d/ -f1) $(echo ${{ github.repository }} | cut -d/ -f2) 41 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Set the version of Node.js to install ("12.x", "13.x", "14.x", "15.x", etc.). 3 | # Version numbers from Nodesource: https://github.com/nodesource/distributions 4 | nodejs_version: "16.x" 5 | 6 | # The user for whom the npm packages will be installed. 7 | # nodejs_install_npm_user: username 8 | 9 | # The directory for global installations. 10 | npm_config_prefix: "/usr/local/lib/npm" 11 | 12 | # Set to true to suppress the UID/GID switching when running package scripts. If 13 | # set explicitly to false, then installing as a non-root user will fail. 14 | npm_config_unsafe_perm: false 15 | 16 | # Define a list of global packages to be installed with NPM. 17 | nodejs_npm_global_packages: [] 18 | # # Install a specific version of a package. 19 | # - name: jslint 20 | # version: 0.9.3 21 | # # Install the latest stable release of a package. 22 | # - name: node-sass 23 | # # This shorthand syntax also works (same as previous example). 24 | # - node-sass 25 | 26 | # The path of a package.json file used to install packages globally. 27 | nodejs_package_json_path: "" 28 | 29 | # Whether or not /etc/profile.d/npm.sh (globa) must be generated. 30 | # Set to false if you need to handle this manually with a per-user install. 31 | nodejs_generate_etc_profile: true 32 | -------------------------------------------------------------------------------- /tasks/setup-Debian.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Ensure dependencies are present. 3 | ansible.builtin.apt: 4 | name: 5 | - apt-transport-https 6 | - python3-debian 7 | - gnupg2 8 | state: present 9 | 10 | - name: Download NodeSource's signing key. 11 | # NodeSource's web server discriminates the User-Agent used by the deb822_repository module. 12 | # https://github.com/nodesource/distributions/issues/1723 13 | ansible.builtin.get_url: 14 | url: https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key 15 | dest: /etc/apt/signing-key-nodesource-repo.asc 16 | owner: root 17 | group: root 18 | mode: '0444' 19 | register: node_signing_key 20 | 21 | - name: Add NodeSource repositories for Node.js. 22 | ansible.builtin.deb822_repository: 23 | name: nodesource_{{ nodejs_version }} 24 | uris: "https://deb.nodesource.com/node_{{ nodejs_version }}" 25 | types: deb 26 | suites: nodistro 27 | components: main 28 | signed_by: "{{ node_signing_key.dest }}" 29 | state: present 30 | register: node_repo 31 | 32 | - name: Update apt cache if repo was added. 33 | ansible.builtin.apt: update_cache=yes 34 | when: node_repo is changed 35 | tags: ['skip_ansible_lint'] 36 | 37 | - name: Ensure Node.js and npm are installed. 38 | ansible.builtin.apt: 39 | name: "nodejs={{ nodejs_version | regex_replace('x', '') }}*" 40 | state: present 41 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - import_tasks: setup-RedHat.yml 3 | when: ansible_facts.os_family == 'RedHat' 4 | 5 | - import_tasks: setup-Debian.yml 6 | when: ansible_facts.os_family == 'Debian' 7 | 8 | - name: Define nodejs_install_npm_user 9 | set_fact: 10 | nodejs_install_npm_user: "{{ ansible_user | default(lookup('env', 'USER')) }}" 11 | when: nodejs_install_npm_user is not defined 12 | 13 | - name: Create npm global directory 14 | file: 15 | path: "{{ npm_config_prefix }}" 16 | owner: "{{ nodejs_install_npm_user }}" 17 | group: "{{ nodejs_install_npm_user }}" 18 | state: directory 19 | mode: 0755 20 | 21 | - name: Add npm_config_prefix bin directory to global $PATH. 22 | template: 23 | src: npm.sh.j2 24 | dest: /etc/profile.d/npm.sh 25 | mode: 0644 26 | when: nodejs_generate_etc_profile 27 | 28 | - name: Ensure npm global packages are installed. 29 | npm: 30 | name: "{{ item.name | default(item) }}" 31 | version: "{{ item.version | default(omit) }}" 32 | global: true 33 | state: "{{ item.state | default('present') }}" 34 | environment: 35 | NPM_CONFIG_PREFIX: "{{ npm_config_prefix }}" 36 | NODE_PATH: "{{ npm_config_prefix }}/lib/node_modules" 37 | NPM_CONFIG_UNSAFE_PERM: "{{ npm_config_unsafe_perm }}" 38 | with_items: "{{ nodejs_npm_global_packages }}" 39 | 40 | - name: Install packages defined in a given package.json. 41 | npm: 42 | path: "{{ nodejs_package_json_path }}" 43 | when: nodejs_package_json_path is defined and nodejs_package_json_path != "" 44 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Close inactive issues 3 | 'on': 4 | schedule: 5 | - cron: "55 16 * * 6" # semi-random time 6 | 7 | jobs: 8 | close-issues: 9 | runs-on: ubuntu-latest 10 | permissions: 11 | issues: write 12 | pull-requests: write 13 | steps: 14 | - uses: actions/stale@v8 15 | with: 16 | days-before-stale: 120 17 | days-before-close: 60 18 | exempt-issue-labels: bug,pinned,security,planned 19 | exempt-pr-labels: bug,pinned,security,planned 20 | stale-issue-label: "stale" 21 | stale-pr-label: "stale" 22 | stale-issue-message: | 23 | This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! 24 | 25 | Please read [this blog post](https://www.jeffgeerling.com/blog/2020/enabling-stale-issue-bot-on-my-github-repositories) to see the reasons why I mark issues as stale. 26 | close-issue-message: | 27 | This issue has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. 28 | stale-pr-message: | 29 | This pr has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution! 30 | 31 | Please read [this blog post](https://www.jeffgeerling.com/blog/2020/enabling-stale-issue-bot-on-my-github-repositories) to see the reasons why I mark issues as stale. 32 | close-pr-message: | 33 | This pr has been closed due to inactivity. If you feel this is in error, please reopen the issue or file a new issue with the relevant details. 34 | repo-token: ${{ secrets.GITHUB_TOKEN }} 35 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: CI 3 | 'on': 4 | pull_request: 5 | push: 6 | branches: 7 | - master 8 | schedule: 9 | - cron: "0 7 * * 3" 10 | 11 | defaults: 12 | run: 13 | working-directory: 'geerlingguy.nodejs' 14 | 15 | jobs: 16 | 17 | lint: 18 | name: Lint 19 | runs-on: ubuntu-latest 20 | steps: 21 | - name: Check out the codebase. 22 | uses: actions/checkout@v4 23 | with: 24 | path: 'geerlingguy.nodejs' 25 | 26 | - name: Set up Python 3. 27 | uses: actions/setup-python@v5 28 | with: 29 | python-version: '3.13' # Can't go to 3.14+ until Ansible 13.x 30 | 31 | - name: Install test dependencies. 32 | run: pip3 install yamllint 33 | 34 | - name: Lint code. 35 | run: | 36 | yamllint . 37 | 38 | molecule: 39 | name: Molecule 40 | runs-on: ubuntu-latest 41 | strategy: 42 | matrix: 43 | include: 44 | # Was getting 'Hash algorithm SHA1 not available.' 45 | # - distro: rockylinux9 46 | # playbook: converge.yml 47 | - distro: ubuntu2204 48 | playbook: converge.yml 49 | - distro: debian12 50 | playbook: converge.yml 51 | - distro: debian11 52 | playbook: converge.yml 53 | 54 | - distro: debian12 55 | playbook: playbook-latest.yml 56 | 57 | steps: 58 | - name: Check out the codebase. 59 | uses: actions/checkout@v4 60 | with: 61 | path: 'geerlingguy.nodejs' 62 | 63 | - name: Set up Python 3. 64 | uses: actions/setup-python@v5 65 | with: 66 | python-version: '3.13' # Can't go to 3.14+ until Ansible 13.x 67 | 68 | - name: Install test dependencies. 69 | run: pip3 install ansible molecule molecule-plugins[docker] docker 70 | 71 | - name: Run Molecule tests. 72 | run: molecule test 73 | env: 74 | PY_COLORS: '1' 75 | ANSIBLE_FORCE_COLOR: '1' 76 | MOLECULE_DISTRO: ${{ matrix.distro }} 77 | MOLECULE_PLAYBOOK: ${{ matrix.playbook }} 78 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Role: Node.js 2 | 3 | [![CI](https://github.com/geerlingguy/ansible-role-nodejs/actions/workflows/ci.yml/badge.svg)](https://github.com/geerlingguy/ansible-role-nodejs/actions/workflows/ci.yml) 4 | 5 | Installs Node.js on RHEL/CentOS or Debian/Ubuntu. 6 | 7 | ## Requirements 8 | 9 | None. 10 | 11 | ## Role Variables 12 | 13 | Available variables are listed below, along with default values (see `defaults/main.yml`): 14 | 15 | ```yaml 16 | nodejs_version: "16.x" 17 | ``` 18 | 19 | The Node.js version to install. "14.x" is the default and works on most supported OSes. Other versions such as "10.x", "14.x", "18.x", etc. should work on the latest versions of Debian/Ubuntu and RHEL/CentOS. 20 | 21 | ```yaml 22 | nodejs_install_npm_user: "{{ ansible_ssh_user }}" 23 | ``` 24 | 25 | The user for whom the npm packages will be installed can be set here, this defaults to `ansible_user`. 26 | 27 | ```yaml 28 | npm_config_prefix: "/usr/local/lib/npm" 29 | ``` 30 | 31 | The global installation directory. This should be writeable by the `nodejs_install_npm_user`. 32 | 33 | ```yaml 34 | npm_config_unsafe_perm: "false" 35 | ``` 36 | 37 | Set to true to suppress the UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail. 38 | 39 | ```yaml 40 | nodejs_npm_global_packages: [] 41 | ``` 42 | 43 | A list of npm packages with a `name` and (optional) `version` to be installed globally. For example: 44 | 45 | ```yaml 46 | nodejs_npm_global_packages: 47 | # Install a specific version of a package. 48 | - name: jslint 49 | version: 0.9.3 50 | # Install the latest stable release of a package. 51 | - name: node-sass 52 | # This shorthand syntax also works (same as previous example). 53 | - node-sass 54 | # Remove a package by setting state to 'absent'. 55 | - name: node-sass 56 | state: absent 57 | ``` 58 | 59 | ```yaml 60 | nodejs_package_json_path: "" 61 | ``` 62 | 63 | Set a path pointing to a particular `package.json` (e.g. `"/var/www/app/package.json"`). This will install all of the defined packages globally using Ansible's `npm` module. 64 | 65 | ```yaml 66 | nodejs_generate_etc_profile: true 67 | ``` 68 | 69 | By default the role will create `/etc/profile.d/npm.sh` with exported variables (`PATH`, `NPM_CONFIG_PREFIX`, `NODE_PATH`). If you prefer to avoid generating that file (e.g. you want to set the variables yourself for a non-global install), set it to "false". 70 | 71 | ## Dependencies 72 | 73 | None. 74 | 75 | ## Example Playbook 76 | 77 | ```yaml 78 | - hosts: utility 79 | vars_files: 80 | - vars/main.yml 81 | roles: 82 | - geerlingguy.nodejs 83 | ``` 84 | 85 | *Inside `vars/main.yml`*: 86 | 87 | ```yaml 88 | nodejs_npm_global_packages: 89 | - name: jslint 90 | - name: node-sass 91 | ``` 92 | 93 | ## License 94 | 95 | MIT / BSD 96 | 97 | ## Author Information 98 | 99 | This role was created in 2014 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/). 100 | --------------------------------------------------------------------------------