Ansible command module with nvm - linux

I am trying to execute this command using Ansible:
- name: install node v5.5.0
sudo: yes
shell: nvm ls-remote
environment:
http_proxy: http://17.99.193.229:3128
https_proxy: http://17.99.193.229:3128
I tried shell and command.
When using command: I got this error:
"[Errno 2] No such file or directory", "rc": 2"
When using shell: I got this error:
"fatal: [gocdagent-dev-01.rno.apple.com]: FAILED! => {"changed": true, "cmd": "nvm ls-remote", "delta": "0:00:00.016365", "end": "2016-05-16 18:26:07.259729", "failed": true, "rc": 127, "start": "2016-05-16 18:26:07.243364", "stderr": "/bin/sh: nvm: command not found", "stdout": "", "stdout_lines": [], "warnings": []}"
I can execute nvm command directly on the system. Why can't Ansible run nvm?

Nvm is not a binary, you have to load it first :
- name: install node v5.5.0
shell: . <NVM_DIR>/.nvm/nvm.sh && nvm ls-remote
environment:
http_proxy: http://17.99.193.229:3128
https_proxy: http://17.99.193.229:3128

Because you are running it as root (sudo: yes) in Ansible script. Make sure nvm is in root's PATH or give /full/path/to/nvm in shell command.

I chased my tail for hours on this one. Olivier Lecrivain headed me in the right direction. This solved it. This also gives you the ability to install multiple versions at the same time
- name: installing node versions using loop
become: yes
# Execute config files such as .profile (Ansible uses non-interactive login shells)
become_flags: -i
become_user: yourdesireduser
# $HOME will become yourdesireduser's home directory
shell: ". $HOME/.nvm/nvm.sh && nvm install {{item}}"
args:
chdir: "$HOME/.nvm/"
# not needed as it is created automatically through the nvm install command
# creates: "$HOME/.nvm/versions/node/v{{item}}"
loop:
- 14.18.3

Related

Can't change installed version via NVM for user

