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?