【问题标题】:Saltstack cmd.run - if statement and unlessSaltstack cmd.run - if 语句和除非
【发布时间】:2018-04-27 08:52:17
【问题描述】:

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

  • 根据 fqdn 设置位置
  • 检查 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 语句时,它没有考虑除非。它直接运行命令而不检查除非条件。 另外,我确信我的除非语句有效,只有一个位置就可以了。

我怎样才能写这个让 if 和 unless 同时工作? 谢谢

【问题讨论】:

  • 你试过类似- unless: false的东西吗?
  • 你也可以试试onlyif
  • 我试过 - unless: false ,它仍然运行命令。

标签: salt-stack


【解决方案1】:

我认为问题在于 if 条件。如果您使用if salt['cmd.run'](),那么第一个if 将始终为真。

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

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

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

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

【讨论】:

  • 我在答案/解决方法中使用的 IF 条件有效,所以这不是主要问题。但是谷物的使用很有趣,谢谢。
【解决方案2】:

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

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 起作用。

【讨论】:

    猜你喜欢
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多