# Ansible loop over array

**URL:** <https://forum.ansible.com/t/ansible-loop-over-array/27940>\
**Category:** Ansible Project\
**Created:** [July 3, 2018, 5:39am UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940 "2018-07-03T05:39:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Madushan\_Chathuranga](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Madushan\_Chathuranga](https://forum.ansible.com/u/Madushan_Chathuranga)\
**Post date:** [July 3, 2018, 5:39am UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940/1 "2018-07-03T05:39:03Z")

</div>

Hi,

Is there a way to loop over an array in ansible with a given array size. eg: I have an array with 10 elements. I want to limit the iteration only to first 5 elemets.

Thanks.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [July 3, 2018, 5:44am UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940/2 "2018-07-03T05:44:36Z")

</div>

the slice() filter will give you a list of arrays each with one element in it

(i.e., not what you were expecting, but still useful).

---

<div class="post-metadata">

**Author:** ![erichymowitz](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@erichymowitz](https://forum.ansible.com/u/erichymowitz)\
**Post date:** [July 3, 2018, 12:26pm UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940/3 "2018-07-03T12:26:28Z")

</div>

Could you use an “indexed array” and use when to check for index \<= 5?

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html#with-indexed-items](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-indexed-items)

```auto
- name: with_indexed_items
  debug:
    msg: "Index {{ item.0 }} - Value {{ item.1 }}"
  with_indexed_items: "{{ items }}"

```

```auto
- name: with_indexed_items -> loop
  debug:
    msg: "{{ index }} - {{ item }}"
  loop: "{{ items|flatten(levels=1) }}"
  loop_control:
    index_var: index

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 3, 2018, 12:44pm UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940/4 "2018-07-03T12:44:41Z")

</div>

name: loop over first 5  
debug: var=item  
loop: "{{ mylist[:5] }}"

---

<div class="post-metadata">

**Author:** ![Madushan\_Chathuranga](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Madushan\_Chathuranga](https://forum.ansible.com/u/Madushan_Chathuranga)\
**Post date:** [July 3, 2018, 12:49pm UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940/5 "2018-07-03T12:49:06Z")

</div>

@Brain Coca

That looks nice. Btw what if the number of elements available in the array is less than 5 and we give loop: “{{ mylist[:5] }}”. Should that throw any exception?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 3, 2018, 12:58pm UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940/6 "2018-07-03T12:58:22Z")

</div>

No, it will consume UP TO that number

---

<div class="post-metadata">

**Author:** ![Madushan\_Chathuranga](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Madushan\_Chathuranga](https://forum.ansible.com/u/Madushan_Chathuranga)\
**Post date:** [July 3, 2018, 1:05pm UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940/7 "2018-07-03T13:05:39Z")

</div>

Thanks. I tried it out as well. In addition this degit no ‘5’ will be an input from --extra-vars. eg : --extra-vars ‘size=5’  
then i was trying to consume that variable like this loop: “{{ mylist[:size] }}” But this throws an error saying ‘’ slice indices must be integers or none or have an **init** method. is there a way that I can set this limit of the array using a variable?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 3, 2018, 1:09pm UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940/8 "2018-07-03T13:09:22Z")

</div>

cause size=5 is a string (k=v notation does not 'type' well),  
mylist[:size|int] to avoid that or you can try yaml notation, but that  
can also use type depending on circumstance, the |int at 'consumption'  
is always safe.

---

<div class="post-metadata">

**Author:** ![Madushan\_Chathuranga](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Madushan\_Chathuranga](https://forum.ansible.com/u/Madushan_Chathuranga)\
**Post date:** [July 3, 2018, 1:09pm UTC](https://forum.ansible.com/t/ansible-loop-over-array/27940/9 "2018-07-03T13:09:46Z")

</div>

UPDATE

a solution would be “{{size | int}}”. I think when we pass it as an optional it take it as a string by default so using ‘int’ filter would solve this.

Thanks ,  
Madushan
