2017-05-01 32 views
0

给定一个计数,比如说5和字符串列表['string1',string2','string3']我想在字符串列表中循环指定的次数。预期产出将为['string1',string2','string3','string1','string2']Ansible循环数与列表中的数

我认为这与loop.cyclein jinja2有什么相似之处,但是,我想在操作手册中将它用作键值而不是模板。有没有什么典型的方法来完成这个?如果我需要制作一个自定义的filter_plugin,我想这不是什么大问题,但是想知道该功能是否已经存在。

我打算的实际使用案例是,能够将给定的子网列表与主机数量进行匹配,以便提供任意数量的主机以跨越子网列表。产生的列表可能会被with_indexed_items循环使用。

回答

2

例如:

--- 
- hosts: localhost 
    gather_facts: no 
    connection: local 
    vars: 
    mylist: 
     - string1 
     - string2 
     - string3 
    tasks: 
    - debug: 
     msg: "{{ mylist[item|int % 3] }}" 
     with_sequence: start=0 end=4 
+0

这是非常有益的,谢谢你。我结束了一个轻微的修改: '--- - 主机:本地主机 gather_facts:没有 连接:本地 增值经销商: 数:5 MYLIST: - 字符串1 - 字符串2 - STRING3 任务: - debug: msg:“{{mylist [item | int%mylist | length]}}” with_sequence:start = 0 end = {{count - 1}}' – mat