无法从可靠的任务输出中读取结果字典中的 stderr

Unable to read stderr in results dictionary from ansible task output

团队,我正在努力学习如何阅读结果。

我尝试在线搜索但没有成功。基本上,我正在尝试根据 when 条件编写一条消息,但它失败了,因为我可能没有遵循如何拦截。

变量:

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

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

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


任务

  - 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

注册变量的示例输出

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

错误

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

输出


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

循环的语法错误(第一眼没看出来)。

你应该使用适合你的 jinja 语法 with_items: with_items: "{{device_exists.results }}"

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