[qubes-users] saltstack: detect the os within a running target/state.sls

Hi!

Running grains[‘os’] in a target returns always “Fedora” and grains[‘id’] returns “dom0”. For this reason I cannot branch in my state files for different os templates.
Executing this test in such a targeted state.sls:

test-grains-for-choosing-the-right-template:
test.configurable_test_state:

  • name: “grains test”
  • changes: True
  • result: True
  • comment: {{ grains[‘os’] ~ ", " ~ grains[‘id’] ~ ", " ~ grains[‘osfullname’] ~ ", " ~ grains[‘os_family’] }}
returns
grains['os']=Fedora, grains['os']=dom0, grains['osfullname']=Qubes, grains['osfamily']=RedHat

Any ideas why this happens? How to branch correctly in a state file depending on the os of the template?

Best, P!

When you use qubesctl it acts in the following order - dom0, templates,
template based qube. Unless you specifically exclude dom0, the state
will be applied to dom0.

You havent said what command you entered, which would have been useful,
but my guess is that you used `qubesctl state.apply ...`
If you want to target a template, then you should use:
`qubesctl --skip-dom0 --targets=LIST_OF_TARGETS .....`

If you want a state file to be used BOTH for dom0 and templates, you can
use a "defensive" approach - there are examples of this at

Use a jinja block like - {% if grains['nodename'] != 'dom0' %}
This will ensure that non-dom0 stuff gets correctly applied, while
making sure that those parts of the state dont get applied to dom0.