ansible shell任务在没有正确消息的情况下出错

时间:2019-11-26 20:11:48

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

团队

我正在尝试验证mount命令输出中是否存在sdd。所以当有的时候,我很好,但是当没有的时候,我的任务就是失败,而不仅仅是告诉我不存在任何挂载。任何提示如何解决这个问题?我不希望我的任务失败,而是要报告什么状态。

当状态码为0时很好,但是当状态码为1时只是看到失败,而不是一条不存在堆sdd的有用消息。

 "mount | grep sdd"
      - name: "Verify LVP Mounts sdd exists on CPU Nodes for mount_device"
        shell: "mount | grep sdd"
        register: lvp_mount
        ignore_errors: yes
        failed_when: False
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-cpu-node'] }}"
      - name: "Report status of mounts"
        fail:
          msg: |
            Mounts sdd not found
            Output of `mount | grep sdd`:
            {{ lvp_mount.stdout }}
            {{ lvp_mount.stderr }}
        when: lvp_mount | failed

输出:

fatal: [localhost]: FAILED! => {"msg": "The conditional check 'lvp_mount | failed' failed. The error was: template error while templating string: no filter named 'failed'. String: {% if lvp_mount | failed %} True {% else %} False {% endif %}\n\nThe error appears to be in '/k8s/baremetal/roles/maglev-services-pre-install-checks/tasks/main.yml': line 111, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n        with_items: \"{{ groups['kube-cpu-node'] }}\"\n      - name: \"Report status of mounts\"\n        ^ here\n"}

预期输出:

if lvp_mount.rc == 0
msg: mount sdd exists

if lvp_mount.rc == 1
msg: mount sdd does not exists

if lvp_mount.rc not in [0, 1]
msg: mount exec errir

1 个答案:

答案 0 :(得分:2)

该错误告诉您没有名为failed的过滤器。要在条件条件下检查失败的结果,请改用此方法:

when: lvp_mount is failed

或者,要检查是否成功,请使用:

when: lvp_mount is succeeded