Ansible无法使基于stdout_lines列表上的字符串搜索的任务失败

时间:2019-12-17 06:33:12

标签: ansible ansible-2.x ansible-inventory ansible-facts

团队

我的下面的任务应该基于输出中的字符串而失败,但并非如此。我想念什么? Validate all nodes have labels应该失败。


      - name: "list node labels"
        debug:
          var: nodeType_label_info.stdout_lines

      - name: "Validate all nodes have labels"
        debug:
          var: nodeType_label_info.stdout_lines
        failed_when: '"does not have" in {{ item }}'
        with_items: nodeType_label_info.stdout_lines
        #failed_when: nodeType_label_info.stdout_lines is not search('does not have')
TASK [: list node labels] ***************************************************************************************************************************************
Tuesday 17 December 2019  06:29:45 +0000 (0:00:01.449)       0:00:01.598 ******
ok: [localhost] => {
    "nodeType_label_info.stdout_lines": [
        "nodeTyp=test",
        "node2 does not have nodeType label"
    ]
}

TASK [: Validate all nodes have labels] *************************************************************************************************************************
Tuesday 17 December 2019  06:29:45 +0000 (0:00:00.210)       0:00:01.808 ******
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: "does not have" in {{ item }}

ok: [localhost] => (item=nodeType_label_info.stdout_lines) => {
    "ansible_loop_var": "item",
    "failed_when_result": false,
    "item": "nodeType_label_info.stdout_lines",
    "nodeType_label_info.stdout_lines": [
        "nodeTyp=test",
        "node1 does not have nodeType label"
    ]
}

1 个答案:

答案 0 :(得分:0)

自己修复。我不需要将值存储在var中,只需按以下所示检查failed_w

      - name: "Validate all nodes have labels"
        debug:
          msg: "label not found on all nodes"
        failed_when: '"does not have" in nodeType_label_info.stdout'