使用变量SaltStack中的PS命令



我有一个State,如果文件夹存在与否,它应该会给我一个输出。我不知道如何转义文件夹的名称程序文件,所以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 中逃离线路的信息

我建议使用Saltstack文件模块来检查是否存在具有directory_exists函数的目录。

这使我们能够使用必要的系统来触发进一步的状态。

示例:

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

问题很可能不在对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

最新更新