Looping through JSON URI responce

Hi All,

I am new to Ansible, but do have some coding background.
I an retrieving a 2 node list of items from a URI.
One is the name of the item.
The second is the date it was created.

I need to loop through the list evaluate the date created and then take action based on the results.

My problem is that I can get the JSON into an ansible dictionary but I can not loop through it and access the individual elements as I would expect to be able to.

The raw JSON back from the URI is as follows

        "failed": false,
        "json": {
            "created": "2024-09-23T12:56:56.551Z",
            "files": [
                {
                    "folder": false,
                    "lastModified": "2024-09-06T18:06:30.282Z",
                    "sha1": "88c6c34c475b4e413be69db71c7cdf3ececf0a7a",
                    "sha2": "2b053c09729cc69507dc46fcaff8e1527e0e92f0144ff42ec3f6ea4e3d914092",
                    "size": 1393787,
                    "uri": "/labforge_backup_2024-09-06.zip"
                },
                {
                    "folder": false,
                    "lastModified": "2024-09-10T21:34:51.121Z",
                    "sha1": "367b6a6060678c4d8330eab5f9b97cb379cb3d18",
                    "sha2": "78c70eb95ed4abe2a13eb10745271dda5bd8af18ee710ff7a68497c10214962b",
                    "size": 5245231,
                    "uri": "/labforge_labforge-k2_backup_2024-09-10.zip"
                },
                {
                    "folder": false,
                    "lastModified": "2024-09-13T14:23:36.053Z",
                    "sha1": "903513799f82086cbb031b110ef46a11e89fcac5",
                    "sha2": "36ef9a3d62228f39a5c6e9b3f5b080ddc6918bb50b199d291fb3947591ef6a64",
                    "size": 28521181,
                    "uri": "/labforge_labforge-k2_backup_2024-09-13.zip"
                },
                {
                    "folder": false,
                    "lastModified": "2024-09-17T04:31:47.248Z",
                    "sha1": "b2968ade244f3533bd95e05de1bdfb016a4e49c6",
                    "sha2": "9644d914c730ebae5ab952cd13d69e536002adb93a11cac73728c91538ca662c",
                    "size": 39670339,
                    "uri": "/labforge_labforge-k2_backup_2024-09-17.zip"
                },
                {
                    "folder": false,
                    "lastModified": "2024-09-19T04:31:45.761Z",
                    "sha1": "9a1d2d34ef0cbff351a3bc7fc74926e61014a1e5",
                    "sha2": "7dd96b9d2770d11117413311b8ed38a9eef3ce0146f346ec9e5e0ea6290d0c47",
                    "size": 52770555,
                    "uri": "/labforge_labforge-k2_backup_2024-09-19.zip"
                },
                {
                    "folder": false,
                    "lastModified": "2024-09-21T04:31:46.750Z",
                    "sha1": "acbfb9ef3f5586da0832b5724634ed8feeb7820c",
                    "sha2": "a0d3eb869245993260c1d7f4fdd97f2313cc362d078bfb14801d2c3055b87e88",
                    "size": 71471404,
                    "uri": "/labforge_labforge-k2_backup_2024-09-21.zip"
                },
                {
                    "folder": false,
                    "lastModified": "2024-09-23T04:31:45.219Z",
                    "sha1": "6f2a24f7c6b9d338d2cb7e46c3c8f0ed792337f8",
                    "sha2": "9bc31457baa657c3589393e6631bb150028bc229a769f2309cd4dff648b39cd3",
                    "size": 75496966,
                    "uri": "/labforge_labforge-k2_backup_2024-09-23.zip"
                }
            ],
            "uri": "https://host.domain.com/users/labforge/files"
        },

My dictionary

    - name: Create json Files Dictionary from results
      set_fact:
       files: "{{ backup_files_results.json }}"

My printout of the dictionary

    - name: Print out files Dictionary
      debug:
       var: files.files

