-2

Team, I am trying to learn how to read results.

I tried searching online but no luck. Basically, I am trying to write a message based on when condition and it is failing as i might not be following how to intercept.

vars:

local_volume_mount_disks:
  - device: /dev/nvme2n1
    partitions:
      - number: 1

  - device: /dev/nvme1n1
    partitions:
      - number: 1

  - device: /dev/sdg
    partitions:
      - number: 1
      - number: 2


task

  - name: Check existence of all inventory defined devices on node
    shell: "lsblk -p -l | grep {{ item.0.device }}"
    loop:  "{{ local_volume_mount_disks|subelements('partitions') }}"
    register: device_exists
    ignore_errors: True
 
  - name: debug device_exists
    debug:
      var: device_exists
 
  - name: Inventory Device existence condition to continue
    fail:
      msg: "The command has failed"
    when: stderr != ""
    #when: item.stderr != ""
    #when: item.item.stderr != ""
    with_items: device_exists.results

sample output of registered variable

TASK [local_volume_mount : debug device_exists] ********************************
   Wednesday 05 May 2021  20:08:07 +0000 (0:00:01.050)       0:00:08.588 ********* 
   ok: [node1] => {
       "device_exists": {
           "changed": true,
           "msg": "All items completed",
           "results": [
               {
                   "ansible_loop_var": "item",
                   "changed": true,
                   "cmd": "lsblk -p -l | grep /dev/nvme2n1",
                   "delta": "0:00:00.018988",
                   "end": "2021-05-05 20:08:07.193247",
                   "failed": false,
                   "invocation": {
                       "module_args": {
                           "_raw_params": "lsblk -p -l | grep /dev/nvme2n1",
                           "_uses_shell": true,
                           "warn": true
                       }
                   },
                   "item": [
                       {
                           "device": "/dev/nvme2n1",
                           "partitions": [
                               {
                                   "end": "100%",
                                   "number": 1,
                                   "start": "0%",
                                   "storage_class": "ssd-wkr "
                               }
                           ]
                       },
                       {
                           "end": "100%",
                           "number": 1,
                           "start": "0%",
                           "storage_class": "ssd-wkr "
                       }
                   ],
                   "rc": 0,
                   "start": "2021-05-05 20:08:07.174259",
                   "stderr": "",
                   "stderr_lines": [],
                   "stdout": "/dev/nvme2n1   259:2    0 894.3G  0 disk  ",
                   "stdout_lines": [
                       "/dev/nvme2n1   259:2    0 894.3G  0 disk  "
                   ]
               },
               {
                   "ansible_loop_var": "item",
                   "changed": true,
                   "cmd": "lsblk -p -l | grep /dev/nvme1n1",
                   "delta": "0:00:00.015661",
                   "end": "2021-05-05 20:08:07.375474",
                   "failed": false,
                   "invocation": {
                       "module_args": {
                           "_raw_params": "lsblk -p -l | grep /dev/nvme1n1",
                           "_uses_shell": true,
                           "warn": true
                       }
                   },

                   "item": [
                       {
                           "device": "/dev/sdg",
                           "partitions": [
                               {
                                   "end": "20GiB",
                                   "number": 1,
                                   "start": "1GiB",
                                   "storage_class": "ssd-wkr "
                               },
                               {
                                   "end": "40GiB",
                                   "number": 2,
                                   "start": "20GiB",
                                   "storage_class": "ssd-wkr "
                               }
                           ]
                       },
                       {
                           "end": "40GiB",
                           "number": 2,
                           "start": "20GiB",
                           "storage_class": "ssd-wkr "
                       }
                   ],
                   "rc": 0,
                   "start": "2021-05-05 20:08:07.903902",
                   "stderr": "",
                   "stderr_lines": [],
                   "stdout": "/dev/sdg         8:96   0   9.1T  0 disk  ",
                   "stdout_lines": [
                       "/dev/sdg         8:96   0   9.1T  0 disk  "
                   ]
               }
           ]
       }
   }

error

TASK [local_volume_mount : Inventory Device existence condition to continue] ***
 Wednesday 05 May 2021  20:08:08 +0000 (0:00:00.053)       0:00:08.642 ********* 
  fatal: [node1]: FAILED! => {"msg": "The conditional check 'stderr != \"\"' failed. The error was: error while evaluating conditional (stderr != \"\"): 'stderr' is undefined\n\nThe error appears to be in '/ansible-managed/jenkins-slave/slave0/workspace/run_ansible_playbook/k8s/baremetal/roles/local_volume_mount/tasks/main.yml': line 11, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Inventory Device existence condition to continue\n  ^ here\n"}