I am trying to install Node in eg. version 12.18.2 for user dev.
There is no issue when I'm doing it via command line.
Theoretically, I run the same commands from Ansible on a server and there is still the first version installed. I wonder how to make Ansible change version already installed on a server.
This is how I install nvm:
- name: Install nvm
ansible.builtin.shell: >
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.38.0/install.sh | bash
args:
executable: /bin/bash
chdir: "$HOME"
creates: "$HOME/.nvm/nvm.sh"
- name: Setup .profile
ansible.builtin.lineinfile:
path: ~/.profile
line: source ~/.nvm/nvm.sh
create: yes
- name: Install node
ansible.builtin.shell: |
source ~/.profile & nvm install {{ item }}
args:
executable: /bin/bash
chdir: "$HOME"
creates: "$HOME/.nvm/versions/node/v{{item}}"
loop:
- 12.18.2
When running this code first time, there is issue with /bin/bash: nvm: command not found, when running second time Ansible informs that it's skipping this installation step as it's already installed. I want user dev to use this installed version so I added this task that will use a specific version for a specific user:
- name: Change version
become_user: dev
ansible.builtin.shell: |
source ~/.profile & nvm use {{ item }}
args:
executable: /bin/bash
chdir: "$HOME"
creates: "$HOME/.nvm/versions/node/v{{item}}"
loop:
- 12.18.2
The output is the same as it's with install step:
ok: [my-server] => (item=12.18.2) => changed=false
ansible_loop_var: item
cmd: |-
source ~/.profile & nvm use 12.18.2
item: 12.18.2
rc: 0
stdout: skipped, since /home/dev/.nvm/versions/node/v12.18.2 exists
stdout_lines: <omitted>
When running the same command directly on a a server I get proper output:
dev#my-server:$ nvm use 12.18.2
Now using node v12.18.2 (npm v6.14.5)
Also the strange this is, when running install nvm step for other version like 12.18.1, I'm getting this:
TASK [nodejs : Install node] ***********************************************************************************************************************************************************************
task path: /server/nodejs/tasks/main.yml:120
Monday 05 December 2022 09:01:41 +0100 (0:00:00.870) 0:00:09.360 *******
failed: [my-server] (item=12.18.1) => changed=true
ansible_loop_var: item
cmd: |-
source ~/.profile & nvm install 12.18.1
delta: '0:00:00.452931'
end: '2022-12-05 09:01:43.287552'
item: 12.18.1
msg: non-zero return code
rc: 127
start: '2022-12-05 09:01:42.834621'
stderr: '/bin/bash: nvm: command not found'
stderr_lines: <omitted>
stdout: ''
stdout_lines: <omitted>
But I am sourcing .profile before installation source ~/.profile & nvm install {{ item }}
Of course when I use command line on a server there is no issue even without sourcing the .profile:
dev#my-server:$ nvm install 12.18.1
Downloading and installing node v12.18.1...
Downloading https://nodejs.org/dist/v12.18.1/node-v12.18.1-linux-x64.tar.xz...
############################################################################################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.18.1 (npm v6.14.5)
dev#my-server:$ node --version
v12.18.1
dev#my-server:$ nvm install 12.18.0
Downloading and installing node v12.18.0...
Downloading https://nodejs.org/dist/v12.18.0/node-v12.18.0-linux-x64.tar.xz...
############################################################################################################################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.18.0 (npm v6.14.4)
TLTR
When using nvm use Ansible skip step and inform it's already installed (Yes, it's installed, but I want to use it, not install it using nvm use).
When using nvm install for not installed version Ansible informs that /bin/bash: nvm: command not found even when sourcing ~/.profile before installation.
Doing the same via command line works perfectly. Can somebody help me to find what am I doing wrong via Ansible?
EDIT: Thanks #β.εηοιτ.βε for pointing that creates in Change version step that should be remove. Now looks like Ansible changed version, but not exactly:
TASK [nodejs : Change version] *********************************************************************
changed: [my-server] => (item=12.18.4) => changed=true
ansible_loop_var: item
cmd: |-
source ~/.profile && nvm use 12.18.4
delta: '0:00:01.658122'
end: '2022-12-05 09:40:57.817836'
item: 12.18.4
rc: 0
start: '2022-12-05 09:40:56.159714'
stderr: ''
stderr_lines: <omitted>
stdout: Now using node v12.18.4 (npm v6.14.6)
stdout_lines: <omitted>
TASK [nodejs : WHO] *********************************************************************
changed: [my-server] => changed=true
cmd: whoami
delta: '0:00:00.014623'
end: '2022-12-05 09:46:05.535453'
rc: 0
start: '2022-12-05 09:46:05.520830'
stderr: ''
stderr_lines: <omitted>
stdout: dev
stdout_lines: <omitted>
TASK [nodejs : debug] *********************************************************************
ok: [my-server] =>
msg: dev
TASK [nodejs : Check version] *********************************************************************
changed: [my-server] => changed=true
cmd: node --version
delta: '0:00:00.023858'
end: '2022-12-05 09:46:06.998710'
rc: 0
start: '2022-12-05 09:46:06.974852'
stderr: ''
stderr_lines: <omitted>
stdout: v12.18.2
stdout_lines: <omitted>
TASK [nodejs : debug] *********************************************************************
ok: [my-server] =>
msg: v12.18.2
The issue is that there is still the old version on a dev user when I'm checking. I'm pretty confused why Ansible thinks that version was changed for user dev and then when Ansible checks version for this user it's still the old one.

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
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
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
I will just post under here, because there are hundreds of these posts.
- name: Install node
become: true
become_user: root
shell: "source /root/.nvm/nvm.sh && nvm install {{ personal_node_version }} && nvm alias default {{ personal_node_version }}"
args:
executable: /bin/bash
worked for me.
This worked for me on Ubuntu 20.04 using nvm version 0.39.1:
- name: Install NVM
shell: >
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
args:
creates: "/root/.nvm/nvm.sh"
- name: Install Node Versions
shell: ". /root/.bashrc && nvm install {{item}}"
with_items:
- 'v10.24.1'
- 'v16.17.0'
- '--lts'
- 'node'
Based on all the posts found on stack and tweaked a little for my own needs - I found this solution worked perfectly for both installing NVM (the easy part) and creating a loop that allows you to insert 1 or many versions of Node as needed
# test if nvm has been installed by the user desired
- stat:
path: /home/yournonrootuser/.nvm
register: nvm_path
- name: Setup NodeVersionManager and install node version
become: yes
# Execute config files such as .profile (Ansible uses non-interactive login shells)
become_flags: -i
become_user: yournonrootuser
block:
- name: Install nvm
shell: >
curl -o- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
args:
executable: /bin/bash
chdir: "$HOME"
creates: "$HOME/.nvm/nvm.sh"
- name: Setup .profile of yournonrootuser
lineinfile:
path: ~/.profile
# This will make sure Node is on the users PATH
line: source ~/.nvm/nvm.sh
create: yes
become_flags: -i
when: nvm_path.stat.exists == false
# if we got here we already know node version manager is installed
- name: installing node versions using loop
command: sudo -iu yournonrootuser nvm install {{item}}
args:
executable: /bin/bash
chdir: "$HOME"
creates: "$HOME/.nvm/versions/node/v{{item}}"
loop:
- 14.18.3

