Issue with Ansible uri JSON parsing

AlanG

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?

β.εηοιτ.βε

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:

  • either
    - hosts: all
      gather_facts: no
    
      tasks:
        - debug: 
            msg: "{{ result.json.results[0].series[0]['values'] }}"
          vars:
              result:
                json:
                  results:
                    - series:
                        - values: foo
    
  • or
    - 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   

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related