File Render with multiple inventories

Prerequisites

Modules

git checkout origin/osp-web-configuration .

Vscode extension

Example configuration

git checkout origin/template-playbooks-filerender-multiple-inventory .

Description

This example demonstrate usage of a template-playbook to automatically generate a configuration file that take multiple Inventory as data input:

../../_images/targets-file-renderer-multiple-inventory.svg

This will simulate multiple devices that are present in different sites.

This example integrate a list of sites that may contains some lights. Playbook will generate a single dashboard that contains a widget for every light. Every widgets will have a background color corresponding to the site where it is located.

Example

1. Create inventories

This example integrate a list of sites that may contains some lights. First create two inventories.

One for the list of sites that contains site name and corresponding color:

templates/inventory/sites.csv
source;destination;rule;variable-siteLocation;variable-backgroundColor
templates.sources.site;root.fribourg;;fribourg;A1A1A1
templates.sources.site;root.lausanne;;lausanne;FFD835
templates.sources.site;root.berne;;berne;1DAA54

And a second one that describe every lights and where their sites.

templates/inventory/devices.csv
source;destination;rule;variable-deviceName;variable-siteLocation
templates.sources;root.fribourg;;light01;fribourg
templates.sources;root.lausanne;;light02;lausanne
templates.sources;root.berne;;light03;berne
templates.sources;root.lausanne;;light04;lausanne

2. Create a playbook

Note

In this example we have a second target of type ItemsRender to automatically add a dummy value for every widget subscription. This is not necessary.

templates/playbooks/example.playbook
 1[
 2    {
 3        "type": "FileRender",
 4        "generation": [
 5            {
 6                "destination": "root.devices.light.monitoring",
 7                "source": "templates/sources/dashboard.view.nunjucks"
 8            }
 9        ],
10        "import": {
11            "lights": "templates/inventory/devices.csv",
12            "sites": "templates/inventory/sites.csv"
13        }
14    },
15    {
16        "type": "FileRender",
17        "generation": [
18            {
19                "destination": "root.devices.light.monitoring",
20                "source": "templates/sources/dashboard.web.nunjucks"
21            }
22        ],
23        "import": {}
24    },
25    {
26        "type": "ItemsRender",
27        "source": "templates/inventory/sites.csv",
28        "fileRenderContext": {}
29    }
30]

Target FileRender import both two inventories defines in previous step as lights and sites.

3. Create dashboard template

templates/sources/dashboard.view.nunjucks
 1{% set comma = joiner(',') %}
 2{
 3    "configuration": [
 4      {% for light in lights -%}{{ comma() }}
 5      {
 6        "type": "BasicInputOutputValue",
 7        "id": "{{ light.deviceName }}",
 8        "title": "{{ light.deviceName }}",
 9        "valueSubscriptions": {
10            "values": [
11                {
12                    "id": "{{ light.destination }}.light",
13                    "type": "BOOLEAN",
14                    "right": "READ_WRITE"
15                }
16            ]
17        },
18        "basicWidgetSettings": {
19            "label": "{{ light.deviceName }}",
20            "showLabel": true,
21            "showTooltip": true,
22            "showIcon": true,
23            "showValue": false,
24            "onValue": false,
25            "offValue": true,
26            "onIcon": "logout",
27            "offIcon": "warning_amber",
28            "onBackgroundColor": "#{{ sites | innerJoin(light, "siteLocation", "backgroundColor") | first }}",
29            "offBackgroundColor": "#{{ sites | innerJoin(light, "siteLocation", "backgroundColor") | first }}"
30        }
31    }
32    {%- endfor %}
33    ],
34    "layout": {
35        "lg": []
36    }
37}

This template create a widget for every lights inside light’s inventory. But on top of that (on line 28-29), target will access to sites inventory to lookup for a match entry between variable siteLocation. When an entry is found, it will replace be the value in backgroundColor.

Note

Remember sites inventory header:

templates/inventory/sites.csv
source;destination;rule;variable-siteLocation;variable-backgroundColor

4. Execute the playbook

Generate the playbook template with one of the given command :

5. Check files generated

Finally you can verify the file generated. It contains one widget for every lights and background colors correspond to the one defines in site inventory:

root/devices/light/monitoring/dashboard.view
{
  "type": "BasicInputOutputValue",
  "id": "light01",
  "title": "light01",
  "valueSubscriptions": {
    "values": [
      {
        "id": "root.fribourg.light",
        "type": "BOOLEAN",
        "right": "READ_WRITE"
      }
    ]
  },
  "basicWidgetSettings": {
    "label": "light01",
    "showLabel": true,
    "showTooltip": true,
    "showIcon": true,
    "showValue": false,
    "onValue": false,
    "offValue": true,
    "onIcon": "logout",
    "offIcon": "warning_amber",
    "onBackgroundColor": "#A1A1A1",
    "offBackgroundColor": "#A1A1A1"
  }
}