Ansible loop over array

2,501 views
Skip to first unread message

Madushan Chathuranga

unread,
Jul 3, 2018, 1:39:03 AM7/3/18
to Ansible Project
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.

Karl Auer

unread,
Jul 3, 2018, 1:44:47 AM7/3/18
to ansible...@googlegroups.com
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).

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cb5340b2-ffd3-4def-b899-cb59403772df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Karl Auer

Email  : ka...@2pisoftware.com
Website: 
http://2pisoftware.com

GPG/PGP : 958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816
Previous: F0AB 6C70 A49D 1927 6E05 81E7 AD95 268F 2AB6 40EA

eric.b....@nasa.gov

unread,
Jul 3, 2018, 8:26:28 AM7/3/18
to Ansible Project
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

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


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

Brian Coca

unread,
Jul 3, 2018, 8:46:10 AM7/3/18
to Ansible Project
name: loop over first 5
debug: var=item
loop: "{{ mylist[:5] }}"

--
----------
Brian Coca

Madushan Chathuranga

unread,
Jul 3, 2018, 8:49:06 AM7/3/18
to Ansible Project
@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?

Brian Coca

unread,
Jul 3, 2018, 8:59:51 AM7/3/18
to Ansible Project
No, it will consume UP TO that number


--
----------
Brian Coca

Madushan Chathuranga

unread,
Jul 3, 2018, 9:05:39 AM7/3/18
to Ansible Project
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?  

Madushan Chathuranga

unread,
Jul 3, 2018, 9:09:46 AM7/3/18
to Ansible Project
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

Brian Coca

unread,
Jul 3, 2018, 9:10:49 AM7/3/18
to Ansible Project
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.




--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages