Issue 1:
I'm reissueing this duplicate of ansible/ansible-modules-extras#3442 here as repository ansible-modules-extras had been locked.

ISSUE TYPE

Bug Report

COMPONENT NAME

npm module

ANSIBLE VERSION

ansible 2.1.1.0

CONFIGURATION

vars:
node_branch: '4.x'
node_version: '4.6.2-1nodesource1~trusty1'

tasks:
- name: node | Register NodeSource signing key
become: true
apt_key: url=https://deb.nodesource.com/gpgkey/nodesource.gpg.key state=present

- name: node | Add NodeSource repository
become: true
apt_repository: repo='{{item}}' state=present
with_items:
- deb https://deb.nodesource.com/node_{{node_branch}} trusty main
- deb-src https://deb.nodesource.com/node_{{node_branch}} trusty main

- name: node | Install Node.js
become: true
apt: name='nodejs={{node_version}}' update_cache=yes state=present

- name: node | Install pm2
become: true
npm: name=pm2 global=yes version=2.1.4

OS / ENVIRONMENT

nodejs v4.6.2 LTS shipping with npm v2.5.11
Mac OS X 10.11.6 (local)
Ubuntu 14.04 LTS (provisioning target)

SUMMARY

If a module is installed globally the ownership of npm's cache (the .npm folder) after provisioning is not correct which causes subsequent errors and strange behaviors due to wrong permissions on that folder.

After provisioning with Ansible's npm module the npm cache is populated at /root/.npm but with ownership deploy:deploy where deploy is the user that is used for SSH-login on the remote machine.

As global installation of npm modules requires root privileges become: true is set and causes invocation via sudo. I found out that in these cases Ansible invokes npm via sudo -H ... which causes $HOME to be changed to /root. This might cause population of .npm folder at $HOME/.npm == /root/.npm but with ownership of the user defined by $SUDO_USER which is still deploy.

Unfortunately there is no proper documentation on how npm handles installs via sudo, so this is only a guess from my side.

Nevertheless this can also be reproduced manually via deploy:~$ sudo -H npm install -g pm2 which causes the same behavior. If the flag -H is omitted everything is fine as the .npm folder gets populated at /home/deploy/.npm with ownership deploy:deploy but I do not see any configuration option for Ansible to influence the parameters for the sudo-invocation.

STEPS TO REPRODUCE

see above

EXPECTED RESULTS

npm's cache should be populated at /home/deploy/.npm with ownership deploy:deploy.

ACTUAL RESULTS

npm's cache is populated at /root/.npm with ownership deploy:deploy.

root:~# ls -la
-rw------- 1 root root 6893 Nov 11 07:26 .bash_history
-rw-r--r-- 1 root root 3106 Feb 20 2014 .bashrc
drwxr-xr-x 3 deploy deploy 4096 Nov 10 21:01 .npm/
-rw-r--r-- 1 root root 141 Nov 7 19:55 .profile
drwx------ 2 root root 4096 Jul 29 10:00 .ssh/
-rw------- 1 root root 3787 Nov 9 20:42 .viminfo

Issue 2:
From @akloeber on 2016-11-11T19:19:21Z

ISSUE TYPE

Bug Report

COMPONENT NAME

npm module

ANSIBLE VERSION

ansible 2.1.1.0

CONFIGURATION

vars:
node_branch: '4.x'
node_version: '4.6.2-1nodesource1~trusty1'

tasks:
- name: node | Register NodeSource signing key
become: true
apt_key: url=https://deb.nodesource.com/gpgkey/nodesource.gpg.key state=present

- name: node | Add NodeSource repository
become: true
apt_repository: repo='{{item}}' state=present
with_items:
- deb https://deb.nodesource.com/node_{{node_branch}} trusty main
- deb-src https://deb.nodesource.com/node_{{node_branch}} trusty main

- name: node | Install Node.js
become: true
apt: name='nodejs={{node_version}}' update_cache=yes state=present

- name: node | Install pm2
become: true
npm: name=pm2 global=yes version=2.1.4

OS / ENVIRONMENT

nodejs v4.6.2 LTS shipping with npm v2.5.11
Mac OS X 10.11.6 (local)
Ubuntu 14.04 LTS (provisioning target)

SUMMARY

If a module is installed globally the ownership of npm's cache (the .npm folder) after provisioning is not correct which causes subsequent errors and strange behaviors due to wrong permissions on that folder.

After provisioning with Ansible's npm module the npm cache is populated at /root/.npm but with ownership deploy:deploy where deploy is the user that is used for SSH-login on the remote machine.

As global installation of npm modules requires root privileges become: true is set and causes invocation via sudo. I found out that in these cases Ansible invokes npm via sudo -H ... which causes $HOME to be changed to /root. This might cause population of .npm folder at $HOME/.npm == /root/.npm but with ownership of the user defined by $SUDO_USER which is still deploy.

Unfortunately there is no proper documentation on how npm handles installs via sudo, so this is only a guess from my side.

Nevertheless this can also be reproduced manually via deploy:~$ sudo -H npm install -g pm2 which causes the same behavior. If the flag -H is omitted everything is fine as the .npm folder gets populated at /home/deploy/.npm with ownership deploy:deploy but I do not see any configuration option for Ansible to influence the parameters for the sudo-invocation.

STEPS TO REPRODUCE

see above

EXPECTED RESULTS

npm's cache should be populated at /home/deploy/.npm with ownership deploy:deploy.

ACTUAL RESULTS

npm's cache is populated at /root/.npm with ownership deploy:deploy.

root:~# ls -la
-rw------- 1 root root 6893 Nov 11 07:26 .bash_history
-rw-r--r-- 1 root root 3106 Feb 20 2014 .bashrc
drwxr-xr-x 3 deploy deploy 4096 Nov 10 21:01 .npm/
-rw-r--r-- 1 root root 141 Nov 7 19:55 .profile
drwx------ 2 root root 4096 Jul 29 10:00 .ssh/
-rw------- 1 root root 3787 Nov 9 20:42 .viminfo

Copied from original issue: ansible/ansible-modules-extras#3442