Reputation: 53
In Ansible, using the uri
module to retrieve some JSON, then output one of the fields using debug
.
- name: debug
debug:
msg: "{{ result.json.results[0].series[0].values }}"
The JSON data contains a key called values
and this seems to match the Python keyword values()
. So instead of returning the value for that key it's returning an object.
ok: [XXX] => {
"msg": "<built-in method values of dict object at 0x7fd131418280>"
}
Any idea how I can protect the call from being interpreted in this way?
Upvotes: 4
Views: 686
Reputation: 39119
What you can try is to access it via the square brackets notation rather:
- debug:
msg: "{{ result.json.results[0].series[0]['values'] }}"
If this still doesn't work, the .get(key[, default])
method of Python's dictionaries should comes in handy:
- debug:
msg: "{{ result.json.results[0].series[0].get('values') }}"
So indeed with:
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "{{ result.json.results[0].series[0].values }}"
vars:
result:
json:
results:
- series:
- values: foo
We get:
PLAY [all] **********************************************************************************************************
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"msg": "<built-in method values of dict object at 0x7fb3111e9cc0>"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
But with:
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "{{ result.json.results[0].series[0]['values'] }}"
vars:
result:
json:
results:
- series:
- values: foo
- hosts: all
gather_facts: no
tasks:
- debug:
msg: "{{ result.json.results[0].series[0].get('values') }}"
vars:
result:
json:
results:
- series:
- values: foo
You do get:
PLAY [all] **********************************************************************************************************
TASK [debug] ********************************************************************************************************
ok: [localhost] => {
"msg": "foo"
}
PLAY RECAP **********************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Upvotes: 4