# Install Node.js using packages crafted by NodeSource
---
- name: Ensure the system can use the HTTPS transport for APT
  stat:
    path: /usr/lib/apt/methods/https
  register: apt_https_transport

- name: Install HTTPS transport for APT
  apt:
    pkg: apt-transport-https
    state: installed
  when: not apt_https_transport.stat.exists

- name: Import the NodeSource GPG key into apt
  apt_key:
    url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
    state: present

- name: Add NodeSource deb repository
  apt_repository:
    repo: 'deb https://deb.nodesource.com/node_{{ debian_repo_version }} {{ ansible_distribution_release }} main'
    state: present

- name: Add NodeSource deb-src repository
  apt_repository:
    repo: 'deb-src https://deb.nodesource.com/node_{{ debian_repo_version }} {{ ansible_distribution_release }} main'
    state: present

- name: Add NodeSource repository preferences
  template:
    src: etc/apt/preferences.d/deb_nodesource_com_node.pref.2
    dest: /etc/apt/preferences.d/deb_nodesource_com_node.pref

- name: Install Node.js
  apt:
    pkg:
      - nodejs={{ nodejs_version }}*
    state: installed
    update_cache: yes

# Install Node.js using packages crafted by NodeSource
---
- name: Ensure the system can use the HTTPS transport for APT
  stat:
    path: /usr/lib/apt/methods/https
  register: apt_https_transport

- name: Install HTTPS transport for APT
  apt:
    pkg: apt-transport-https
    state: installed
  when: not apt_https_transport.stat.exists

- name: Import the NodeSource GPG key into apt
  apt_key:
    url: https://keyserver.ubuntu.com/pks/lookup?op=get&fingerprint=on&search=0x1655A0AB68576280
    id: "68576280"
    state: present

- name: Add NodeSource deb repository
  apt_repository:
    repo: 'deb https://deb.nodesource.com/node_{{ debian_repo_version }} {{ ansible_distribution_release }} main'
    state: present

- name: Add NodeSource deb-src repository
  apt_repository:
    repo: 'deb-src https://deb.nodesource.com/node_{{ debian_repo_version }} {{ ansible_distribution_release }} main'
    state: present

- name: Add NodeSource repository preferences
  template:
    src: etc/apt/preferences.d/deb_nodesource_com_node.pref.2
    dest: /etc/apt/preferences.d/deb_nodesource_com_node.pref

- name: Install Node.js
  apt:
    pkg:
      - nodejs={{ nodejs_version }}*
    state: installed
    update_cache: yes

Install GPG key from keyserver.ubuntu.com
Install GPG key from keyserver.ubuntu.com

deb.nodesource.com is now in CloudFront, and older versions of Python
(such as than installed in Ubuntu 12.04 and 14.04) can no longer install
the GPG key from that server. Instead, install the key from
keyserver.ubuntu.com, and include an ID so that downloading can be
skipped if the key is already installed. Fixes #33.