Output

 {
    "files.files": [
        {
            "folder": false,
            "lastModified": "2024-09-06T18:06:30.282Z",
            "sha1": "88c6c34c475b4e413be69db71c7cdf3ececf0a7a",
            "sha2": "2b053c09729cc69507dc46fcaff8e1527e0e92f0144ff42ec3f6ea4e3d914092",
            "size": 1393787,
            "uri": "/labforge_backup_2024-09-06.zip"
        },
        {
            "folder": false,
            "lastModified": "2024-09-10T21:34:51.121Z",
            "sha1": "367b6a6060678c4d8330eab5f9b97cb379cb3d18",
            "sha2": "78c70eb95ed4abe2a13eb10745271dda5bd8af18ee710ff7a68497c10214962b",
            "size": 5245231,
            "uri": "/labforge_labforge-k2_backup_2024-09-10.zip"
        },
        {
            "folder": false,
            "lastModified": "2024-09-13T14:23:36.053Z",
            "sha1": "903513799f82086cbb031b110ef46a11e89fcac5",
            "sha2": "36ef9a3d62228f39a5c6e9b3f5b080ddc6918bb50b199d291fb3947591ef6a64",
            "size": 28521181,
            "uri": "/labforge_labforge-k2_backup_2024-09-13.zip"
        },
        {
            "folder": false,
            "lastModified": "2024-09-17T04:31:47.248Z",
            "sha1": "b2968ade244f3533bd95e05de1bdfb016a4e49c6",
            "sha2": "9644d914c730ebae5ab952cd13d69e536002adb93a11cac73728c91538ca662c",
            "size": 39670339,
            "uri": "/labforge_labforge-k2_backup_2024-09-17.zip"
        },
        {
            "folder": false,
            "lastModified": "2024-09-19T04:31:45.761Z",
            "sha1": "9a1d2d34ef0cbff351a3bc7fc74926e61014a1e5",
            "sha2": "7dd96b9d2770d11117413311b8ed38a9eef3ce0146f346ec9e5e0ea6290d0c47",
            "size": 52770555,
            "uri": "/labforge_labforge-k2_backup_2024-09-19.zip"
        },
        {
            "folder": false,
            "lastModified": "2024-09-21T04:31:46.750Z",
            "sha1": "acbfb9ef3f5586da0832b5724634ed8feeb7820c",
            "sha2": "a0d3eb869245993260c1d7f4fdd97f2313cc362d078bfb14801d2c3055b87e88",
            "size": 71471404,
            "uri": "/labforge_labforge-k2_backup_2024-09-21.zip"
        },
        {
            "folder": false,
            "lastModified": "2024-09-23T04:31:45.219Z",
            "sha1": "6f2a24f7c6b9d338d2cb7e46c3c8f0ed792337f8",
            "sha2": "9bc31457baa657c3589393e6631bb150028bc229a769f2309cd4dff648b39cd3",
            "size": 75496966,
            "uri": "/labforge_labforge-k2_backup_2024-09-23.zip"
        }
    ]
}

To me, this looks like ONE item rather than multiple items as I need it to be.

I have managed to get it into a list as well with:

   - name: Create dictionary of Bakcup file name and backup file date
      set_fact: 
        backup_file_names: "{{ backup_files_results | json_query(jmesquery) }}"
      vars:
        jmesquery: '*.files[*].{uri: uri, lastModified: lastModified}'

This is the data.

 [
        {
            "lastModified": "2024-09-06T18:06:30.282Z",
            "uri": "/labforge_backup_2024-09-06.zip"
        },
        {
            "lastModified": "2024-09-10T21:34:51.121Z",
            "uri": "/labforge_labforge-k2_backup_2024-09-10.zip"
        },
        {
            "lastModified": "2024-09-13T14:23:36.053Z",
            "uri": "/labforge_labforge-k2_backup_2024-09-13.zip"
        },
        {
            "lastModified": "2024-09-17T04:31:47.248Z",
            "uri": "/labforge_labforge-k2_backup_2024-09-17.zip"
        },
        {
            "lastModified": "2024-09-19T04:31:45.761Z",
            "uri": "/labforge_labforge-k2_backup_2024-09-19.zip"
        },
        {
            "lastModified": "2024-09-21T04:31:46.750Z",
            "uri": "/labforge_labforge-k2_backup_2024-09-21.zip"
        },
        {
            "lastModified": "2024-09-23T04:31:45.219Z",
            "uri": "/labforge_labforge-k2_backup_2024-09-23.zip"
        }
    ]

But NONE of my with_items: or loops allow me to access each set of items to work with.

Can anyone provide some suggestions or examples for looping through this list and working with both pieces of cate IE the URI and the lastModified?

You haven’t actually shown what you tried so it’s hard to say where you’ve gone wrong, but you seem to be overcomplicating things in your head.

It returns a list, so all you have to do is loop over that list.

- debug:
    msg: File {{ item.uri }} was last modified {{ item.lastModified }}
  loop: "{{ backup_files_results.json.files }}"`

In the future, try to mark preformatted text in your posts with code blocks to keep them readable. Use triple backticks (```) to start and end a block; if it’s in a specific syntax you can also specify that as part of the opening delimiter. For instance, this is what I’ve typed above:

```yaml
- debug:
    msg: File {{ item.uri }} was last modified {{ item.lastModified }}
  loop: "{{ backup_files_results.json.files }}"`
