【发布时间】:2021-02-06 21:29:16
【问题描述】:
我有一个简单的剧本,使用 curl 从 Vault 服务器获取一些数据。
tasks:
- name: role_id
shell: 'curl \
--header "X-Vault-Token: s.ddDblh8DpHkOu3IMGbwrM6Je" \
--cacert vault-ssl-cert.chained \
https://active.vault.service.consul:8200/v1/auth/approle/role/cpanel/role-id'
register: 'vault_role_id'
- name: test1
debug:
msg: "{{ vault_role_id.stdout }}"
输出是这样的:
TASK [test1] *********************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"auth": null,
"data": {
"role_id": "65d02c93-689c-eab1-31ca-9efb1c3e090e"
},
"lease_duration": 0,
"lease_id": "",
"renewable": false,
"request_id": "8bc03205-dcc2-e388-57ff-cdcaef84ef69",
"warnings": null,
"wrap_info": null
}
}
如果我正在访问第一级属性,一切正常,例如前面示例中的 .stdout。我需要更深层次的属性才能达到,比如vault_role_id.stdout.data.role_id。当我尝试这个时,它失败并出现以下错误:
"The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'data'\n\n
您有什么建议可以从这个对象层次结构的更深层次正确获取属性值吗?
【问题讨论】:
-
过滤curl的输出。例如 curl ... | jq '.' 见Display curl output in readable JSON format in Unix shell script.
-
我试过了,但没有更好的。在没有过滤的情况下,我也有 json 格式的输出,就像我在上面粘贴的那样。但是,由于某种原因,无法从对象结构中的 dipper 级别到达属性,然后是第一级,在我的示例中是 .stdout ...
标签: dictionary ansible