How to loop through a json file records in ansible

0

I'm new in Ansible, just wandering how could I loop through an json records with ansible. I have a json file with the following data inside:

{
    "map": {
        "entry": [
            {
                "string": [
                    "6a032ae5-3eed-4d20-a9ef-ccbd88118c47"
                ]
            },
            {
                "string": [
                    "27617813-e268-4c94-b539-3550a3a8501a"
                ]
            },
            {
                "string": [
                    "b39b10fb-0538-42d7-96fc-553eee9c7ded"
                ]
            }
        ]
    }
}

and I have the following code in ansible to run through the json file:

- name: Initialise variables
  set_fact:
    NEW_CODE_TEMPLATES_IDS: "{{ lookup('file', 'code_template_map/codeTemplate.json') | json_query('map.entry[*].item') }}"
    
    
- name: code template id
  debug:
    var: NEW_CODE_TEMPLATES_IDS

I expecting the output will be like following:

6a032ae5-3eed-4d20-a9ef-ccbd88118c47
27617813-e268-4c94-b539-3550a3a8501a
b39b10fb-0538-42d7-96fc-553eee9c7ded

But for some reason this is doesn't seem working for me. doesn't anyone know why ?

ansible
2021-11-24 05:09:25
1

1

You are not converting the string returned by the lookup from JSON into a data structure (json_query() cannot be used with JSON strings, it expects parsed data structures), and json_query('map.entry[*].item') does not match the data you provided.

I would avoid json_query() and use standard Jinja features for this data manipulation:

"{{ (lookup('file', 'code_template_map/codeTemplate.json') | from_json).map.entry | map(attribute='string') | flatten }}"

If you insist on using json_query(), it would be:

"{{ (lookup('file', 'code_template_map/codeTemplate.json') | from_json) | json_query('map.entry[*].string[]') }}"
2021-11-24 06:21:42

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................