【发布时间】:2019-08-20 00:20:10
【问题描述】:
以下是我的任务:
- name: Get pods
shell: kubectl -n kube-system get pods -o json | jq '.items[] | .metadata.name'
register: result
- debug:
var=result2
- debug:
msg: name is {{result.stdout['name']}}
我运行 playbook 时的输出是:
ok: [tester] => {
"result": {
"changed": true,
...
"stderr": "",
"stderr_lines": [],
"stdout": "{\"name\":\"nginx-79cdd9df6b-8xbpz\",\"namespace\":\"kube-system\"}"
"stdout_lines": [
"{\"name\":\"nginx-79cdd9df6b-8xbpz\",\"namespace\":\"kube-system\"}"
]
}
}
我想解析stdout 并获取输出的name。但是,使用result.stdout.name 或result.stdout['name'] 时调试阶段失败,并给出以下错误:
the error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'name'\n\nThe error appears to...
: line 25, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.
如何解析来自调试变量的 JSON 输出?
【问题讨论】:
标签: ansible ansible-facts