bash on ubuntu on windows run npm command gives error "/usr/bin/env: node: No such file or directory"

When trying to install npm on "bash on ubuntu on windows", it installs, but every call to npm or whatever results in the error:
"/usr/bin/env: node: No such file or directory"
How to solve this?
This is often a misnaming error, if you install from a package manager you bin may be called nodejs so you just need to symlink it.
In "bash on ubuntu on windows", one has to run it with sudo:
sudo ln -s /usr/bin/nodejs /usr/bin/node
Originally found here: https://github.com/nodejs/node-v0.x-archive/issues/3911
(Thanks, digitalmediums)

Why is nvm command installed as root and also not found during vagrant bootstrap.sh?

When I try to install nvm and test that it's installed in my vagrant shell provisioning script, using:
sudo wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash
source ~/.bashrc
nvm
I get:
==> default: => Downloading nvm as script to '/root/.nvm'
==> default: => Appending source string to /root/.bashrc
==> default: => You can now install Node.js by running `nvm install`
==> default: /tmp/vagrant-shell: line 7: nvm: command not found
First I don't want it to install as root, I want to install it as the vagrant user in /home/vagrant to avoid permission issues.
Second why is the command not found when I ran source .bashrc as specified in the nvm installation instructions here https://github.com/creationix/nvm?
I have a solution which I'm adding now.
So first if you want to install somethnig as the vagrant user during a bootstrap.sh script, the easiest way to do that is to add another provisioner script to your Vagrantfile which is run in unprivileged mode:
config.vm.provision "ScriptRunAsRoot", type:"shell", path: "Vagrantdata/rootUserBootstrap.sh"
config.vm.provision "ScriptRunAsVagrantUser", privileged: false, type:"shell", path: "Vagrantdata/vagrantUserBootstrap.sh"
Then to get the nvm command in this situation, you have to be sure to run the nvm.sh script directly, ie not via the .bashrc file as stated in the nvm installation instructions. This is because .bashrc has a line which exits early if it is being run in a non interactive shell, which I guess vagrant is. From .bashrc:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
So instead of the source ~/.bashrc, you need:
source ~/.nvm/nvm.sh

npm global install on elastic beanstalk

I am running into an issue installing pm2 globally on aws elastic beanstalk. I created the following script for installing pm2:
option_settings:
- option_name: NODE_ENV
value: production
container_commands:
01_enable_rootaccess:
command: echo Defaults:root \!requiretty >> /etc/sudoers
02_install_imagemagic:
command: yum install -y ImageMagick
03_download_new_relic:
command: rpm -Uvh http://download.newrelic.com/pub/newrelic/el5/i386/newrelic-repo-5-3.noarch.rpm
ignoreErrors: true
04_install_new_relic:
command: yum install -y newrelic-sysmond
ignoreErrors: true
05_add_license_key:
command: /usr/sbin/nrsysmond-config --set license_key=xxxxxxx
ignoreErrors: true
06_start_new_relic:
command: /etc/init.d/newrelic-sysmond start
ignoreErrors: true
07_install_pm2:
command: sudo /opt/elasticbeanstalk/node-install/node-v0.10.26-linux-x64/bin/npm install pm2 -g
ignoreErrors: true
08_stop_old_pm2_processes:
command: sudo /opt/elasticbeanstalk/node-install/node-v0.10.26-linux-x64/bin/pm2 delete all
ignoreErrors: true
09_start_pm2:
command: sudo /opt/elasticbeanstalk/node-install/node-v0.10.26-linux-x64/bin/pm2 startup -u ec2-user
ignoreErrors: true
I have tried using just 'pm2 delete all' and 'pm2 startup' for commands 8 & 9 put i just get command not found. when i give the specific path to pm2(i logged on to the ec2 and verified) i get "line 4: exec: : not found". any idea what i am doing wrong here? Thanks in advance for your help!
I managed to install pm2 globally on elastic beanstalk with the following code snippet embedded in an .ebextensions/your_file_name.config file
container_commands:
01_node_symlink:
command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node"
02_npm_symlink:
command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm"
03_pm2_install:
command: "if [ ! -e /bin/pm2 ]; then npm install pm2 -g; fi"
ignoreErrors: true
You may need to ensure that the nodejs-legacy module is installed. If pm2 is depending on the executable named node, that will fail when the system installs it globally as nodejs as some Linux systems (Ubuntu, Debian) often do.

Resources