Flattening and filtering a complex structure in ansible - dict of list of dict

前端 未结 1 1841
北恋
北恋 2020-12-21 07:50

I have data that is represented in this manner:

 {
    \"key1\": [{
      \"name\": \"some name1\",
      \"index\": \"some idx1\"
    },
    {
      \"name\         


        
相关标签:
1条回答
  • 2020-12-21 07:55

    Ansible has rather limited tools to work with keys in dictionaries (see this answer).

    But dictsort filter can be handy sometimes, as in your case.
    It converts dict into list and you can use json_query to process it.

    Here's a task to fetch root key by index name:

    - debug:
        msg: "key is '{{ list_with_keys | json_query(qry) }}' for index {{ item }}"
      vars:
        list_with_keys: "{{ dict1 | dictsort | to_json | from_json }}"
        qry: "[?contains(([1] | [].index),`{{ item }}`)][] | [0]"
      with_items:
        - some idx5
        - some idx3
    

    where dict1 is taken from your example.

    0 讨论(0)
提交回复
热议问题