如何解决无法从 ansible 任务输出读取结果字典中的 stderr
团队,我正在努力学习如何阅读结果。
我尝试在网上搜索但没有运气。基本上,我正在尝试根据 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%","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","cmd": "lsblk -p -l | grep /dev/nvme1n1","delta": "0:00:00.015661","end": "2021-05-05 20:08:07.375474","invocation": {
"module_args": {
"_raw_params": "lsblk -p -l | grep /dev/nvme1n1","item": [
{
"device": "/dev/sdg","partitions": [
{
"end": "20GiB","start": "1GiB","storage_class": "ssd-wkr "
},{
"end": "40GiB","number": 2,"start": "20GiB",{
"end": "40GiB","start": "2021-05-05 20:08:07.903902","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,"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,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,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 使用 jinja 语法: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) }}"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。