diff --git a/nodejs.yml b/nodejs.yml new file mode 100644 index 0000000..3aed213 --- /dev/null +++ b/nodejs.yml @@ -0,0 +1,5 @@ +--- +- name: nodejs test + hosts: all + roles: + - nodejs diff --git a/roles/nodejs/defaults/main.yml b/roles/nodejs/defaults/main.yml new file mode 100644 index 0000000..eeae50e --- /dev/null +++ b/roles/nodejs/defaults/main.yml @@ -0,0 +1,27 @@ +--- +# Set the version of Node.js to install (8.x", "10.x", "12.x", "13.x", etc.). +# Version numbers from Nodesource: https://github.com/nodesource/distributions +nodejs_version: "14.x" + +# The user for whom the npm packages will be installed. +# nodejs_install_npm_user: username + +# The directory for global installations. +npm_config_prefix: "/usr/local/lib/npm" + +# 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. +# - name: jslint +# version: 0.9.3 +# # Install the latest stable release of a package. +# - name: node-sass +# # This shorthand syntax also works (same as previous example). +# - node-sass + +# The path of a package.json file used to install packages globally. +nodejs_package_json_path: "" diff --git a/roles/nodejs/tasks/main.yml b/roles/nodejs/tasks/main.yml new file mode 100644 index 0000000..8d40607 --- /dev/null +++ b/roles/nodejs/tasks/main.yml @@ -0,0 +1,38 @@ +--- +- include: setup.yml + +- name: Define nodejs_install_npm_user + set_fact: + nodejs_install_npm_user: "{{ ansible_user | default(lookup('env', '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 + +- name: Ensure npm global packages are installed. + npm: + name: "{{ item.name | default(item) }}" + version: "{{ item.version | default('latest') }}" + global: true + state: latest + 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 }}" + tags: ['skip_ansible_lint'] + +- name: Install packages defined in a given package.json. + npm: + path: "{{ nodejs_package_json_path }}" + when: nodejs_package_json_path is defined and nodejs_package_json_path diff --git a/roles/nodejs/tasks/setup.yml b/roles/nodejs/tasks/setup.yml new file mode 100644 index 0000000..4057de9 --- /dev/null +++ b/roles/nodejs/tasks/setup.yml @@ -0,0 +1,33 @@ +--- +- name: Ensure dependencies are present. + apt: + name: + - apt-transport-https + - gnupg2 + - build-essential + state: present + +- name: Add Nodesource apt key. + apt_key: + url: https://keyserver.ubuntu.com/pks/lookup?op=get&fingerprint=on&search=0x1655A0AB68576280 + id: "68576280" + state: present + +- name: Add NodeSource repositories for Node.js. + apt_repository: + repo: "{{ item }}" + state: present + 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 + tags: ['skip_ansible_lint'] + +- name: Ensure Node.js and npm are installed. + apt: + name: "nodejs={{ nodejs_version|regex_replace('x', '') }}*" + state: present diff --git a/roles/nodejs/templates/npm.sh.j2 b/roles/nodejs/templates/npm.sh.j2 new file mode 100644 index 0000000..aaeecee --- /dev/null +++ b/roles/nodejs/templates/npm.sh.j2 @@ -0,0 +1,3 @@ +export PATH=$PATH:{{ npm_config_prefix }}/bin +export NPM_CONFIG_PREFIX={{ npm_config_prefix }} +export NODE_PATH=$NODE_PATH:{{ npm_config_prefix }}/lib/node_modules diff --git a/tests/dev/Vagrantfile b/tests/dev/Vagrantfile new file mode 100644 index 0000000..42585e0 --- /dev/null +++ b/tests/dev/Vagrantfile @@ -0,0 +1,11 @@ +# vagrant cisti.org dev file +# use this vagrant to build and test your ansible role + +Vagrant.configure("2") do |config| + config.vm.define :dev do |dev| + dev.vm.box = "generic/debian10" + dev.vm.synced_folder "../..", "/vagrant", disabled: false + dev.vm.provision "shell", inline: "DEBIAN_FRONTEND=noninteractive apt-get -qq update && apt-get -y autoclean && apt-get -y autoremove" + dev.vm.provision "shell", inline: "DEBIAN_FRONTEND=noninteractive apt-get -fqy dist-upgrade && apt-get -qq -y install ansible" + end +end diff --git a/tests/dev/roles b/tests/dev/roles new file mode 120000 index 0000000..b741aa3 --- /dev/null +++ b/tests/dev/roles @@ -0,0 +1 @@ +../../roles \ No newline at end of file diff --git a/tests/nodejs/Vagrantfile b/tests/nodejs/Vagrantfile new file mode 100644 index 0000000..270d0ab --- /dev/null +++ b/tests/nodejs/Vagrantfile @@ -0,0 +1,8 @@ +Vagrant.configure("2") do |config| + config.vm.define :node do |node| + node.vm.box = "generic/debian10" + node.vm.provision "ansible" do |ansible| + ansible.playbook = "./nodejs.yml" + end + end +end diff --git a/tests/nodejs/nodejs.yml b/tests/nodejs/nodejs.yml new file mode 100644 index 0000000..72c38cd --- /dev/null +++ b/tests/nodejs/nodejs.yml @@ -0,0 +1,6 @@ +--- +- name: nodejs test + hosts: localhost + become: yes + roles: + - nodejs diff --git a/tests/nodejs/roles b/tests/nodejs/roles new file mode 120000 index 0000000..b741aa3 --- /dev/null +++ b/tests/nodejs/roles @@ -0,0 +1 @@ +../../roles \ No newline at end of file