# Splitting a variable length list into a list of lists

**URL:** <https://forum.ansible.com/t/splitting-a-variable-length-list-into-a-list-of-lists/24716>\
**Category:** Ansible Project\
**Created:** [June 13, 2017, 8:37pm UTC](https://forum.ansible.com/t/splitting-a-variable-length-list-into-a-list-of-lists/24716 "2017-06-13T20:37:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed1](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Ed1](https://forum.ansible.com/u/Ed1)\
**Post date:** [June 13, 2017, 8:37pm UTC](https://forum.ansible.com/t/splitting-a-variable-length-list-into-a-list-of-lists/24716/1 "2017-06-13T20:37:04Z")

</div>

Greetings,

I’d like to take a list (of varying size) and split it into a list of lists, where each sub-list is at most N elements. I’m not certain where to start on this one. Advice welcome.

The background is that I’ve got a dynamically built list of packages for installation. For Centos 6.x, if the list is too large, the yum module will fail out, reporting “ValueError: filedescriptor out of range in select()”. I’d like to split the large list into sub-lists, and process each sub-list with separate yum tasks. I’m not clear how I can use Jinja / Ansible to build that list of lists. I figure if I can craft the list of lists, I can use with\_items on an include, where the include file will run the yum install on one sub-list at a time.

I’m open to other approaches on how to solve the problem.

Thanks,  
Ed

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [June 14, 2017, 5:14am UTC](https://forum.ansible.com/t/splitting-a-variable-length-list-into-a-list-of-lists/24716/2 "2017-06-14T05:14:19Z")

</div>

Are you sure the size of the list is the source of your error? It sounds more like an “open files” issue. Whats the output of ulimit -n?

---

<div class="post-metadata">

**Author:** ![Ed1](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Ed1](https://forum.ansible.com/u/Ed1)\
**Post date:** [June 14, 2017, 1:14pm UTC](https://forum.ansible.com/t/splitting-a-variable-length-list-into-a-list-of-lists/24716/3 "2017-06-14T13:14:58Z")

</div>

It’s a combination of both, actually. I can boost the nofile setting, but I’m still able to construct a large enough package list that it fails. Things work fine in Centos 7.x - I don’t know if the resolution arrived in python or in Centos.

I’m going to try using the batch filter for my sub-lists. I’d somehow missed it in my searching.

---

<div class="post-metadata">

**Author:** ![Ed1](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Ed1](https://forum.ansible.com/u/Ed1)\
**Post date:** [June 14, 2017, 3:06pm UTC](https://forum.ansible.com/t/splitting-a-variable-length-list-into-a-list-of-lists/24716/4 "2017-06-14T15:06:27Z")

</div>

I think I have a working solution. The outer file has:

> - set\_fact:  
> batches: “{{ packages|batch(batch\_size)|list }}”
> 
> - include: run\_installs.yml  
> with\_list: “{{ batches }}”  
> loop\_control:  
> loop\_var: batch

Then run\_installs.yml is simply:

> * * *
> 
> - name: Update and install  
> yum:  
> state: latest  
> name: “{{ item }}”  
> with\_items: “{{ batch }}”  
> become: true  
> become\_user: root

I have to use with\_list in the outer file since with\_items will flatten lists (ref: [https://stackoverflow.com/questions/43902117/ansible-list-of-lists-flattening](https://stackoverflow.com/questions/43902117/ansible-list-of-lists-flattening)).

Ed
