--- - name: clone nodenv git: repo: https://github.com/nodenv/nodenv.git dest: "{{nodenv_directory}}" depth: 1 register: cloned_nodenv - name: setup nodenv lineinfile: dest: ~{{nodenv_user}}/.profile line: '{{item}}' with_items: - 'export PATH="{{nodenv_directory}}/bin:$PATH"' - 'eval "$(nodenv init -)"' - name: setup node-build git: repo: https://github.com/nodenv/node-build.git dest: "{{nodenv_directory}}/plugins/node-build" depth: 1 register: cloned_node_build # do not run this as a handler but as a task here because we will need this before # end of playbook execution. - name: change ownership of nodenv file: path: "{{nodenv_directory}}" owner: '{{nodenv_user}}' group: '{{nodenv_group}}' recurse: yes when: cloned_node_build.changed or cloned_nodenv.changed - name: get installed node versions command: nodenv versions register: nodenv_versions changed_when: False failed_when: False become: yes become_user: '{{nodenv_user}}' become_method: su environment: PATH: "{{nodenv_directory}}/bin:{{ ansible_env.PATH }}" - name: install node command: nodenv install {{node_version}} when: node_version not in nodenv_versions.stdout async: 3600 poll: 5 become: yes become_user: '{{nodenv_user}}' become_method: su environment: PATH: "{{nodenv_directory}}/bin:{{ ansible_env.PATH }}" # no need to nodenv rehash here since su -c will re-source .profile anyways - name: get selected nodenv node version command: nodenv version register: nodenv_version changed_when: False become: yes become_user: '{{nodenv_user}}' become_method: su environment: PATH: "{{nodenv_directory}}/bin:{{ ansible_env.PATH }}" - name: select node version command: nodenv global {{node_version}} when: node_version not in nodenv_version.stdout become: yes become_user: '{{nodenv_user}}' become_method: su environment: PATH: "{{nodenv_directory}}/bin:{{ ansible_env.PATH }}"