*ansible 2.9.4*

Let's assume the following list of dictionaries:

list:
    -   key1:
        - 'abc'
        - 'def'
        key2: 'ghi'
        key3: 'jkl'
    -   key1:
        - 'mno'
        - 'pqr'
        key3: 'stu'
        key4: 'dfg'
    -   key1:
        - 'vwx'
        - 'yza'
        key3: 'okl'
        key4: 'azel'



The goal is threefold:

   - to extract only the records which match a regex criteria over one of 
   the keys, for instance if "key3" contains a 'k'.
   - to keep only a selection of keys, for instance only "key1", "key3" and 
   "key4", which may not always be present
   - to be as efficient as possible with thousands of records

In this case, we expect the resulting list:
result_list:
    -   key1:
        - 'abc'
        - 'def'
        key3: 'jkl'
    -   key1:
        - 'vwx'
        - 'yza'
        key3: 'okl'
        key4: 'azel'


The difficulty I'm experiencing concerns "key1" as a list. 

My solution works without considering "key1":

- name: Filtering a complex list of dictionaries into another list of 
dictionaries
  vars:
        "list": [
            {
                "key1": [
                    "abc",
                    "def"
                ],
                "key2": "ghi",
                "key3": "jkl"
            },
            {
                "key1": [
                    "mno",
                    "pqr"
                ],
                "key3": "stu",
                "key4": "dfg"
            },
            {
                "key1": [
                    "vwx",
                    "yza"
                ],
                "key3": "okl",
                "key4": "azel"
            }
        ]
  set_fact:
        result_list: "{{ result_list|default([]) + [ {'key3': item.key3, 
'key4': item.key4|default('')} ] }}"
  loop: "{{ list }}"
  when: item.key3 | regex_search('(^.*k.*$)')


leads to:

TASK [yang : Filtering a complex list of dictionaries into another list of 
dictionaries] 
*********************************************************************
task path: test.yml:133
<172.16.136.116> attempting to start connection
<172.16.136.116> using connection plugin network_cli
<172.16.136.116> found existing local domain socket, using it!
<172.16.136.116> updating play_context for connection
<172.16.136.116> 
<172.16.136.116> local domain socket path is .ansible/pc/521e859c25
ok: [TEST] => (item={'key1': ['abc', 'def'], 'key2': 'ghi', 'key3': 'jkl'}) 
=> {
    "ansible_facts": {
        "result_list": [
            {
                "key3": "jkl",
                "key4": ""
            }
        ]
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": {
        "key1": [
            "abc",
            "def"
        ],
        "key2": "ghi",
        "key3": "jkl"
    }
}
skipping: [TEST] => (item={'key1': ['mno', 'pqr'], 'key3': 'stu', 'key4': 
'dfg'})  => {
    "ansible_loop_var": "item",
    "changed": false,
    "item": {
        "key1": [
            "mno",
            "pqr"
        ],
        "key3": "stu",
        "key4": "dfg"
    },
    "skip_reason": "Conditional result was False"
}
ok: [TEST] => (item={'key1': ['vwx', 'yza'], 'key3': 'okl', 'key4': 'azel'}) 
=> {
    "ansible_facts": {
        "result_list": [
            {
                "key3": "jkl",
                "key4": ""
            },
            {
                "key3": "okl",
                "key4": "azel"
            }
        ]
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": {
        "key1": [
            "vwx",
            "yza"
        ],
        "key3": "okl",
        "key4": "azel"
    }
}


How can we insert "*key1*" in the picture?

Also, when the list contains thousands of records, it may be less compute 
intensive to use ```*json_query*```, but I don't know how to use it in this 
context.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/86948dc2-7abd-4c3c-8012-ea89e34a8969%40googlegroups.com.

Reply via email to