Skip to content

Commit

Permalink
Create global install dir
Browse files Browse the repository at this point in the history
  • Loading branch information
thom8 committed May 30, 2016
1 parent 2d0021d commit dfcd192
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ script:
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm node -v'

# Ensure npm packages are installed globally.
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm npm list -g --depth=0 jslint'
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm npm list -g --depth=0 node-sass'
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm npm list -g --depth=0 yo'
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm NPM_CONFIG_PREFIX=/root/.npm-global NODE_PATH=/root/.npm-global/lib/node_modules npm list -g --depth=0 jslint'
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm NPM_CONFIG_PREFIX=/root/.npm-global NODE_PATH=/root/.npm-global/lib/node_modules npm list -g --depth=0 node-sass'
- 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm NPM_CONFIG_PREFIX=/root/.npm-global NODE_PATH=/root/.npm-global/lib/node_modules npm list -g --depth=0 yo'

# Clean up
- 'sudo docker stop "$(cat ${container_id})"'
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ The Node.js version to install. "0.10" is the default and works on all supported

nodejs_install_npm_user: "{{ ansible_ssh_user }}"

The user for whom the npm packages will be installed can be set here, this defaults to ansible_ssh_user
The user for whom the npm packages will be installed can be set here, this defaults to ansible_user

npm_config_prefix: "~/.npm-global"

The global installation directory. This should be writeable by the nodejs_install_npm_user.

npm_config_unsafe_perm: "false"

Set to true to suppress the UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail.

nodejs_npm_global_packages: []

Expand Down
6 changes: 6 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ nodejs_version: "0.12"
# The user for whom the npm packages will be installed.
# nodejs_install_npm_user: username

# The directory for global installations.
npm_config_prefix: "~/.npm-global"

# Set to true to suppress the UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail.
npm_config_unsafe_perm: "false"

# Define a list of global packages to be installed with NPM.
nodejs_npm_global_packages: []
# # Install a specific version of a package.
Expand Down
31 changes: 24 additions & 7 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,44 @@

- name: Define nodejs_install_npm_user
set_fact:
nodejs_install_npm_user: "{{ ansible_ssh_user }}"
nodejs_install_npm_user: "{{ ansible_user }}"
when: nodejs_install_npm_user is not defined

- name: Create npm global directory
file:
path: "{{ npm_config_prefix }}"
owner: "{{ nodejs_install_npm_user }}"
group: "{{ nodejs_install_npm_user }}"
state: directory

- name: Add npm_config_prefix bin directory to global $PATH.
template:
src: npm.sh.j2
dest: /etc/profile.d/npm.sh
mode: 0644

# TODO: Remove this task after Ansible 2.x npm module bug is fixed. See:
# https://github.com/ansible/ansible-modules-extras/issues/1375
- name: Ensure npm global packages are installed.
npm:
name: "{{ item.name }}"
version: "{{ item.version | default('') }}"
version: "{{ item.version | default('latest') }}"
global: yes
state: present
become: yes
become_user: "{{ nodejs_install_npm_user }}"
environment:
NPM_CONFIG_PREFIX: "{{ npm_config_prefix }}"
NODE_PATH: "{{ npm_config_prefix }}/lib/node_modules"
NPM_CONFIG_UNSAFE_PERM: "{{ npm_config_unsafe_perm }}"
with_items: "{{ nodejs_npm_global_packages }}"

- name: Ensure npm global packages are at the latest release.
npm:
name: "{{ item.name }}"
version: "{{ item.version | default('') }}"
version: "{{ item.version | default('latest') }}"
global: yes
state: latest
become: yes
become_user: "{{ nodejs_install_npm_user }}"
environment:
NPM_CONFIG_PREFIX: "{{ npm_config_prefix }}"
NODE_PATH: "{{ npm_config_prefix }}/lib/node_modules"
NPM_CONFIG_UNSAFE_PERM: "{{ npm_config_unsafe_perm }}"
with_items: "{{ nodejs_npm_global_packages }}"
7 changes: 6 additions & 1 deletion tasks/setup-Debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
with_items:
- "deb https://deb.nodesource.com/node_{{ nodejs_version }} {{ ansible_distribution_release }} main"
- "deb-src https://deb.nodesource.com/node_{{ nodejs_version }} {{ ansible_distribution_release }} main"
register: node_repo

- name: Update apt cache if repo was added.
apt: update_cache=yes
when: node_repo.changed

- name: Ensure Node.js and npm are installed.
apt: "name=nodejs={{ nodejs_version|regex_replace('x', '') }}* state=present update_cache=yes"
apt: "name=nodejs={{ nodejs_version|regex_replace('x', '') }}* state=present"
3 changes: 3 additions & 0 deletions templates/npm.sh.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export PATH={{ npm_config_prefix }}/bin:$PATH
export NPM_CONFIG_PREFIX={{ npm_config_prefix }}
export NODE_PATH=$NODE_PATH:{{ npm_config_prefix }}/lib/node_modules
2 changes: 2 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
vars:
nodejs_version: "5.x"
nodejs_install_npm_user: root
npm_config_prefix: /root/.npm-global
npm_config_unsafe_perm: "true"
nodejs_npm_global_packages:
- name: node-sass
- name: jslint
Expand Down

0 comments on commit dfcd192

Please sign in to comment.