Search code examples
registrysalt-project

How to refer slot in Saltstack state?


I'm able to use Saltstack Slot to set a value directly. What I need is to use the value inline.

My goal is to use reg.present to set a key for a particular user, like:

HKU\Student\Software\Microsoft\Windows\CurrentVersion\Policies\Uninstall!NoAddRemovePrograms:
  reg.present:
    - name: HKU\__slot__:salt:user.getUserSid(Student)\Software\Microsoft\Windows\CurrentVersion\Policies\Uninstall!NoAddRemovePrograms

It doesn't work, as I don't get the Sid in place as I'd need to. I tried figuring this out, but I'm stuck. Below are my attempts to get a grip on it.

So, this works great:

echo variable:
 cmd.run:
   - env:
     - myuid: __slot__:salt:user.getUserSid(Student)
   - name: Get-Item -Path Env:myuid
   - shell: powershell

This does not:

echo direct:
  cmd.run:
    - name: echo __slot__:salt:user.getUserSid(Student)

==========================================

EDIT:

As OrangeDog pointed out in the comment, it's not possible to prepend a slot with any string, so that is why the latter won't work.

I tried using Jinja following the advice, here's the state:

{% set uid = __slot__:salt:user.getUserSid(Student) %}
echo with jinja:
  cmd.run:
    - name:  echo {{ uid }}
    - shell: powershell

This doesn't work, here's the error:

----------
    Rendering SLS 'base:echo' failed: Jinja syntax error: expected token 'end of statement block', got ':'; line 17

---
[...]   
{% set uid = __slot__:salt:user.getUserSid(Student) %}    <======================
echo with jinja:
  cmd.run:
    - name:  echo {{ uid }}
    - shell: powershell
---

Now, I think there is no chance to use Jinja here. As far as I understand, Jinja template is rendered BEFORE state is executed - as per the doc. So my guess is it's executed before slot is evaluated, so Jinja tries to assign the value __slot__:salt:user.getUserSid(Student) to the uid variable, rather then evaluating the slot and assigning the result to uid.

==========================================

EDIT: final, working solution thanks to @OrangeDog support for anyone else struggling:

{% set uid = salt["user.getUserSid"]("Student") %}

HKU\Studnet\Software\Microsoft\Windows\CurrentVersion\Policies\Uninstall!NoAddRemovePrograms:
  reg.present:
    - name: HKU\{{ uid }}\Software\Microsoft\Windows\CurrentVersion\Policies\Uninstall!NoAddRemovePrograms

Solution

  • It's in the documentation you linked in the question.

    You can append text to the slot result with ~, but you cannot prepend.

    If you really need to use a slot then you can write a custom execution module to return the full string you need. Otherwise, just use jinja:

    {% set SID = salt["user.getUserSid"]("Student") %}