morgangraphics.ansible-role-nvm

Ansible Role: NVM

This Ansible role installs NVM (Node Version Manager) and Node.js on Debian/Ubuntu, RHEL/CentOS, and other Unix-based systems.

Challenges with NVM and Ansible

Using NVM with Ansible can be tricky due to issues with SSH and shell types. For tips on handling these issues, you can check out this Stack Overflow post.

Limitations of Other Roles

Many other Ansible roles for installing NVM and Node.js have shortcomings:

  1. They use package managers like apt-get or yum, which may install older versions of Node.js that aren't LTS (Long Term Support) or compatible if you want multiple Node.js versions.

  2. They often install NVM and Node.js as the root user, which can create permission issues and security risks.

  3. They do not allow running NVM, npm, node, or shell commands on the fly.

Advantages of This Role

  1. You can install NVM using wget, curl, or git.
  2. You can use NVM just like you would in your command line within your Ansible tasks.
  3. You can install any specific versions of Node.js that you want.
  4. NVM and Node.js are not installed as root.
  5. You can run any nvm, npm, node, bash, or shell commands without needing a separate role for Node.js.

Requirements

  • Ansible version (ansible-core) 2.16.0 or newer.

Note: If you need to use an older version of Ansible, check out the legacy 1.5.X branch.

Installation Steps

  1. Clone this repository into your roles folder.
  2. Set the roles_path variable in your ansible.cfg file to point to your roles folder (e.g., roles_path = ../ansible-roles/).
  3. Include this role in your playbook.

:warning: IMPORTANT!

DO NOT RUN THIS ROLE AS ROOT! (Avoid using become: true)

Reasons to avoid running as root:

  1. It's a security risk; you usually don't need all tasks to run as the root user. If you do need to run everything as root, reconsider the role's requirements.
  2. This role installs nvm in the same environment where Node.js will run, which is not as root.
  3. If you run as root, NVM will be installed in the root's home directory, and it won't work as expected for other users.

Example of Bad Usage:

- hosts: all
  become: true

  roles:
    - role: ansible-role-nvm
      nodejs_version: "8.16.0"
      nvm_commands:
       - "nvm exec default npm install"

Example of Good Usage:

- hosts: all

  roles:
    - role: ansible-role-nvm
      nodejs_version: "8.16.0"
      nvm_commands:
       - "nvm exec default npm install"

    - role: another-role
      become: true

Example of Best Usage:

- hosts: all

  roles:
    - role: ansible-role-nvm
      nodejs_version: "8.16.0"
      nvm_commands:
       - "nvm exec default npm install"
      become: true
      become_user: ec2-user

Example Playbooks

Super Simple

This will install the latest LTS version of Node.js:

- hosts: all

  roles:
    - role: ansible-role-nvm

Simple

Install a specific version of Node.js:

- hosts: all

  roles:
    - role: ansible-role-nvm
      nodejs_version: "8.15.0"

More Complex

Set up different environments (Dev/Prod) with specific options:

- hosts: dev
  vars_files:
    - vars/dev.yml

  roles:
    - role: ansible-role-nvm
      nodejs_version: "{{ config.dev.nodejs.version }}"

- hosts: prod
  vars_files:
    - vars/prod.yml

  roles:
    - role: ansible-role-nvm
      nvm_commands:
       - "nvm install {{ config.prod.client-1.nodejs.version }}"
       - "npm install"

Managing Multiple Node.js Versions

By default, the first version of Node.js you install will be the "default" version. You can manage this with aliases like default and system.

Example Installations

- hosts: host-1

  roles:
    - role: ansible-role-nvm
      nodejs_version: "8.15.0"

    - role: ansible-role-nvm
      nodejs_version: "10.15.0"

Alias Example

- hosts: host-2

  roles:
    - role: ansible-role-nvm
      nodejs_version: "8.15.0"

    - role: ansible-role-nvm
      default: true
      nodejs_version: "10.15.0"

NVM Commands

You can use NVM commands to run scripts or manage your application directly within the Ansible role.

Notes on Using NVM

  • nvm run is similar to node server.js.
  • nvm exec is like running npm commands.

You can place any command related to your application within the nvm_commands section.

Caveats

  1. The first version installed will always be the default. Use default: true for any version you want to set as default later.
  2. If you set default: true and provide an alias later, the first will always be executed first.

Issues

"nvm: command not found"

This usually happens if NVM is installed for a different user context. Ensure you run the role under the right user.

"cannot find /usr/bin/python"

If you're using an OS with Python 3 as default, specify the Ansible Python interpreter.

glibc_2.28' not found

This occurs when trying to run an unsupported Node.js version on your OS. Upgrade your OS or choose a compatible Node.js version.

Ansible Version Support

  • Supports ansible-core 2.16 and above: Major changes in how Ansible handles includes.
  • Use the legacy branch for 2.15 and below.

Role Variables

  • nodejs_version: Specify the Node.js version (defaults to "lts").
  • autocomplete: Enable NVM bash autocomplete.
  • clean_install: Force a clean NVM installation.
  • default: Set a Node.js version as default when handling multiple versions.
  • nvm_commands: List of commands to run with NVM.
  • nvm_install: Method for installing NVM (wget, curl, git).
  • nvm_dir: Custom directory for NVM installation.
  • nvm_profile: The shell profile to use for NVM (e.g., .bashrc).

License

MIT / BSD

Author Information

This role is developed by MORGANGRAPHICS, INC and is inspired by Jeff Geerling's Node.js role from his book, Ansible for DevOps.

Installa
ansible-galaxy install morgangraphics.ansible-role-nvm
Licenza
mit
Download
476
Proprietario