Home > database >  ansible json_query to with dicts containing key values as list
ansible json_query to with dicts containing key values as list

Time:11-19

I have a below value in a variable allcsv

"msg": [
    {
        "added_bundle_images": [
            "locuz.qe.pnq.local/eng-build/lzfirewallmod:v4.8.3-10"
        ],
        "index_image": "locuz.qe.pnq.local/eng-build/iib:297699",
        "locuz_version": "v4.8"
    },
    {
        "added_bundle_images": [
            "locuz.qe.pnq.local/eng-build/lzfirewallmod:v4.10.0-57"
        ],
        "index_image": "locuz.qe.pnq.local/eng-build/iib:297697",
        "locuz_version": "v4.9"
    },
    {
        "added_bundle_images": [
            "locuz.qe.pnq.local/eng-build/lzfirewallmod:v4.7.7-14"
        ],
        "index_image": "locuz.qe.pnq.local/eng-build/iib:297497",
        "locuz_version": "v4.7"
    },
    {
        "added_bundle_images": [
            "locuz.qe.pnq.local/eng-build/lzfirewallmod:v4.9.2-3"
        ],
        "index_image": "locuz.qe.pnq.local/eng-build/iib:297495",
        "locuz_version": "v4.9"
    }
]

I have a ansible variable which contains one of the values from added_bundle_images. for example i have a variable called lastcsv which contains value "locuz.qe.pnq.local/eng-build/lzfirewallmod:v4.7.7-14"

I am trying to write a json_query and also trying selectattr to get index_image if my key value matches "locuz.qe.pnq.local/eng-build/lzfirewallmod:v4.7.7-14"

- name: set my variable
  set_fact:
     lastcsv: "locuz.qe.pnq.local/eng-build/lzfirewallmod:v4.7.7-14"

- name: my debug2                                                                                                                                                        
  debug: msg="{{ allcsv | selectattr('added_bundle_images','equalto', [lastcsv])  | list }}"

The output i see is :

TASK [common : my debug2] ***************************************************************************************************************************************************
ok: [foo.example.com] => {
    "msg": []
}

Need help in constructing the query so that i get the index_image value when the added_bundle_image key has value "locuz.qe.pnq.local/eng-build/lzfirewallmod:v4.7.7-14"

CodePudding user response:

In allcsv, added_bundle_images is a list (denoted by []), so we can use contains match rather than equalto to see if the lastcsv element is in that list. Something like below:

- name: set my variable
  set_fact:
     lastcsv: "locuz.qe.pnq.local/eng-build/lzfirewallmod:v4.7.7-14"

- name: my debug2                                                                                                                                                        
  debug:
    msg: "{{ allcsv | selectattr('added_bundle_images', 'contains', lastcsv) | map(attribute='index_image') | list }}"

Produces:

TASK [my debug2] ********************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "locuz.qe.pnq.local/eng-build/iib:297497"
    ]
}
  • Related