2017-05-01 35 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