shantanoo-desai / komponist

A Composer for your favorite IoT/ IIoT container stacks with Ansible + Jinja2 + Docker Compose v2
GNU Affero General Public License v3.0
25 stars 2 forks source link

[node-red] Provide a playbook to bootstrap node-RED with relevant DB nodes #108

Closed shantanoo-desai closed 12 months ago

shantanoo-desai commented 1 year ago

Description

Depending on the database selected (default: all), provision the Node-RED instance with the following:

shantanoo-desai commented 1 year ago

Implementation

  1. Check if container exists and is running via assert
  2. Obtain Access Token from the /auth/token API via HTTP POST
  3. Install module with /nodes API via HTTP POST depending on existences of databases
  4. Revoke Access Token at the end
shantanoo-desai commented 1 year ago

Playbook

---
- name: bootstrap node-RED with komponist related Nodes
  hosts: localhost
  gather_facts: true
  vars_files:
    - vars/creds.yml
    - vars/config.yml
  module_defaults:
    ansible.builtin.uri:
      method: POST
      headers:
        Content-Type: application/json

  tasks:
    - name: Get Node-RED user with all Privileges
      set_fact:
        nodered_creds: "{{ item }}"
      when: "item.permissions is defined and item['permissions'] == '*'"
      loop: "{{ credentials.nodered.users }}"

    - name: Obtain Authentication Token
      ansible.builtin.uri:
        url: http://localhost/nodered/auth/token
        body:
          client_id: node-red-admin
          grant_type: password
          scope: "{{ nodered_creds.permissions }}"
          username: "{{ nodered_creds.username }}"
          password: "{{ nodered_creds.password }}"
        body_format: json
        status_code: 200
      register: auth_token
      when: nodered_creds is defined

    - name: Install InfluxDB Node into Node-RED
      ansible.builtin.uri:
        url: http://localhost/nodered/nodes
        headers:
          Authorization: "{{ auth_token.json.token_type }} {{ auth_token.json.access_token }}"
        body:
          module: "node-red-contrib-influxdb"
        body_format: json
        status_code: 200
      when: "'influxdbv1' in komponist.configuration.keys() or 'influxdbv2' in komponist.configuration.keys()"

    - name: Install PostgreSQL Node into Node-RED
      ansible.builtin.uri:
        url: http://localhost/nodered/nodes
        headers:
          Authorization: "{{ auth_token.json.token_type }} {{ auth_token.json.access_token }}"
        body:
          module: "node-red-contrib-postgresql"
        body_format: json
        status_code: 200
      when: "'questdb' in komponist.configuration.keys() or 'timescaledb' in komponist.configuration.keys()"
      register: node_results

    - name: Revoke Authentication Token
      ansible.builtin.uri:
        url: http://localhost/nodered/auth/revoke
        headers:
          Authorization: "{{ auth_token.json.token_type }} {{ auth_token.json.access_token }}"
        body:
          token: "{{ auth_token.json.access_token }}"
        body_format: json
        status_code: 200