0

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"}
AhmFM
  • 1,552
  • 3
  • 23
  • 53

1 Answers1

1

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
Alassane Ndiaye
  • 4,427
  • 1
  • 10
  • 19
  • You mean, I should add this in playbook and not role? – AhmFM Nov 06 '19 at 20:30
  • I don't have much experience writing roles so I am not sure if it's the right practice in those cases. Still, you can leave the tasks in the role, then call the role from your playbook using the `hosts` keyword as mentioned above. – Alassane Ndiaye Nov 06 '19 at 20:57
  • Or simpler, you can put everything inside a playbook if you don't need the role. – Alassane Ndiaye Nov 06 '19 at 20:58
  • when i use it in role/task/main.yaml i get syntax error. so not sure if its supported to use hosts in role. – AhmFM Nov 06 '19 at 21:43
  • You can't use `hosts` in roles. As I was saying in my first suggestion, you could leave the tasks in the role, and put the hosts part in a playbook. You can find out how to use roles here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#using-roles – Alassane Ndiaye Nov 06 '19 at 22:15