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

无法从ansible任务输出读取结果字典中的stderr,ansible,ansible-2.x,ansible-facts,Ansible,Ansible 2.x,Ansible Facts,团队,我正在努力学习如何阅读结果 我试着在网上搜索,但没有成功。基本上,我正在尝试根据when条件编写消息,但失败了,因为我可能没有遵循如何拦截 变量: 任务 注册变量的样本输出 TASK [local_volume_mount : debug device_exists] ******************************** Wednesday 05 May 2021 20:08:07 +0000 (0:00:01.050) 0:00:08.588 ******

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

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

变量:

任务

注册变量的样本输出

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"}
与_items:item.stderr!=“”


循环的语法有错误(第一眼看到时没有看到)

您应该将jinja语法用于\u项:
用于\u项:“{{device\u exists.results}}”

见:


我想你在:item.stderr!=“”(哪种语法正确)?另外,您以前是否尝试过“item.stderr已定义”?
item.stderr
。与搜索web或用一整堵墙的数据提问相比,深入阅读错误消息并使用debug探索可变内容会让您受益匪浅。with(item.stderr!=\“\”):'ansible.utils.unsafe_proxy.AnsibleUnsafeText object'没有属性'stderr'`我得到的错误是没有属性。是的,我尝试了所有可能的选项。当我使用'when:item.stderr is defined'时,它只是跳过了任务,尽管我使用了一个不存在的设备进行了测试。感谢您仔细理解我的问题,也注意到了我的眼睛没有看到的东西。我接受答案,因为我的错误已经消失了。然而,我的任务只是被跳过,我认为这是一个不同的问题。
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"}
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"}
- 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) }}"