Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible install node with nvm

I'm looking for a way to install a given version of node via ansible and nvm, the installation of nvm is working as expected because if I connect with the root user, I can execute the command nvm install 8.11.3 but this same command doesn't work with Ansible, I don't understand why.

---
- name: Install nvm
  git: repo=https://github.com/creationix/nvm.git dest=~/.nvm version=v0.33.11
  tags: nvm

- name: Source nvm in ~/.{{ item }}
  lineinfile: >
      dest=~/.{{ item }}
      line="source ~/.nvm/nvm.sh"
      create=yes
  tags: nvm
  with_items:
    - bashrc
    - profile

- name: Install node and set version
  become: yes
  become_user: root
  shell: nvm install 8.11.3
...

error log

TASK [node : Install node and set version] *************************************************************************************
    fatal: [51.15.128.164]: FAILED! => {"changed": true, "cmd": "nvm install 8.11.3", "delta": "0:00:00.005883", "end": "2018-12-03 15:05:10.394433", "msg": "non-zero return code", "rc": 127, "start": "2018-12-03 15:05:10.388550", "stderr": "/bin/sh: 1: nvm: not found", "stderr_lines": ["/bin/sh: 1: nvm: not found"], "stdout": "", "stdout_lines": []}
        to retry, use: --limit .../.../ansible/stater-debian/playbook.retry
like image 681
eth3rnit3 Avatar asked Dec 03 '18 15:12

eth3rnit3


People also ask

Does NVM install node?

Installing Node Version Manager. Node Version Manager, or nvm, allows you to install, update, and uninstall Node on your system, and also to manage multiple versions of Node that you can switch between.

Can I use yarn with NVM?

You can install Yarn through the Homebrew package manager. This will also install Node.js if it is not already installed. If you use nvm or similar, you should ensure that your PATH lists nvm's shims before the version of Node.js installed by Homebrew.

Can we install NVM using NPM?

Node Version Manager, more commonly called nvm, is the most popular way to install multiple versions of Node.js, but is only available for Mac/Linux and not supported on Windows. Instead, we recommend installing nvm-windows and then using it to install Node.js and Node Package Manager (npm).


2 Answers

It's okay, here's the configuration that works

- name: Install node and set version
  become: yes
  become_user: root
  shell: "source /root/.nvm/nvm.sh && nvm install 8.11.3" 
  args:
    executable: /bin/bash
like image 99
eth3rnit3 Avatar answered Sep 27 '22 21:09

eth3rnit3


I think the clue in the output you need is:

"/bin/sh: 1: nvm: not found"

To run a command without including the full path to that command (i.e. nvm rather than /the/dir/nvm/is/installed/in/nvm), then the directory that contains the command, must be in the $PATH environment variable for the shell that runs the command.

In this case it looks like that is not present for the shell that Ansible spawns, versus the shell your interactive commands run in. Change:

- name: Install node and set version
  become: yes
  become_user: root
  shell: nvm install 8.11.3

to

- name: Install node and set version
  become: yes
  become_user: root
  shell: /full/path/to/nvm install 8.11.3

If you don't know what to put in place of '/full/path/to', try either:

which nvm

or

find / -name nvm
like image 42
clockworknet Avatar answered Sep 27 '22 22:09

clockworknet