Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible shell task erroring out without proper message

Team,

I am trying to verify if sdd exists in mount command output. so when there is any, i am fine but when there is none, my task is simply failing instead of just telling me that no mounts exists. any hint how to tackle this? I don't want my task to fail but to report what is the state.

when status code is 0 am good but when status code is 1 am just seeing failure instead of a helpful message that mounds sdd don't exist.

 "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

output:

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"}

expected output:

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
like image 517
AhmFM Avatar asked Nov 26 '19 20:11

AhmFM


1 Answers

The error is telling you that there is no filter named failed. To check for a failed result in a conditional, use this instead:

when: lvp_mount is failed

Alternatively, to check for a successful result, use:

when: lvp_mount is succeeded
like image 102
Matt P Avatar answered Sep 28 '22 17:09

Matt P