-
-
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 | |
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! 👍
@JeanCarlosChavarriaHughes, make sure your
.profile
gets executed. For example, if there is a.bash_profile
,.profile
won't be sourced.