Search code examples
ansiblejson-query

Getting "The error was: AttributeError: 'module' object has no attribute 'exceptions'" while using json_query


I'm trying to extract key, value from list of dictionary using json_query filter but getting an error. Below is the playbook

  1
  2 - name: test
  3   hosts: localhost
  4   vars:
  5           testList: [ { "key1": "value11", "key2": "value12", "key3": "value13" },
  6                       { "key1": "value21", "key2": "value22", "key3": "value23" },
  7                       { "key1": "value31", "key2": "value32", "key3": "value33" } ]
  8   tasks:
  9        - name: Getting keys
 10          debug:
 11            msg: "{{ testList | json_query('[*].{key1: key1 , key2: key2}')}}"

Here is the output

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: Could not match supplied host pattern, ignoring: all

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [test] ****************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************
ok: [localhost]

TASK [Getting keys] ********************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'module' object has no attribute 'exceptions'
fatal: [localhost]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}


PLAY RECAP *****************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1

With a single key it's working fine "{{ testList | json_query('[*].key1')}}"

Referring the document http://docs.ansible.com/ansible/latest/playbooks_filters.html#json-query-filter

Is there any other filter to get multiple key, value from list of dictionaries?


Solution

  • Upgrade to the latest Ansible version.

    This error happens for example on 2.1.3.0 (although you might be using a different version judging from the phrase in one of the warnings).

    Here is the result on current version 2.4.1.0:

    TASK [Getting keys] *****************************************************************************************************
    Unaltered: {'msg': [{'key2': u'value12', 'key1': u'value11'}, {'key2': u'value22', 'key1': u'value21'}, {'key2': u'value32', 'key1': u'value31'}]}
    ok: [localhost] => {
        "msg": [
            {
                "key1": "value11",
                "key2": "value12"
            },
            {
                "key1": "value21",
                "key2": "value22"
            },
            {
                "key1": "value31",
                "key2": "value32"
            }
        ]
    }