Configuration of App VMS via `salt`: using a variable as a key in a `python`/`salt`/`jinja2` dict?

I’m streamlining my repetitive and generally byzantine salt configuration and came up with the following for my custom app VMs *.sls:

# Assure presence of custom VMs
## Define defaults
{% set dflt_lbl = 'red' %}
{% set dflt_menu_items = 'qubes-open-file-manager.desktop qubes-run-terminal.desktop' %}
{% set dflt_prvt_vlm_sz = '2147483648' %}
{% set dflt_tmplt_fdr = 'fedora-38' %}
{% set dflt_tmplt = dflt_tmplt_fdr %}
{% set dflt_dev_tmplt = [dflt_tmplt, 'dev']|join('-') %}
{% set dflt_dvm_tmplt = [dflt_tmplt, 'dvm']|join('-') %}
{% set dflt_tmplt_dbn = 'debian-12' %}

## VM definitions
{% set cstm_appvms = {
 'task-privarchive': {
    'label':	   'blue',
    'menu-items':  ['org.kde.digikam.desktop', dflt_menu_items]|join(' '),
    'prvt_vlm_sz': '80530636800'
  },
  'task-privcom': {
    'template':    dflt_tmplt_dbn,
    'label':	   'orange',
    'menu-items':  ['org.gnome.Evolution.desktop', dflt_menu_items]|join(' '),
    'prvt_vlm_sz': '8589934592'
  }
}
%}

## Iterate over definitions
{% for ca in cstm_appvms %}

### Define VMs
Assure presence of {{ ca }}:
  qvm.vm:
    - name: {{ ca }}
    - present:
      - template: {{ cstm_appvms[ca]['template']|default(dflt_tmplt) }}
      - label:    {{ cstm_appvms[ca]['label']|default(dflt_lbl) }}
    - features:
      - set:
        - appmenus-dispvm: {{ cstm_appvms[ca]['appmenus-dispvm']|default('0') }}
        - icon: {{ cstm_appvms[ca]['icon']|default(['appvm', cstm_appvms[ca]['label']|default(dflt_lbl)]|join('-')) }}
        - menu-items: {{ cstm_appvms[ca]['menu-items']|default(dflt_menu_items) }}
        - template_for_dispvms: {{ cstm_appvms[ca]['template_for_dispvms']|default('False') }}

### Define size of vm:private volume
Assure sizing of private volume of {{ ca }}:
  cmd.run:
    - name: qvm-volume extend {{ ca }}:private {{ cstm_appvms[ca]['prvt_vlm_sz']|default(dflt_prvt_vlm_sz) }}
{% endfor %}

So far so good.

Now I am trying to amend the cstm_appvms dictionary by an entry that derives its key from a variable - and am utterly failing with all options searchengining has provided me with. Can someone (@unman, please!?) put me out of my misery, please?

My latest attempt, which goes before the for loop above, fails and is included here to communicate what I’m trying to achieve, looks as follows:

## Include item with variable key - note the dflt_dvm_tmplt
{% do cstm_appvms.update(
  dflt_dvm_tmplt,
  {
    'appmenus-dispvm':      '1',
    'icon':                 'templatevm-red',
    'template_for_dispvms': 'True'
  })
%}

This particular “solution” fails with

Rendering SLS 'base:custom_dom0.dom0_assert_custom_appvms_presence' failed: Jinja error: update expected at most 1 argument, got 2

I have now idea, what I have been screwing up, but just using the following dict of VM definitions now just works:

 {% set cstm_appvms = {
  dflt_dvm_tmplt: {
    'appmenus-dispvm':      '1',
    'icon':                 'templatevm-red',
    'menu-items':           ['chromium-browser.desktop', 'firefox.desktop', dflt_menu_items>
    'template_for_dispvms': 'True'
  },
  'task-privarchive': {
    'label':	   'blue',
    'menu-items':  ['org.kde.digikam.desktop', dflt_menu_items]|join(' '),
    'prvt_vlm_sz': '80530636800'
  },
  'task-privcom': {
    'template':    dflt_tmplt_dbn,
    'label':	   'orange',
    'menu-items':  ['org.gnome.Evolution.desktop', dflt_menu_items]|join(' '),
    'prvt_vlm_sz': '8589934592'
  }
}
%}

Sorry for the apparent noise …

1 Like