如何解决在变量 SaltStack
我有一个状态,如果文件夹存在与否,它应该给我一个输出。 我不知道如何转义文件夹 Programs 文件的名称,所以 PS 知道我的意思。
{% set label = salt['cmd.run']('powershell.exe "test-path \"$env:SystemDrive\\Program Files (x86)\\Folder\""') %}
{% if label == 'True' %}
First:
cmd.run:
- name: Write-Host "Exist"
- shell: powershell
{% else %}
Secondary:
cmd.run:
- name: Write-Host " NOT "
- shell: powershell
{% endif %}
我每次尝试都没有得到,但文件夹存在
你能告诉我如何进行这项工作吗?我在哪里可以找到有关如何在 YAML 中转义行的信息
解决方法
问题很可能不在于对 powershell.exe 的调用,因为这是我从 cmd.exe 命令提示符运行以下命令时得到的结果:
C:\>powershell.exe "test-path \"$env:SystemDrive\\Program Files (x86)\\DoesntExist\""
False
C:\>powershell.exe "test-path \"$env:SystemDrive\\Program Files (x86)\\""
True
,
我建议使用 Saltstack file module 通过 directory_exists
函数检查目录是否存在。
这允许我们使用 requisite system 来触发进一步的状态。
示例:
check-dir-exists:
module.run:
- name: file.directory_exists
- path: 'C:\Program Files (x86)\SomeDirectory'
show-dir-exists:
cmd.run:
- name: Write-Host "Dir exists"
- shell: powershell
- require:
- module: check-dir-exists
show-dir-not-exist:
cmd.run:
- name: Write-Host "Dir not exists"
- shell: powershell
- onfail:
- module: check-dir-exists
或者将返回值 True/False
存储在一个变量中,并在 if/else
语句中匹配条件。
示例:
{% set label = salt['file.directory_exists']('C:\Program Files (x86)\SomeDirectory') %}
{% if label %}
show-dir-exists:
cmd.run:
- name: Write-Host "Dir exists"
- shell: powershell
{% else %}
show-dir-not-exist:
cmd.run:
- name: Write-Host "Dir not exists"
- shell: powershell
{% endif %}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。