Search code examples
ansibleansible-2.xansible-inventoryansible-facts

Ansible mount module to just check state and not report status


Team, I am writing a validation task that is supposed to just check if a mount exists or not and report its state from output. so my task is below but it fails and am not sure how to handle it. any hint what adjustments do i need to make?

      - name: "Verify LVP Mounts on CPU Nodes for mount_device"
        shell: "mount | grep sdd"
        register: lvp_mount
        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-cpu-node'] }}"
        #failed_when: lvp_mount.rc != 0
        #ignore_errors: yes

#      - debug:
#          var: lvp_mount

      - 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

  changed: [localhost -> ] => (item=hostA)
   [WARNING]: Consider using the mount module rather than running 'mount'.  If you
   need to use command because mount is insufficient you can add 'warn: false' to
   this command task or set 'command_warnings=False' in ansible.cfg to get rid of
   this message.

  failed: [localhost -> hostA.test.net] (item=hostA) => {"ansible_loop_var": "item", "changed": true, "cmd": "mount | grep sdd", "delta": "0:00:00.009284", "end": "2019-11-06 18:22:56.138007", "failed_when_result": true, "item": "hostA", "msg": "non-zero return code", "rc": 1, "start": "2019-11-06 18:22:56.128723", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
  ...ignoring

  TASK [services-pre-install-checks : Report status of mounts] ************

  fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to be in '/home/run_ansible_playbook/k8s/baremetal/roles/services-pre-install-checks/tasks/main.yml': line 265, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n      - name: \"Report status of mounts\"\n        ^ here\n"}

Solution

  • Your task "Verify LVP Mounts on CPU Nodes for mount_device" is a loop so the register behavior is modified as specified in the documentation. You can access the various outputs with lvp_mount.results.X.stdout where X is the index.

    There is a cleaner way to write your script however. More specifically using:

    delegate_to: "{{ item }}"
    with_items: "{{ groups['kube-cpu-node'] }}"
    

    is bad practice. You can accomplish your desired outcome at the play level.

    For example:

    - hosts: kube-cpu-node # allows you to iterate over all hosts in kube-cpu-node group
      tasks:
        - name: "Verify LVP Mounts on CPU Nodes for mount_device"
          shell: "mount | grep sdd"
          register: lvp_mount
          ignore_errors: yes
          # notice there is no loop here
    
        - name: "Report status of mounts"
          fail:
            msg: |
              Mounts sdd not found
              Output of `mount | grep sdd`:
              {{ lvp_mount.stdout }} # no loop so you can use lvp_mount.stdout
              {{ lvp_mount.stderr }} # no loop so you can use lvp_mount.stderr
          when: lvp_mount | failed