Saltstack cmd.run - if 语句和 unless

Saltstack cmd.run - if statement and unless

我使用 Saltstack 管理我们工作站的安装。 在我的客户端 ipa-client-automount 安装秘诀中,我需要:

目前,我处于以下状态:

 ipa-client-automount:
  cmd.run:
    {% if salt['cmd.run']('hostname -f | grep domain1') %}
    - name: ipa-client-automount --location=linkedtodomain1 -U
    {% elif salt['cmd.run']('hostname -f | grep domain2') %}
    - name: ipa-client-automount --location=linkedtodomain2 -U
    {% endif %}
    - unless: python -c "from ipapython import sysrestore; from ipaplatform.paths import paths; statestore = sysrestore.StateFile(paths.IPA_CLIENT_SYSRESTORE); exit(not statestore.has_state('autofs'))"

问题是在添加 if 和 elif 语句时,它没有考虑 unless。它直接运行命令而不检查 unless 条件。 另外,我确定我的 unless 语句有效,只有一个位置就没问题。

我怎么写才能让 if 和 unless 同时工作? 谢谢

我有一个可行的解决方案:

ipa-client-automount:
  cmd.run:
    - names: 
      {% if salt['cmd.run']('hostname -f | grep domain1') %}
      - ipa-client-automount --location=linkedtodomain1 -U
      {% elif salt['cmd.run']('hostname -f | grep domain2') %}
      - ipa-client-automount --location=linkedtodomain2 -U
      {% endif %}
    - unless: condition

这不是最干净的解决方案,但对我有用。不知道为什么这对 names 不起作用,但对 name.

起作用

我觉得是if条件的问题。如果您以这种方式使用 if salt['cmd.run'](),第一个 if 将始终为真。

在 salt 中,更好的方法是将 host grain 与类似的东西一起使用:

{% if grains.get('host') == 'domain1' %}

或者,如果您真的想使用 cmd.run 方法,请尝试以下操作:

{% if salt['cmd.run']('hostname -f') == 'domain1' %}