TASK [local_volume_mount : debug device_exists] ********************************
   Wednesday 05 May 2021  20:08:07 +0000 (0:00:01.050)       0:00:08.588 ********* 
   ok: [node1] => {
       "device_exists": {
           "changed": true,
           "msg": "All items completed",
           "results": [
               {
                   "ansible_loop_var": "item",
                   "changed": true,
                   "cmd": "lsblk -p -l | grep /dev/nvme2n1",
                   "delta": "0:00:00.018988",
                   "end": "2021-05-05 20:08:07.193247",
                   "failed": false,
                   "invocation": {
                       "module_args": {
                           "_raw_params": "lsblk -p -l | grep /dev/nvme2n1",
                           "_uses_shell": true,
                           "warn": true
                       }
                   },
                   "item": [
                       {
                           "device": "/dev/nvme2n1",
                           "partitions": [
                               {
                                   "end": "100%",
                                   "number": 1,
                                   "start": "0%",
                                   "storage_class": "ssd-wkr "
                               }
                           ]
                       },
                       {
                           "end": "100%",
                           "number": 1,
                           "start": "0%",
                           "storage_class": "ssd-wkr "
                       }
                   ],
                   "rc": 0,
                   "start": "2021-05-05 20:08:07.174259",
                   "stderr": "",
                   "stderr_lines": [],
                   "stdout": "/dev/nvme2n1   259:2    0 894.3G  0 disk  ",
                   "stdout_lines": [
                       "/dev/nvme2n1   259:2    0 894.3G  0 disk  "
                   ]
               },
               {
                   "ansible_loop_var": "item",
                   "changed": true,
                   "cmd": "lsblk -p -l | grep /dev/nvme1n1",
                   "delta": "0:00:00.015661",
                   "end": "2021-05-05 20:08:07.375474",
                   "failed": false,
                   "invocation": {
                       "module_args": {
                           "_raw_params": "lsblk -p -l | grep /dev/nvme1n1",
                           "_uses_shell": true,
                           "warn": true
                       }
                   },

                   "item": [
                       {
                           "device": "/dev/sdg",
                           "partitions": [
                               {
                                   "end": "20GiB",
                                   "number": 1,
                                   "start": "1GiB",
                                   "storage_class": "ssd-wkr "
                               },
                               {
                                   "end": "40GiB",
                                   "number": 2,
                                   "start": "20GiB",
                                   "storage_class": "ssd-wkr "
                               }
                           ]
                       },
                       {
                           "end": "40GiB",
                           "number": 2,
                           "start": "20GiB",
                           "storage_class": "ssd-wkr "
                       }
                   ],
                   "rc": 0,
                   "start": "2021-05-05 20:08:07.903902",
                   "stderr": "",
                   "stderr_lines": [],
                   "stdout": "/dev/sdg         8:96   0   9.1T  0 disk  ",
                   "stdout_lines": [
                       "/dev/sdg         8:96   0   9.1T  0 disk  "
                   ]
               }
           ]
       }
   }
   

output


TASK [local_volume_mount : Inventory Device existence condition to continue] ***
 Wednesday 05 May 2021  20:08:08 +0000 (0:00:00.053)       0:00:08.642 ********* 
  fatal: [mlwkr1k8s0301d]: FAILED! => {"msg": "The conditional check 'stderr != \"\"' failed. The error was: error while evaluating conditional (stderr != \"\"): 'stderr' is undefined\n\nThe error appears to be in '/ansible-managed/jenkins-slave/slave0/workspace/run_ansible_playbook/k8s/baremetal/roles/local_volume_mount/tasks/main.yml': line 11, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Inventory Device existence condition to continue\n  ^ here\n"}

with_items: item.stderr != ""

fatal: [node1]: FAILED! => {"msg": "The conditional check 'item.stderr != \"\"' failed. The error was: error while evaluating conditional (item.stderr != \"\"): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stderr'\n\nThe error appears to be in '/ansible-managed/jenkins-slave/slave0/workspace/run_ansible_playbook/k8s/baremetal/roles/local_volume_mount/tasks/main.yml': line 6, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Inventory Device existence condition to continue\n  ^ here\n"}
AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • I supposed you did try when: item.stderr != "" (which is the right syntax)? Also, have you tried with "item.stderr is defined" before? – Sebcworks May 05 '21 at 20:42
  • `item.stderr`. rather than searching the web or asking questions with a full wall of data you could have stripped down, you would really benefit from reading your error messages in depth and play around with debug to explore your variable content. – Zeitounator May 05 '21 at 20:52
  • with (item.stderr != \"\"): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stderr'` i get error as no attribute. yes i tried all options i could. – AhmFM May 05 '21 at 21:04
  • and when i use ` when: item.stderr is defined` it just skips the task though i tested with a non-existent device. – AhmFM May 05 '21 at 21:20

1 Answers1

1

There is an error in the syntax (didn't seen it at 1st sight) for the loop.

You should use the jinja syntax for you with_items: with_items: "{{device_exists.results }}"

See https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-items:

- name: with_items
  ansible.builtin.debug:
    msg: "{{ item }}"
  with_items: "{{ items }}"

- name: with_items -> loop
  ansible.builtin.debug:
    msg: "{{ item }}"
  loop: "{{ items|flatten(levels=1) }}"
Sebcworks
  • 139
  • 2
  • 7
  • Appreciate for taking care to understand my question and also caught what my eyes did not. am accepting answer because my error is gone. however, my task is just being skipped which I think is a different issue. – AhmFM May 06 '21 at 02:16