Browse Source

add nodejs vagrant / ansible

les 3 years ago
parent
commit
d314955501

+ 5 - 0
nodejs.yml

@@ -0,0 +1,5 @@
+---
+- name: nodejs test
+  hosts: all
+  roles:
+  - nodejs

+ 27 - 0
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: ""

+ 38 - 0
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

+ 33 - 0
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

+ 3 - 0
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

+ 11 - 0
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	

+ 1 - 0
tests/dev/roles

@@ -0,0 +1 @@
+../../roles

+ 8 - 0
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

+ 6 - 0
tests/nodejs/nodejs.yml

@@ -0,0 +1,6 @@
+---
+- name: nodejs test
+  hosts: localhost
+  become: yes
+  roles:
+  - nodejs

+ 1 - 0
tests/nodejs/roles

@@ -0,0 +1 @@
+../../roles