amazon s3 - ansible include with array slices -
in ansible 2.2, want loop on large list of files read s3.
here role/tasks/main.yml
- name: simulate variable read 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 s3 include: submodule.yml with_items: list_of_files_to_import
here role/tasks/submodule.yml
--- - name: restore table {{ item }} debug: var={{ item }}
this crashes because there many files.
i have found can slice array , send sections @ time:
- name: process each file s3 include: submodule.yml with_items: "{{ list_of_files_to_import[0:5] }}" - name: process each file s3 include: submodule.yml with_items: "{{ list_of_files_to_import[5:10] }}" - name: process each file s3 include: submodule.yml with_items: "{{ list_of_files_to_import[10:15] }}" - name: process each file s3 include: submodule.yml with_items: "{{ list_of_files_to_import[15:20] }}"
instead of hardcoding these little blocks, wanted try like
- name: process each file s3 include: submodule.yml with_items: "{{ list_of_files_to_import[{{start}}:{{end}}] }}"
but cannot variable-defined variable names
how can process large list of items in ansible 2.2?
i ended solving shell script, repeatedly calling playbook --extra-vars
specify files process.
this works because list of files in s3 have similar filenames. loops through filenames , processes each 1 @ time.
#!/usr/bin/env bash # return 0(true) if year/month combination valid valid() { yr=$1 mn=$2 # not run on months after month if [ "$yr" -eq "2017" -a "$mn" -gt "$(date +%m)" ] return 1 fi return 0 } # every year 2002 year year in `seq 2002 $(date +%y)` # every zero-padded month, 01-12 month in `seq -f "%02g" 01 12` # each type of item in inventory object in widgets doodads if valid $year $month; ansible-playbook playbook_name.yml --extra-vars "object=$object year=$year month=$month" else echo skipping invalid combo $object $year $month fi done done done
Comments
Post a Comment