You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用json_query解析Ansible输出,解决字段返回null问题

问题排查与修改方案

错误原因

  • 层级路径缺失:proxy_id字段嵌套在auto_key属性下,你编写的JMES查询遗漏了auto_key层级,导致无法定位到目标字段返回null
  • 数组取值逻辑错误:proxy_id.entry是数组结构,无法直接通过.local/.remote批量获取数组内元素的属性,需要先通过[*]遍历数组提取所有对应字段值

修复后代码

- debug: msg="{{output3.msg|json_query(jmesquery)}}"     
  vars:
      jmesquery: "[*].{name: name, local: join(',', auto_key.proxy_id.entry[*].local), remote: join(',', auto_key.proxy_id.entry[*].remote), tunnel: tunnel_interface}" 

说明

JMES查询内置的join函数会把提取到的网段数组拼接为逗号分隔的字符串,完全匹配你期望的输出格式。如果后续需要保留网段数组格式,去掉join函数、保留auto_key.proxy_id.entry[*].local即可。

内容的提问来源于stack exchange,提问作者netv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 18:15:04