-
-
Save komuw/b3b5d24977d4df7bd549 to your computer and use it in GitHub Desktop.
#first seen here: http://www.snip2code.com/Snippet/63701/Ansible-task-to-install-nvm-and-node | |
# Here is how to install nvm and node in an Ansible task. | |
# I tried a bunch of different things, and as usual it's simple, but you have to get it right. | |
# The important part is that you have to shell with /bin/bash -c and source nvm.sh | |
--- | |
- name: Install nvm | |
shell: > | |
curl https://raw.githubusercontent.com/creationix/nvm/v0.7.0/install.sh | sh | |
creates=/home/{{ ansible_user_id }}/.nvm/nvm.sh | |
- name: Install node and set version | |
shell: > | |
/bin/bash -c "source ~/.nvm/nvm.sh && nvm install 0.10 && nvm alias default 0.10" | |
creates=/home/{{ ansible_user_id }}/.nvm/alias | |
You could use "args" to specify the /bin/bash executable to make this example a little bit more elegant.
http://docs.ansible.com/ansible/latest/shell_module.html
I installed Node like this:
- name: Install Node.js
shell: '. {{ ansible_env.HOME }}/.nvm/nvm.sh && nvm install node'
args:
creates: "{{ ansible_env.HOME }}/.nvm/versions/node"
chdir: "{{ ansible_env.HOME }}"
executable: /bin/bash
I'm trying to default node to version 9.11.1, but its not taking up.
any idea why its not happening_
- name: Install Node.js
shell: '. {{ ansible_env.HOME }}/.nvm/nvm.sh && nvm alias default 9.11.1'
args:
chdir: "{{ ansible_env.HOME }}"
executable: /bin/bash
Corrected:
its working, I had to close the terminal and open another one, or do source to see the changes.
Here's what works for me, and can be extended for more than one node version:
- name: "nvm"
shell: >
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
args:
executable: /bin/bash
chdir: "{{ ansible_env.HOME }}"
creates: "{{ ansible_env.HOME }}/.nvm/nvm.sh"
- name: "node"
shell: >
. {{ ansible_env.HOME }}/.nvm/nvm.sh && nvm install {{ item }}
args:
executable: /bin/bash
chdir: "{{ ansible_env.HOME }}"
creates: "{{ ansible_env.HOME }}/.nvm/versions/{{ item }}"
loop:
- node
+1 to this whole thread, thanks for the tips and dialog!
https://github.com/leonidas/ansible-nvm/blob/646c61d175e2b8c6c768bac6d80347ba3f0a960d/tasks/main.yml
- hosts: webservers
user: ec2-user
roles:- { role: ansibl-nvm, sudo: yes }
nvm:
user: ec2-user
version: v0.34.0
node_version: '6.9.0'
- { role: ansibl-nvm, sudo: yes }
Update:
- Updated paths for nvm v0.38
- Make it work with
become
- Setup
.profile
to make sure Node is on the user'sPATH
- name: Setup Node
become: yes
become_flags: -i # Execute config files such as .profile (Ansible uses non-interactive login shells)
become_user: runner
block:
- 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 # This will make sure Node is on the user's PATH
create: yes
- name: Install node
ansible.builtin.shell: |
nvm install {{item}}
args:
executable: /bin/bash
chdir: "$HOME"
creates: "$HOME/.nvm/versions/node/v{{item}}"
loop:
- 14.15.0
The last step: "Install node" is failing in my end with: /bin/bash: nvm: command not found
TASK [Install nvm] ***********************************************************************************************************************************************
[WARNING]: Consider using the get_url or uri module rather than running 'curl'. If you need to use command because get_url or uri is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.1.163]
TASK [Setup .profile] ********************************************************************************************************************************************
changed: [192.168.1.163]
TASK [Install node] **********************************************************************************************************************************************
failed: [192.168.1.163] (item=12) => {"ansible_loop_var": "item", "changed": true, "cmd": "nvm install 12\n", "delta": "0:00:00.010348", "end": "2021-05-15 05:12:29.384196", "item": 12, "msg": "non-zero return code", "rc": 127, "start": "2021-05-15 05:12:29.373848", "stderr": "/bin/bash: nvm: command not found", "stderr_lines": ["/bin/bash: nvm: command not found"], "stdout": "", "stdout_lines": []}
Above error will be fixed by changing:
nvm install {{item}}
with
source /root/.nvm/nvm.sh && nvm install {{item}}
Reference:
https://stackoverflow.com/a/53597273/4696051
The last step: "Install node" is failing in my end with: /bin/bash: nvm: command not found
@JeanCarlosChavarriaHughes, make sure your .profile
gets executed. For example, if there is a .bash_profile
, .profile
won't be sourced.
The last step: "Install node" is failing in my end with: /bin/bash: nvm: command not found
@JeanCarlosChavarriaHughes, make sure your
.profile
gets executed. For example, if there is a.bash_profile
,.profile
won't be sourced.
Don’t forget to define this: become_flags: -i
Update:
- Updated paths for nvm v0.38
- Make it work with
become
- Setup
.profile
to make sure Node is on the user'sPATH
- name: Setup Node become: yes become_flags: -i # Execute config files such as .profile (Ansible uses non-interactive login shells) become_user: runner block: - 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 # This will make sure Node is on the user's PATH create: yes - name: Install node ansible.builtin.shell: | nvm install {{item}} args: executable: /bin/bash chdir: "$HOME" creates: "$HOME/.nvm/versions/node/v{{item}}" loop: - 14.15.0
A lot of thanks works fine!
make sure your .profile gets executed. For example, if there is a .bash_profile, .profile won't be sourced.
That's not true, at least on ubuntu 20.04 LTS, bash_profile will source ~/.profile by default
cat ~/.bash_profile
$ cat ~/.bash_profile
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
cat ~/.profile
.....
source $HOME/.nvm/nvm.sh
even though, I still get nvm not found, my solution is bash -lc "nvm install "
even though, I still get nvm not found, my solution is bash -lc "nvm install "
None of the above solution work for me except this one.
Thanks
This is a combination of the posted solutions above that worked for to install a specific version for the current (non-root) user:
- name: Install nodejs
hosts: all
tasks:
- name: Install nvm
become: no
ansible.builtin.shell: >
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.1/install.sh | bash
args:
executable: /bin/bash
chdir: "$HOME"
creates: "$HOME/.nvm/nvm.sh"
- name: Install node
become: no
shell: >
. {{ ansible_env.HOME }}/.nvm/nvm.sh && nvm install {{ item }}
args:
executable: /bin/bash
chdir: "{{ ansible_env.HOME }}"
creates: "{{ ansible_env.HOME }}/.nvm/versions/{{ item }}"
loop:
- 14.20.0
@navmed This works great for non-root environments. Thanks for share! 👍
Cool! Thanks!