Splitting a List into chunks

5 views
Skip to first unread message

Spiro Mitsialis

unread,
Sep 5, 2019, 10:15:38 AM9/5/19
to Ansible Project
Hi,
I have a list of 200+ items (showing sample below).  I need to update a database with these records using the URI module.  The backend API only supports 100 records at at time.  
How can I loop through the URI module with 100 records at a time.  I was thinking of using json_query and doing slicing as per the JMEPath docs but cant find any examples of the syntax for Ansible.
If using the json_quere, I would need some sort of variable in the slice.

  - name: Add/Update switchport in cmdb
    uri
:
      url
: https://{{ cmdb }}/{{ switchports }}/batchUpsert
      method
: PUT
      validate_certs
: no
      body_format
: json
      body
: '{{ new_list | json_query([:100] }}'
   
register: batchupsert_result
   loop
:???



    "new_list": [
        {
            "description": null,
           
"id": "cjzu47wi6000301yu0dse3q53",
           
"lastSeenDate": "2019-09-05 10:06:42",
           
"mediaType": "access",
           
"name": "GigabitEthernet2/1/1",
           
"poeEnabledInd": true,
           
"portDuplexModeId": "cjzodb84y001201fmbox90ygt",
           
"portFastInd": true,
           
"portSpeedId": "cjzodk5zx001301fmac9u54oc",
           
"securityShutdownInd": "false",
           
"status": "down",
           
"voiceInd": false
       
},
       
{
           
"description": null,
           
"id": "cjzu4jsq1000u01yu9cw4a9h7",
           
"lastSeenDate": "2019-09-05 10:06:42",
           
"mediaType": "access",
           
"name": "GigabitEthernet2/0/9",
           
"poeEnabledInd": true,
           
"portDuplexModeId": "cjzodb84y001201fmbox90ygt",
           
"portFastInd": true,
           
"portSpeedId": "cjzodk5zx001301fmac9u54oc",
           
"securityShutdownInd": "false",
           
"status": "down",
           
"voiceInd": false
       
},
       
{
           
"description": null,
           
"id": "cjzu4jxhc000v01yuf9c7atd3",
           
"lastSeenDate": "2019-09-05 10:06:42",
           
"mediaType": "access",
           
"name": "TenGigabitEthernet2/1/3",
           
"poeEnabledInd": true,
           
"portDuplexModeId": "cjzodb84y001201fmbox90ygt",
           
"portFastInd": true,
           
"portSpeedId": "cjzodk5zx001301fmac9u54oc",
           
"securityShutdownInd": "false",
           
"status": "shutdown",
           
"voiceInd": false
       
},
       
{
           
"description": null,
           
"id": "cjzu48c09000401yu1o443fuq",
           
"lastSeenDate": "2019-09-05 10:06:42",
           
"mediaType": "access",
           
"name": "TenGigabitEthernet2/1/4",
           
"poeEnabledInd": true,
           
"portDuplexModeId": "cjzodb84y001201fmbox90ygt",
           
"portFastInd": true,
           
"portSpeedId": "cjzodk5zx001301fmac9u54oc",
           
"securityShutdownInd": "false",
           
"status": "down",
           
"voiceInd": false
       
}
   
]



Reply all
Reply to author
Forward
0 new messages