【问题标题】:Run an Ansible task only when the variable contains a specific string仅当变量包含特定字符串时才运行 Ansible 任务
【发布时间】:2016-07-29 13:24:14
【问题描述】:

我有多个任务取决于 variable1 的值。我想检查值是否在 {{variable1}} 中,但出现错误:

- name: do something when the value in variable1
  command: <command>
  when: "'value' in {{variable1}}"

我正在使用 ansible 2.0.2

【问题讨论】:

  • 下面的答案对我有用(好吧,我在assert: ... that: ...中使用它。你看到什么错误?

标签: linux conditional ansible ansible-playbook ansible-2.x


【解决方案1】:

如果variable1 是一个字符串,并且您正在其中搜索一个子字符串,这应该可以工作:

when: '"value" in variable1'

如果variable1 是一个数组或字典,in 将搜索 exact 字符串作为其项目之一。

【讨论】:

  • 不应该是:when: "'value' in variable1"
  • 这个方法效果更好,不会像result|search那样触发警告
  • 这仅适用于更高版本的 Ansible,对于早期版本的替代方案,请参阅其他答案。
  • 如果我没记错的话,我将它与 ansible 1.9 一起使用;如果我今天必须写它,我不会使用它。
  • 简单,优雅,而且有效……为什么这仍然不是选定的答案?非常感谢,顺便说一句;)
【解决方案2】:

我用过

failed_when: not(promtool_version.stdout.find('1.5.2') != -1)

意味着 - 仅当先前注册的变量“promtool_version”不包含字符串“1.5.2”时才会失败。

【讨论】:

    【解决方案3】:

    在 ansible 2.3.0.0 中,以上答案均不适合我,但以下答案:

    when: variable1 | search("value")
    

    在 ansible 2.9 中,不赞成使用 ~ 连接进行变量替换:

    when: "variable1.find('v=' ~ value) == -1"
    

    http://jinja.pocoo.org/docs/dev/templates/#other-operators

    其他选项:

    when: "inventory_hostname in groups[sync_source]"
    

    【讨论】:

    • +1 我更喜欢这个答案。如果要检查 variable1 不包含“value”,也可以在“variable1”之前添加“not”。何时:不可变1 |搜索(“价值”)
    • 表示此方法将在 2.9 中弃用。而不是使用result|search 使用result is search
    • 按照其他解决方案的建议使用'"value" in variable1',不会触发警告。
    • 与 variable1 相比,使用 variable1.find 非常糟糕且不可读 |搜索(“”)
    【解决方案4】:

    此示例使用 regex_search 执行子字符串搜索。

    - name: make conditional variable
      command: "file -s /dev/xvdf"
      register: fsm_out
    
    - name: makefs
      command: touch "/tmp/condition_satisfied"
      when: fsm_out.stdout | regex_search(' data')
    

    ansible 版本:2.4.3.0

    【讨论】:

      【解决方案5】:

      来自 Ansible 2.5

      when: variable1 is search("value")
      

      【讨论】:

      • 很好,它在 ansible 2.9 中工作!如果我需要一个仅在变量不包含特定字符串时才有效的条件,那么正确的语法是什么?我在想这样的事情:when: variable1 is not search("value") 但不工作......
      • 你好,我是when: not variable1 is search("value")
      【解决方案6】:

      使用这个

      when: "{{ 'value' in variable1}}"

      而不是

      何时:“{{variable1}} 中的‘值’”

      也可以用于字符串比较

      当:“{{ variable1 == 'value' }}”

      【讨论】:

      【解决方案7】:

      一些答案​​不再像解释的那样起作用。

      目前这里有一些在 ansible 2.6.x 中对我有用的东西

       when: register_var.stdout is search('some_string')
      

      【讨论】:

        【解决方案8】:

        在 Ansible 2.9.2 版中:

        如果你的变量 variable1 被声明:

        when: "'value' in variable1"

        如果你注册了 variable1 那么:

        when: "'value' in variable1.stdout"

        【讨论】:

          【解决方案9】:

          这在 Ansible 2.9 中适用于我:

          variable1 = www.example.com. 
          variable2 = www.example.org. 
          
          when: ".com" in variable1
          

          当然:

          when: not ".com" in variable2
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-12-17
            • 1970-01-01
            • 1970-01-01
            • 2020-01-19
            • 1970-01-01
            • 2021-06-28
            • 2012-08-04
            • 1970-01-01
            相关资源
            最近更新 更多