```

I can not thank you enough!!!
I have been beating my head against the wall for way too long on this one :slight_smile:

I am adding the below in case others need help with data /date manipulation:

- name: Print number of days since backup format 1 with When condition
      debug:
        msg: > 
          'File: {{ item.uri }} 
          LastModified: {{ item.lastModified[:10] }} 
          CurrentDate: {{ ansible_date_time.date }} 
          Todatetime: {{ item.lastModified[:10] |to_datetime('%Y-%m-%d') }} 
          NumberOfDays: {{ ((ansible_date_time.date | to_datetime('%Y-%m-%d')).date() - 
          (item.lastModified[:10]|to_datetime('%Y-%m-%d')).date()).days }}'
      loop: "{{ backup_files_results.json.files }}"
      when: 
          (((ansible_date_time.date | to_datetime('%Y-%m-%d')).date() - (item.lastModified[:10]|to_datetime('%Y-%m-%d')).date()).days) >7
TASK [Print number of days since backup format 1 with When condition] **************************************************
ok: [localhost] => (item={'uri': '/labforge_backup_2024-09-06.zip', 'size': 1393787, 'lastModified': '2024-09-06T18:06:30.282Z', 'folder': False, 'sha1': '88c6c34c475b4e413be69db71c7cdf3ececf0a7a', 'sha2': '2b053c09729cc69507dc46fcaff8e1527e0e92f0144ff42ec3f6ea4e3d914092'}) => {
    "msg": "'File: /labforge_backup_2024-09-06.zip  LastModified: 2024-09-06  CurrentDate: 2024-09-23  Todatetime: 2024-09-06 00:00:00  NumberOfDays: 17'\n"
}
ok: [localhost] => (item={'uri': '/labforge_labforge-k2_backup_2024-09-10.zip', 'size': 5245231, 'lastModified': '2024-09-10T21:34:51.121Z', 'folder': False, 'sha1': '367b6a6060678c4d8330eab5f9b97cb379cb3d18', 'sha2': '78c70eb95ed4abe2a13eb10745271dda5bd8af18ee710ff7a68497c10214962b'}) => {
    "msg": "'File: /labforge_labforge-k2_backup_2024-09-10.zip  LastModified: 2024-09-10  CurrentDate: 2024-09-23  Todatetime: 2024-09-10 00:00:00  NumberOfDays: 13'\n"
}
ok: [localhost] => (item={'uri': '/labforge_labforge-k2_backup_2024-09-13.zip', 'size': 28521181, 'lastModified': '2024-09-13T14:23:36.053Z', 'folder': False, 'sha1': '903513799f82086cbb031b110ef46a11e89fcac5', 'sha2': '36ef9a3d62228f39a5c6e9b3f5b080ddc6918bb50b199d291fb3947591ef6a64'}) => {
    "msg": "'File: /labforge_labforge-k2_backup_2024-09-13.zip  LastModified: 2024-09-13  CurrentDate: 2024-09-23  Todatetime: 2024-09-13 00:00:00  NumberOfDays: 10'\n"
}
skipping: [localhost] => (item={'uri': '/labforge_labforge-k2_backup_2024-09-17.zip', 'size': 39670339, 'lastModified': '2024-09-17T04:31:47.248Z', 'folder': False, 'sha1': 'b2968ade244f3533bd95e05de1bdfb016a4e49c6', 'sha2': '9644d914c730ebae5ab952cd13d69e536002adb93a11cac73728c91538ca662c'})
skipping: [localhost] => (item={'uri': '/labforge_labforge-k2_backup_2024-09-19.zip', 'size': 52770555, 'lastModified': '2024-09-19T04:31:45.761Z', 'folder': False, 'sha1': '9a1d2d34ef0cbff351a3bc7fc74926e61014a1e5', 'sha2': '7dd96b9d2770d11117413311b8ed38a9eef3ce0146f346ec9e5e0ea6290d0c47'})
skipping: [localhost] => (item={'uri': '/labforge_labforge-k2_backup_2024-09-21.zip', 'size': 71471404, 'lastModified': '2024-09-21T04:31:46.750Z', 'folder': False, 'sha1': 'acbfb9ef3f5586da0832b5724634ed8feeb7820c', 'sha2': 'a0d3eb869245993260c1d7f4fdd97f2313cc362d078bfb14801d2c3055b87e88'})
skipping: [localhost] => (item={'uri': '/labforge_labforge-k2_backup_2024-09-23.zip', 'size': 75496966, 'lastModified': '2024-09-23T04:31:45.219Z', 'folder': False, 'sha1': '6f2a24f7c6b9d338d2cb7e46c3c8f0ed792337f8', 'sha2': '9bc31457baa657c3589393e6631bb150028bc229a769f2309cd4dff648b39cd3'})

Note that if you want prettier output you can change the loop label:

     loop: "{{ backup_files_results.json.files }}"
     loop_control:
       label: "{{ item.uri }}"
1 Like