├── .gitignore ├── templates └── npm.sh.j2 ├── tests ├── test.yml └── README.md ├── meta └── main.yml ├── tasks ├── setup-Debian.yml ├── main.yml └── setup-RedHat.yml ├── defaults └── main.yml ├── LICENSE ├── .travis.yml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.retry 2 | tests/test.sh 3 | -------------------------------------------------------------------------------- /templates/npm.sh.j2: -------------------------------------------------------------------------------- 1 | export PATH={{ npm_config_prefix }}/bin:$PATH 2 | export NPM_CONFIG_PREFIX={{ npm_config_prefix }} 3 | export NODE_PATH=$NODE_PATH:{{ npm_config_prefix }}/lib/node_modules 4 | -------------------------------------------------------------------------------- /tests/test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: all 3 | 4 | vars: 5 | nodejs_version: "5.x" 6 | nodejs_install_npm_user: root 7 | npm_config_prefix: /root/.npm-global 8 | npm_config_unsafe_perm: "true" 9 | nodejs_npm_global_packages: 10 | - node-sass 11 | - name: jslint 12 | version: 0.9.6 13 | - name: yo 14 | 15 | pre_tasks: 16 | - name: Update apt cache. 17 | apt: update_cache=yes cache_valid_time=600 18 | when: ansible_os_family == 'Debian' 19 | 20 | roles: 21 | - role_under_test 22 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | dependencies: [] 3 | 4 | galaxy_info: 5 | author: geerlingguy 6 | description: Node.js installation for Linux 7 | company: "Midwestern Mac, LLC" 8 | license: "license (BSD, MIT)" 9 | min_ansible_version: 1.9 10 | platforms: 11 | - name: EL 12 | versions: 13 | - 6 14 | - 7 15 | - name: Debian 16 | versions: 17 | - all 18 | - name: Ubuntu 19 | versions: 20 | - precise 21 | - raring 22 | - saucy 23 | - trusty 24 | - xenial 25 | galaxy_tags: 26 | - development 27 | - web 28 | -------------------------------------------------------------------------------- /tests/README.md: -------------------------------------------------------------------------------- 1 | # Ansible Role tests 2 | 3 | To run the test playbook(s) in this directory: 4 | 5 | 1. Install and start Docker. 6 | 1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`: 7 | - `wget -O tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/` 8 | 1. Make the test shim executable: `chmod +x tests/test.sh`. 9 | 1. Run (from the role root directory) `distro=[distro] playbook=[playbook] ./tests/test.sh` 10 | 11 | If you don't want the container to be automatically deleted after the test playbook is run, add the following environment variables: `cleanup=false container_id=$(date +%s)` 12 | -------------------------------------------------------------------------------- /tasks/setup-Debian.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Ensure apt-transport-https is installed. 3 | apt: name=apt-transport-https state=present 4 | 5 | - name: Add Nodesource apt key. 6 | apt_key: 7 | url: https://keyserver.ubuntu.com/pks/lookup?op=get&fingerprint=on&search=0x1655A0AB68576280 8 | id: "68576280" 9 | state: present 10 | 11 | - name: Add NodeSource repositories for Node.js. 12 | apt_repository: 13 | repo: "{{ item }}" 14 | state: present 15 | with_items: 16 | - "deb https://deb.nodesource.com/node_{{ nodejs_version }} {{ ansible_distribution_release }} main" 17 | - "deb-src https://deb.nodesource.com/node_{{ nodejs_version }} {{ ansible_distribution_release }} main" 18 | register: node_repo 19 | 20 | - name: Update apt cache if repo was added. 21 | apt: update_cache=yes 22 | when: node_repo.changed 23 | 24 | - name: Ensure Node.js and npm are installed. 25 | apt: "name=nodejs={{ nodejs_version|regex_replace('x', '') }}* state=present" 26 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Set the version of Node.js to install ("0.10", "0.12", "4.x", "5.x", "6.x"). 3 | # Version numbers from Nodesource: https://github.com/nodesource/distributions 4 | nodejs_version: "6.x" 5 | 6 | # The user for whom the npm packages will be installed. 7 | # nodejs_install_npm_user: username 8 | 9 | # The directory for global installations. 10 | npm_config_prefix: "/usr/local/lib/npm" 11 | 12 | # 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. 13 | npm_config_unsafe_perm: "false" 14 | 15 | # Define a list of global packages to be installed with NPM. 16 | nodejs_npm_global_packages: [] 17 | # # Install a specific version of a package. 18 | # - name: jslint 19 | # version: 0.9.3 20 | # # Install the latest stable release of a package. 21 | # - name: node-sass 22 | # # This shorthand syntax also works (same as previous example). 23 | # - node-sass 24 | 25 | # The path of a package.json file used to install packages globally. 26 | nodejs_package_json_path: "" 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Jeff Geerling 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | services: docker 3 | 4 | env: 5 | - distro: centos7 6 | - distro: centos6 7 | - distro: ubuntu1604 8 | - distro: ubuntu1404 9 | - distro: ubuntu1204 10 | 11 | script: 12 | # Configure test script so we can run extra tests after playbook is run. 13 | - export container_id=$(date +%s) 14 | - export cleanup=false 15 | 16 | # Download test shim. 17 | - wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/ 18 | - chmod +x ${PWD}/tests/test.sh 19 | 20 | # Run tests. 21 | - ${PWD}/tests/test.sh 22 | 23 | # Ensure Node.js is installed. 24 | - 'docker exec --tty ${container_id} env TERM=xterm which node' 25 | - 'docker exec --tty ${container_id} env TERM=xterm node -v' 26 | 27 | # Ensure npm packages are installed globally. 28 | - 'docker exec --tty ${container_id} env TERM=xterm bash --login -c "npm list -g --depth=0 jslint"' 29 | - 'docker exec --tty ${container_id} env TERM=xterm bash --login -c "npm list -g --depth=0 node-sass"' 30 | - 'docker exec --tty ${container_id} env TERM=xterm bash --login -c "npm list -g --depth=0 yo"' 31 | 32 | notifications: 33 | webhooks: https://galaxy.ansible.com/api/v1/notifications/ 34 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - include: setup-RedHat.yml 3 | when: ansible_os_family == 'RedHat' 4 | 5 | - include: setup-Debian.yml 6 | when: ansible_os_family == 'Debian' 7 | 8 | - name: Define nodejs_install_npm_user 9 | set_fact: 10 | nodejs_install_npm_user: "{{ ansible_user | default(lookup('env', 'USER')) }}" 11 | when: nodejs_install_npm_user is not defined 12 | 13 | - name: Create npm global directory 14 | file: 15 | path: "{{ npm_config_prefix }}" 16 | owner: "{{ nodejs_install_npm_user }}" 17 | group: "{{ nodejs_install_npm_user }}" 18 | state: directory 19 | 20 | - name: Add npm_config_prefix bin directory to global $PATH. 21 | template: 22 | src: npm.sh.j2 23 | dest: /etc/profile.d/npm.sh 24 | mode: 0644 25 | 26 | - name: Ensure npm global packages are installed. 27 | npm: 28 | name: "{{ item.name | default(item) }}" 29 | version: "{{ item.version | default('latest') }}" 30 | global: yes 31 | state: latest 32 | environment: 33 | NPM_CONFIG_PREFIX: "{{ npm_config_prefix }}" 34 | NODE_PATH: "{{ npm_config_prefix }}/lib/node_modules" 35 | NPM_CONFIG_UNSAFE_PERM: "{{ npm_config_unsafe_perm }}" 36 | with_items: "{{ nodejs_npm_global_packages }}" 37 | 38 | - name: Install packages defined in a given package.json. 39 | npm: 40 | path: "{{ nodejs_package_json_path }}" 41 | when: nodejs_package_json_path is defined and nodejs_package_json_path 42 | -------------------------------------------------------------------------------- /tasks/setup-RedHat.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Set up the Nodesource RPM directory for Node.js > 0.10. 3 | set_fact: 4 | nodejs_rhel_rpm_dir: "pub_{{ nodejs_version }}" 5 | when: nodejs_version != '0.10' 6 | 7 | - name: Set up the Nodesource RPM variable for Node.js == 0.10. 8 | set_fact: 9 | nodejs_rhel_rpm_dir: "pub" 10 | when: nodejs_version == '0.10' 11 | 12 | - name: Import Nodesource RPM key (CentOS < 7). 13 | rpm_key: 14 | key: http://rpm.nodesource.com/pub/el/NODESOURCE-GPG-SIGNING-KEY-EL 15 | state: present 16 | when: ansible_distribution_major_version|int < 7 17 | 18 | - name: Import Nodesource RPM key (CentOS 7+).. 19 | rpm_key: 20 | key: https://rpm.nodesource.com/pub/el/NODESOURCE-GPG-SIGNING-KEY-EL 21 | state: present 22 | when: ansible_distribution_major_version|int >= 7 23 | 24 | - name: Add Nodesource repositories for Node.js (CentOS < 7). 25 | yum: 26 | name: "http://rpm.nodesource.com/{{ nodejs_rhel_rpm_dir }}/el/{{ ansible_distribution_major_version }}/{{ ansible_architecture }}/nodesource-release-el{{ ansible_distribution_major_version }}-1.noarch.rpm" 27 | state: present 28 | when: ansible_distribution_major_version|int < 7 29 | 30 | - name: Add Nodesource repositories for Node.js (CentOS 7+). 31 | yum: 32 | name: "https://rpm.nodesource.com/{{ nodejs_rhel_rpm_dir }}/el/{{ ansible_distribution_major_version }}/{{ ansible_architecture }}/nodesource-release-el{{ ansible_distribution_major_version }}-1.noarch.rpm" 33 | state: present 34 | when: ansible_distribution_major_version|int >= 7 35 | 36 | - name: Ensure Node.js and npm are installed. 37 | yum: "name=nodejs-{{ nodejs_version[0] }}.* state=present enablerepo='epel,nodesource'" 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Role: Node.js 2 | 3 | [![Build Status](https://travis-ci.org/geerlingguy/ansible-role-nodejs.svg?branch=master)](https://travis-ci.org/geerlingguy/ansible-role-nodejs) 4 | 5 | Installs Node.js on RHEL/CentOS or Debian/Ubuntu. 6 | 7 | ## Requirements 8 | 9 | Requires the EPEL repository on RedHat/CentOS (you can install it by simply adding the `geerlingguy.repo-epel` role to your playbook). 10 | 11 | ## Role Variables 12 | 13 | Available variables are listed below, along with default values (see `defaults/main.yml`): 14 | 15 | nodejs_version: "6.x" 16 | 17 | The Node.js version to install. "6.x" is the default and works on most supported OSes. Other versions such as "0.12", "4.x", "5.x", "6.x", etc. should work on the latest versions of Debian/Ubuntu and RHEL/CentOS. 18 | 19 | nodejs_install_npm_user: "{{ ansible_ssh_user }}" 20 | 21 | The user for whom the npm packages will be installed can be set here, this defaults to `ansible_user`. 22 | 23 | npm_config_prefix: "/usr/local/lib/npm" 24 | 25 | The global installation directory. This should be writeable by the `nodejs_install_npm_user`. 26 | 27 | npm_config_unsafe_perm: "false" 28 | 29 | 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. 30 | 31 | nodejs_npm_global_packages: [] 32 | 33 | A list of npm packages with a `name` and (optional) `version` to be installed globally. For example: 34 | 35 | nodejs_npm_global_packages: 36 | # Install a specific version of a package. 37 | - name: jslint 38 | version: 0.9.3 39 | # Install the latest stable release of a package. 40 | - name: node-sass 41 | # This shorthand syntax also works (same as previous example). 42 | - node-sass 43 | 44 | 45 | nodejs_package_json_path: "" 46 | 47 | Set a path pointing to a particular `package.json` (e.g. `"/var/www/app/package.json"`). This will install all of the defined packages globally using Ansible's `npm` module. 48 | 49 | ## Dependencies 50 | 51 | None. 52 | 53 | ## Example Playbook 54 | 55 | - hosts: utility 56 | vars_files: 57 | - vars/main.yml 58 | roles: 59 | - geerlingguy.nodejs 60 | 61 | *Inside `vars/main.yml`*: 62 | 63 | nodejs_npm_global_packages: 64 | - name: jslint 65 | - name: node-sass 66 | 67 | ## License 68 | 69 | MIT / BSD 70 | 71 | ## Author Information 72 | 73 | This role was created in 2014 by [Jeff Geerling](https://www.jeffgeerling.com/), author of [Ansible for DevOps](https://www.ansiblefordevops.com/). 74 | --------------------------------------------------------------------------------