- 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_{{ nodejs_version }} {{ ansible_distribution_release }} main'
    state: present

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

- name: Install Node.js
  apt:
    pkg: nodejs
    state: installed
    update_cache: yes

- name: Update npm
  command: bash -lc "npm install -g npm"

- 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_{{ nodejs_version }} {{ ansible_distribution_release }} main'
    state: present

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

- name: Install Node.js
  apt:
    pkg: nodejs
    state: installed
    update_cache: yes

- name: Update npm
  command: bash -lc "npm install -g npm"

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

Copied from https://github.com/nodesource/ansible-nodejs-role/commit/f2a992dc6943d13de15ef24842b1161b6aad386d
