[root@localhost cycle]# cat cycle.1.yml----name: cycletesthosts: testgather_facts: notasks:-name: debug cycledebug:msg: "{{ item }}"loop:- test1- test2
[root@localhost cycle]# ansible-playbookcycle.1.ymlPLAY [cycle test]***************************************************************************************TASK [debug cycle]**************************************************************************************ok: [192.168.15.10] => (item=test1)=> {"msg": "test1"}ok: [192.168.15.10] => (item=test2)=> {"msg": "test2"}PLAY RECAP**********************************************************************************************192.168.15.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0debug cycle------------------------------------------------------------- 0.17sPlaybook finished: Mon Dec 24 17:57:252018, 1 total tasks. 0:00:00 elapsed.
[root@localhost cycle]# cat cycle.2.yml----name: cycle test2hosts: testgather_facts: novars:dicts:China: 1America: 2tasks:-name: debug cycledebug:msg: "{{ item.key }} is no.{{ item.value }}"loop: "{{ lookup('dict',dicts) }}"
[root@localhost cycle]# ansible-playbookcycle.2.ymlPLAY [cycle test2]**************************************************************************************TASK [debug cycle] **************************************************************************************ok: [192.168.15.10] => (item={'key':u'America', 'value': 2}) => {"msg": "America is no.2"}ok: [192.168.15.10] => (item={'key':u'China', 'value': 1}) => {"msg": "China is no.1"}PLAY RECAP**********************************************************************************************192.168.15.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0debug cycle -------------------------------------------------------------0.18sPlaybook finished: Mon Dec 24 17:59:582018, 1 total tasks. 0:00:00 elapsed
----name: cycle test3hosts: testgather_facts: novars:dicts:China: 1America: 2tasks:-name: debug cycledebug:msg: "{{ item.key }} is no.{{ item.value }}"loop: "{{ dicts | dict2items }}"
[root@localhost cycle]# ansible-playbookcycle.3.ymlPLAY [cycle test3]**************************************************************************************TASK [debug cycle]**************************************************************************************ok: [192.168.15.10] => (item={'key':u'America', 'value': 2}) => {"msg": "America is no.2"}ok: [192.168.15.10] => (item={'key':u'China', 'value': 1}) => {"msg": "China is no.1"}PLAY RECAP **********************************************************************************************192.168.15.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0debug cycle------------------------------------------------------------- 0.18sPlaybook finished: Mon Dec 24 18:05:212018, 1 total tasks. 0:00:00 elapsed.
[root@localhost cycle]# cat cycle.4.yml----name: cycle test4hosts: testgather_facts: novars:dicts:- A- B- [c,D]tasks:-name: debug cycledebug:msg: "{{ item }}"loop: "{{ dicts }}"
[root@localhost cycle]# ansible-playbookcycle.4.ymlPLAY [cycle test4]**************************************************************************************TASK [debug cycle]**************************************************************************************ok: [192.168.15.10] => (item=A) => {"msg": "A"}ok: [192.168.15.10] => (item=B) => {"msg": "B"}ok: [192.168.15.10] => (item=[u'c',u'D']) => {"msg": ["c","D"}PLAY RECAP **********************************************************************************************192.168.15.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0debug cycle------------------------------------------------------------- 0.19sPlaybook finished: Mon Dec 24 18:25:412018, 1 total tasks. 0:00:00 elapsed.
[root@localhost cycle]# cat cycle.5.yml----name: cycle test5hosts: testgather_facts: novars:dicts:- A- B- [c,D]tasks:-name: debug cycledebug:msg: "{{ item }}"loop: "{{ dicts | flatten }}"
[root@localhost cycle]# ansible-playbookcycle.5.ymlPLAY [cycle test5]**************************************************************************************TASK [debug cycle]**************************************************************************************ok: [192.168.15.10] => (item=A) => {"msg": "A"}ok: [192.168.15.10] => (item=B) => {"msg": "B"}ok: [192.168.15.10] => (item=c) => {"msg": "c"}ok: [192.168.15.10] => (item=D) => {"msg": "D"}PLAY RECAP**********************************************************************************************192.168.15.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0debug cycle------------------------------------------------------------- 0.19sPlaybook finished: Mon Dec 24 18:31:472018, 1 total tasks. 0:00:00 elapsed.
[root@localhost cycle]# cat cycle.6.yml----name: cycle test6hosts: testgather_facts: novars:dicts:- A- B- [c,D]tasks:-name: debug cycledebug:msg: "{{ item }}"loop: "{{ dicts | flatten(levels=1) }}"
[root@localhost cycle]# ansible-playbookcycle.6.ymlPLAY [cycle test6] **************************************************************************************TASK [debug cycle]**************************************************************************************ok: [192.168.15.10] => (item=A) => {"msg": "A"}ok: [192.168.15.10] => (item=B) => {"msg": "B"}ok: [192.168.15.10] => (item=c) => {"msg": "c"}ok: [192.168.15.10] => (item=D) => {"msg": "D"}PLAY RECAP **********************************************************************************************192.168.15.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0debug cycle------------------------------------------------------------- 0.29sPlaybook finished: Mon Dec 24 18:34:312018, 1 total tasks. 0:00:00 elapsed.
[root@localhost cycle]# cat cycle.7.yml----name: cycle test7hosts: testgather_facts: novars:dicts:- A- B- [c,D]tasks:-name: debug cycledebug:msg: " {{ index}}--{{ item }}"loop: "{{ dicts | flatten(levels=1) }}"loop_control:index_var: index
[root@localhost cycle]# ansible-playbookcycle.7.ymlPLAY [cycle test7]**************************************************************************************TASK [debug cycle]**************************************************************************************ok: [192.168.15.10] => (item=A) => {"msg": " 0--A"}ok: [192.168.15.10] => (item=B) => {"msg": " 1--B"}ok: [192.168.15.10] => (item=c) => {"msg": " 2--c"}ok: [192.168.15.10] => (item=D) => {"msg": " 3--D"}PLAY RECAP**********************************************************************************************192.168.15.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0debug cycle------------------------------------------------------------- 0.21sPlaybook finished: Mon Dec 24 20:29:572018, 1 total tasks. 0:00:00 elapsed.
[root@localhost cycle]# cat cycle.8.yml----name: cycle test8hosts: testgather_facts: novars:testlist1: [ A,B,C,D ]testlist2: [ 110,120,911 ]testlist3: [ x,y ]tasks:-name: debug cycle with_togetherdebug:msg: " {{ item.0 }} - {{ item.1 }} - {{ item.2 }}"with_together:- "{{ testlist1 }}"- "{{ testlist2 }}"- "{{ testlist3 }}"-name: debug cycle loop+zip_logestdebug:msg: " {{ item.0 }} - {{ item.1 }} - {{ item.2 }}"loop: "{{ testlist1 | zip_longest(testlist2,testlist3) | list }}
[root@localhost cycle]# ansible-playbookcycle.8.ymlPLAY [cycle test8]**************************************************************************************TASK [debug cycle with_together]************************************************************************ok: [192.168.15.10] => (item=[u'A', 110,u'x']) => {"msg": " A - 110 - x"}ok: [192.168.15.10] => (item=[u'B', 120,u'y']) => {"msg": " B - 120 - y"}ok: [192.168.15.10] => (item=[u'C', 911,None]) => {"msg": " C - 911 - "}ok: [192.168.15.10] => (item=[u'D',None, None]) => {"msg": " D - -"}TASK [debug cycle loop+zip_logest]**********************************************************************ok: [192.168.15.10] => (item=[u'A', 110,u'x']) => {"msg": " A - 110 - x"}ok: [192.168.15.10] => (item=[u'B', 120,u'y']) => {"msg": " B - 120 - y"}ok: [192.168.15.10] => (item=[u'C', 911,None]) => {"msg": " C - 911 - "}ok: [192.168.15.10] => (item=[u'D',None, None]) => {"msg": " D - -"}PLAY RECAP**********************************************************************************************192.168.15.10 : ok=2 changed=0 unreachable=0 failed=0 skipped=0debug cycle with_together----------------------------------------------- 0.25sdebug cycle loop+zip_logest--------------------------------------------- 0.21sPlaybook finished: Mon Dec 24 20:44:422018, 2 total tasks. 0:00:00 elapsed.
- debug:msg: "{{ item.0 }} - {{ item.1 }} - {{item.2}}"loop: "{{ testlist1 | zip_longest(testlist2,testlist3,fillvalue='None') | list }}"
- debug:msg: "{{ item.0 }} - {{ item.1 }} - {{item.2}}"loop: "{{ testlist1 | zip(testlist2,testlist3) | list }}"
[root@localhost cycle]# cat cycle.9.yml----name: cycle test4hosts: testgather_facts: novars:list1: [ 1,2,3 ]list2: [ a,b,c,d ]tasks:-name: debug cycledebug:msg: "{{ item.0 }} ---- {{item.1 }}"loop: "{{ list1 | product(list2) | list }}"
[root@localhost cycle]# ansible-playbookcycle.9.ymlPLAY [cycle test4]**************************************************************************************TASK [debug cycle]**************************************************************************************ok: [192.168.15.10] => (item=[1, u'a'])=> {"msg": "1 ---- a"}ok: [192.168.15.10] => (item=[1, u'b'])=> {"msg": "1 ---- b"}ok: [192.168.15.10] => (item=[1, u'c'])=> {"msg": "1 ---- c"}ok: [192.168.15.10] => (item=[1, u'd'])=> {"msg": "1 ---- d"}ok: [192.168.15.10] => (item=[2, u'a'])=> {"msg": "2 ---- a"}ok: [192.168.15.10] => (item=[2, u'b'])=> {"msg": "2 ---- b"}ok: [192.168.15.10] => (item=[2, u'c'])=> {"msg": "2 ---- c"}ok: [192.168.15.10] => (item=[2, u'd'])=> {"msg": "2 ---- d"}ok: [192.168.15.10] => (item=[3, u'a'])=> {"msg": "3 ---- a"}ok: [192.168.15.10] => (item=[3, u'b'])=> {"msg": "3 ---- b"}ok: [192.168.15.10] => (item=[3, u'c'])=> {"msg": "3 ---- c"}ok: [192.168.15.10] => (item=[3, u'd'])=> {"msg": "3 ---- d"}PLAY RECAP **********************************************************************************************192.168.15.10 : ok=1 changed=0 unreachable=0 failed=0 skipped=0debug cycle------------------------------------------------------------- 0.31sPlaybook finished: Mon Dec 24 20:55:192018, 1 total tasks. 0:00:00 elapsed.
#使用random函数可以替代with_random_choice,由于random函数是随机取出列表中的一个值,并不涉及循环操作,所以并不用使用loop关键字。[root@localhost cycle]# cat cycle.10.yml----name: cycle testhosts: testgather_facts: novars:list: [a,b,c]tasks:-name: debug cycledebug:msg: "{{ list | random }}"- debug:msg: "{{ list | random }}"- debug:msg: "{{ list | random }}
[root@localhost cycle]# ansible-playbookcycle.10.ymlPLAY [cycle test]***************************************************************************************TASK [debug cycle] **************************************************************************************ok: [192.168.15.10] => {"msg": "a"}TASK [debug]********************************************************************************************ok: [192.168.15.10] => {"msg": "a"}TASK [debug]********************************************************************************************ok: [192.168.15.10] => {"msg": "c"}PLAY RECAP**********************************************************************************************192.168.15.10 : ok=3 changed=0 unreachable=0 failed=0 skipped=0debug cycle------------------------------------------------------------- 0.18s------------------------------------------------------------------------0.16sPlaybook finished: Mon Dec 24 21:06:322018, 2 total tasks. 0:00:00 elapsed.
[root@localhost cycle]# cat cycle.11.yml----name: cycle test2hosts: testgather_facts:novars:dicts:China: 1America: 2aaa: 3bbb: 4ccc: 5tasks:-name: debug cycle dict2itemsdebug:msg: "{{ item.key }} is no.{{ item.value }}"loop: "{{ dicts | dict2items }}"-name: debug cycledebug:msg: "{{ item.0 }} is no.{{ item.1 }}"loop: "{{ dicts | dictsort }}"
[root@localhost cycle]# ansible-playbookcycle.11.ymlPLAY [cycle test2]**************************************************************************************TASK [debug cycle dict2items]***************************************************************************ok: [192.168.15.10] => (item={'key':u'China', 'value': 1}) => {"msg": "China is no.1"}ok: [192.168.15.10] => (item={'key':u'America', 'value': 2}) => {"msg": "America is no.2"}ok: [192.168.15.10] => (item={'key':u'aaa', 'value': 3}) => {"msg": "aaa is no.3"}ok: [192.168.15.10] => (item={'key':u'bbb', 'value': 4}) => {"msg": "bbb is no.4"}ok: [192.168.15.10] => (item={'key':u'ccc', 'value': 5}) => {"msg": "ccc is no.5"}TASK [debug cycle]**************************************************************************************ok: [192.168.15.10] => (item=[u'aaa', 3])=> {"msg": "aaa is no.3"}ok: [192.168.15.10] =>(item=[u'America', 2]) => {"msg": "America is no.2"}ok: [192.168.15.10] => (item=[u'bbb',4]) => {"msg": "bbb is no.4"}ok: [192.168.15.10] => (item=[u'ccc',5]) => {"msg": "ccc is no.5"}ok: [192.168.15.10] => (item=[u'China',1]) => {"msg": "China is no.1"}PLAY RECAP **********************************************************************************************192.168.15.10 : ok=2 changed=0 unreachable=0 failed=0 skipped=0debug cycle dict2items-------------------------------------------------- 0.26sdebug cycle------------------------------------------------------------- 0.22sPlaybook finished: Mon Dec 24 21:10:402018, 2 total tasks. 0:00:00 elapsed.
错误信息