条件或字符串比较在块中不起作用

Conditional or on string comparison not working in block

我有一个带有块的剧本,我正在像这样对块进行字符串比较。假设我的 inventory_hostname 是“hello-test.com”,换句话说,应该跳过整个块。

  - name: Do cool stuff
    block:
      
      - name: Some tasks
        set_fact: feeder="feeder.stuff.com"

      
    when:  ("'hello-prod.com' in inventory_hostname")

如果我运行像上面那样,它会跳过它。但是,如果我 运行 它带有一个或,如下所示,它不会跳过它,即使 inventory_hostname 不匹配任何一个比较

  - name: Do cool stuff
    block:
      
      - name: Some tasks
        set_fact: feeder="feeder.stuff.com"

      
    when:  ("'hello-prod.com' in inventory_hostname") or ("'hello-cool-prod.com' in inventory_hostname")

我从来没有在 when 语句中使用过 or before,我错过了什么?

您的 when: 子句实际上是在乱用引号,我什至很惊讶它没有引发错误。正确的语法是:

    when: "'hello-prod.com' in inventory_hostname or 'hello-cool-prod.com' in inventory_hostname"

同时,您可以使用以下方法缩短此条件并去掉封闭的双引号

    when: inventory_hostname is regex('hello(-cool)?-prod.com')