Amazon s3 带数组切片的ansible include
在Ansible 2.2中,我想循环遍历从S3读取的大量文件列表 这是我的Amazon s3 带数组切片的ansible include,amazon-s3,ansible,Amazon S3,Ansible,在Ansible 2.2中,我想循环遍历从S3读取的大量文件列表 这是我的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",
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
---
- name: Restore TABLE {{ item }}
debug: var={{ item }}
这会崩溃,因为文件太多
我发现我可以对阵列进行切片并一次发送部分:
- 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] }}"
-名称:处理S3中的每个文件
包括:submodule.yml
带_项:“{{list_of_files_to_import[0:5]}”
-名称:处理S3中的每个文件
包括:submodule.yml
带_项:“{{list_of_files_to_import[5:10]}”
-名称:处理S3中的每个文件
包括:submodule.yml
带_项:“{{list_of_files_to_import[10:15]}”
-名称:处理S3中的每个文件
包括:submodule.yml
带_项:“{{list_of_files_to_import[15:20]}”
我不想硬编码所有这些小块,而是想尝试以下方法
- name: Process each file from S3
include: submodule.yml
with_items: "{{ list_of_files_to_import[{{start}}:{{end}}] }}"
-名称:处理S3中的每个文件
包括:submodule.yml
与_项一起:“{{list_of_files__to_import[{{{start}}:{{end}]}”
但是我们
如何处理Ansible 2.2中的大量项目列表?我最终用一个shell脚本解决了这个问题,用一些额外的变量反复调用playbook来指定要处理的文件 这仅仅是因为S3中的文件列表具有相似的文件名。基本上,它循环遍历文件名,并一次处理每个文件名 #!/usr/bin/env bash # return 0(true) if the year/month combination is valid valid() { yr=$1 mn=$2 # do not run on months after this month if [ "$yr" -eq "2017" -a "$mn" -gt "$(date +%m)" ] then return 1 fi return 0 } # For every year from 2002 to this year for year in `seq 2002 $(date +%Y)` do # for every zero-padded month, 01-12 for month in `seq -f "%02g" 01 12` do # for each type of item in inventory for object in widgets doodads do if valid $year $month; then 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 #!/usr/bin/env bash #如果年/月组合有效,则返回0(true) 有效(){ 年=1美元 mn=2美元 #不要在本月之后的月份运行 如果[“$yr”-eq“2017”-a“$mn”-gt“$(日期+%m)”] 然后 返回1 fi 返回0 } #2002年至今年每年 以'seq 2002$为单位的年度(日期+%Y)` 做 #对于每个零填充月,01-12 对于'seq-f”%02g“01 12中的月份` 做 #对于库存中的每种类型的项目 用于小部件doodas中的对象 做 如果有效,$年$月; 然后 ansible playbook playbook_name.yml--额外变量“object=$object year=$year month=$month” 其他的 回显跳过无效组合$object$year$month fi 完成 完成 完成
名单有多大?坠机的原因是什么?包含太多?大约有300个文件。我看到的唯一原因是“内存不足”。但是的,它似乎无法处理这么多的包含。我不确定您的详细信息,但它看起来有点像问题。如果是这样的话,这个问题应该在下一个Ansible版本中解决。@Doomy你可能是对的。我最终用Bash解决了这个问题。我会发布我的解决方案。