首页 > 解决方案 > 为什么即使条件正确,Ansible 也会跳过任务?

问题描述

下面是我的代码,我在其中查找文件 /home/xxx/logs/213079.txt 中的值并使条件为真或假。

  - name: register name
    shell: cat /home/xxx/213079.txt
    register: a213079

  - name: register output
    shell: cat /home/xxx/logs/213079.txt
    register: h213079

  - shell: echo {{ a213079.stdout }} Healthy >> /home/xxx/ABC/213079.txt
    when:
      - "'Serious' not in h213079.stdout"
      - "'Service' not in h213079.stdout"

  - shell: echo {{ a213079.stdout }} Faulty >> /home/xxx/ABC/213079.txt
    when:
      - "'Serious' in h213079.stdout"
      - "'Service' in h213079.stdout"

但是当我运行相同时,所有条件都在跳过。

TASK [tmp : register output] *******************************************************************************************************************************************
changed: [127.0.0.1]

TASK [tmp : shell] *****************************************************************************************************************************************************
skipping: [127.0.0.1]

TASK [tmp : shell] *****************************************************************************************************************************************************
skipping: [127.0.0.1]

TASK [tmp : shell] *****************************************************************************************************************************************************
skipping: [127.0.0.1]

TASK [tmp : shell] *****************************************************************************************************************************************************
skipping: [127.0.0.1]

有人可以帮忙吗。

标签: ansible

解决方案


因为我们不知道你得到的输出,所以这看起来很复杂,但如果你可以 grep 特定的词,我建议你使用类似下面的东西。

当:h213079.stdout =!“严重”和 h213079.stdout=!“服务”

何时:h213079.stdout ==“严重”和 h213079.stdout==“服务”


推荐阅读