---
# tasks file for ndenv
- name: Run specified tasks on current platform
  include: "{{ ansible_os_family }}.yml"
- block:
  - name: Check whether ndenv is installed
    shell: "{{ ndenv_login_shell }} -lc 'ndenv versions'"
    register: result_ndenv_versions
    ignore_errors: yes
    changed_when: no
  - name: Install ndenv
    shell: "{{ ndenv_login_shell }} -lc 'anyenv install ndenv'"
    when: "result_ndenv_versions.rc != 0"
  - name: Install nodejs
    shell: "{{ ndenv_login_shell }} -lc 'ndenv install {{ item }}'"
    when: "item not in result_ndenv_versions.stdout"
    with_items: "{{ ndenv_node_versions }}"
  - name: Check default version
    shell: "{{ ndenv_login_shell }} -lc 'ndenv global'"
    register: result_ndenv_global
    changed_when: no
    when: "ndenv_global_version is defined"
  - name: Set default version
    shell: "{{ ndenv_login_shell }} -lc 'ndenv global {{ ndenv_global_version }}'"
    when: "ndenv_global_version is defined and ndenv_global_version not in result_ndenv_global.stdout"
  become: yes
  become_user: "{{ anyenv_owner }}"
  # Ref. https://github.com/ansible/ansible/pull/13424
  remote_user: "{{ anyenv_owner }}"

---
# tasks file for ndenv
- name: Run specified tasks on current platform
  include: "{{ ansible_os_family }}.yml"
- block:
  - name: Check whether ndenv is installed
    shell: "su -s {{ ndenv_login_shell }} -c 'ndenv versions' - {{ anyenv_owner }}"
    register: result_ndenv_versions
    ignore_errors: yes
    changed_when: no
  - name: Install ndenv
    shell: "su -s {{ ndenv_login_shell }} -c 'anyenv install ndenv' - {{ anyenv_owner }}"
    when: "result_ndenv_versions.rc != 0"
  - name: Install nodejs
    shell: "su -s {{ ndenv_login_shell }} -c 'ndenv install {{ item }}' - {{ anyenv_owner }}"
    when: "item not in result_ndenv_versions.stdout"
    with_items: "{{ ndenv_node_versions }}"
  - name: Check default version
    shell: "su -s {{ ndenv_login_shell }} -c 'ndenv global' - {{ anyenv_owner }}"
    register: result_ndenv_global
    changed_when: no
    when: "ndenv_global_version is defined"
  - name: Set default version
    shell: "su -s {{ ndenv_login_shell }} -c 'ndenv global {{ ndenv_global_version }}' - {{ anyenv_owner }}"
    when: "ndenv_global_version is defined and ndenv_global_version not in result_ndenv_global.stdout"
  become: yes

Refactor tasks to switch user without remote_user.
Refactor tasks to switch user without remote_user.

- In several cases, remote_user can't be overwritten in this role.
- Following warning occurs, but become_method doesn't work well on
  container.
  - When become_method=sudo(default), almost containers don't have sudo
    command.
  - When become_method=su, docker connection plugin of Ansible doesn't
    allow 'su' as become_method.

```
 [WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running su
```
