如何引用Saltstack状态下的slot?

发布于 2025-01-09 03:41:16 字数 2184 浏览 0 评论 0原文

我可以使用 Saltstack Slot 直接设置值。我需要的是使用内联值。

我的目标是使用 reg.present 为特定用户设置密钥,例如:

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

它不起作用,因为我没有按照需要获取 Sid。我试图弄清楚这一点,但我被困住了。以下是我试图抓住它的尝试。

所以,这很好用:

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

这不行:

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

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

编辑:

正如 OrangeDog 在评论中指出的那样,不可能在插槽前添加任何字符串,因此这就是后者不起作用的原因。

我尝试按照建议使用 Jinja,状态如下:

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

这不起作用,错误如下:

----------
    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
---

现在,我认为这里没有机会使用 Jinja。据我了解,Jinja 模板是在执行状态之前渲染的 - 根据 文档。所以我的猜测是它是在插槽求值之前执行的,因此 Jinja 尝试将值 __slot__:salt:user.getUserSid(Student) 分配给 uid 变量,而不是求值槽并将结果分配给 uid

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

编辑:最终的工作解决方案感谢@OrangeDog 对其他陷入困境的人的支持:

{% 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌︼了一个春 2025-01-16 03:41:16

它位于您在问题中链接的文档中。

您可以使用 ~ 将文本附加到槽结果,但不能添加前缀。

如果您确实需要使用插槽,那么您可以编写自定义执行模块来返回您需要的完整字符串。否则,只需使用 jinja:

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

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") %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文