如何引用Saltstack状态下的slot?
我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它位于您在问题中链接的文档中。
您可以使用
~
将文本附加到槽结果,但不能添加前缀。如果您确实需要使用插槽,那么您可以编写自定义执行模块来返回您需要的完整字符串。否则,只需使用 jinja:
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: