In Ansible 2.2, I want to loop over a large list of files read from S3.
Here is my role/tasks/main.yml
- name: Simulate variable read from S3 set_fact: list_of_files_to_import: [ "a.tar.gz", "b.tar.gz", "c.tar.gz", "d.tar.gz", "e.tar.gz", "f.tar.gz", "g.tar.gz", "h.tar.gz", ... "zz.tar.gz" ] - name: Process each file from S3 include: submodule.yml with_items: list_of_files_to_import
Here is role/tasks/submodule.yml
--- - name: Restore TABLE {{ item }} debug: var={{ item }}
This crashes because there are too many files.
I have found that I can slice the array and send sections at a time:
- name: Process each file from S3 include: submodule.yml with_items: "{{ list_of_files_to_import[0:5] }}" - name: Process each file from S3 include: submodule.yml with_items: "{{ list_of_files_to_import[5:10] }}" - name: Process each file from S3 include: submodule.yml with_items: "{{ list_of_files_to_import[10:15] }}" - name: Process each file from S3 include: submodule.yml with_items: "{{ list_of_files_to_import[15:20] }}"
Instead of hardcoding all these little blocks, I wanted to try something like
- name: Process each file from S3 include: submodule.yml with_items: "{{ list_of_files_to_import[{{start}}:{{end}}] }}"
But we cannot get variable-defined variable names
How can I process a large list of items in Ansible 2.2?