說明
在使用ansible時,我們經常需要查看或者處理一些返回信息,這些返回信息多是呈現為列表的形式,當信息很多時不便於我們查看,這時候就需要用循環將列表的有序呈現出來,根據官網說明,ansible循環到目前為止分為仨版本,這里直接介紹第一種遷移到第三種的改變:
- ansible 2.5版本之前,循環通過”with_“開頭的關鍵字實現。
- ansible 2.5版本,官方推薦使用“loop+lookup插件”(插件名對應with_后面的名字。)的方式來替代“with_”方式實現循環,但依然支持“with_”用法。比如with_flattened: "{{testvar}}"和loop: {{lookup('flattened',testvar)}}就是等同作用,flattened就是lookup插件。
- ansible 2.6版本,官方推薦使用"loop+filter"用法來替代之前的方式實現循環,但依然支持前面兩種用法。
我不想接受他的建議,算了,還是接受吧 -_-||>。
with_和loop循環
-
with_items舉例使用:批量創建文件。
# with_方式實現。 --- - hosts: ck-node1 vars: var1: - "/root/aaa" - "/root/bbb" - "/root/ccc" tasks: - name: create directory file: path={{item}} state=touch with_items: "{{var1}}" -------------------------------------------------------------------------------------------------------- # 使用loop+filter方式實現。 --- - hosts: ck-node1 vars: var1: - "/root/aaa" - "/root/bbb" - "/root/ccc" tasks: - name: create directory file: path={{item}} state=directory loop: "{{ var1 | flatten }}" # 省事的寫法,可以省去vars。我下面例子都注明vars便於理解。 --- - hosts: ck-node1 tasks: - name: create directory file: path={{item}} state=touch with_items: - "/root/aaa" - "/root/bbb" - "/root/ccc"
-
with_flattened:和with_items作用一致。
-
with_list:會輸出列表的整體內容,不會遍歷列表。
# with_方式實現。 --- - hosts: ck-node1 vars: var1: - [1,2,3] - [aa,bb,cc] tasks: - debug: msg: "{{item}}" with_list: "{{var1}}" -------------------------------------------------------------------------------------------------------- # 使用loop+filter方式實現。 --- - hosts: ck-node1 vars: var1: - [1,2,3] - [aa,bb,cc] tasks: - debug: msg: "{{item}}" loop: "{{var1}}"
-
with_together:將兩個列表對齊,合並輸出。
# with_方式實現。 --- - hosts: ck-node1 vars: var1: - [1,2,3] - [aa,bb] tasks: - debug: msg: "{{item}}" with_together: "{{var1}}" -------------------------------------------------------------------------------------------------------- # 使用loop+filter方式實現。 --- - hosts: ck-node1 vars: var1: - [1,2,3] - [aa,bb] tasks: - debug: msg: "{{item}}" loop: "{{var1[0] | zip_longest(var1[1]) | list}}" # with_together等同於loop+zip_logest+list。表示按最長的列表對齊,按最短列表對齊可以使用loop+zip+list
-
with_indexed_items:打印出循環列表中每一項的索引。
# with_方式實現。 --- - hosts: ck-node1 vars: var1: - aa - [bb,cc] - [dd,[ee,ff]] tasks: - debug: msg: "{{item}}" with_indexed_items: "{{var1}}" -------------------------------------------------------------------------------------------------------- # 使用loop+filter方式實現。 --- - hosts: ck-node1 vars: var1: - aa - [bb,cc] - [dd,[ee,ff]] tasks: - debug: msg: "{{var1_index}},{{item}}" loop: "{{var1 | flatten(levels=1)}}" # 1代表之展開1層列表,2代表第2層列表。 loop_control: index_var: var1_index # loop_control用於控制循環的行為,比如將循環獲取到的元素索引放在指定的var1_index變量中。
-
with_cartesian:笛卡爾積的方式組合列表,兩兩相組。
# with_方式實現。 --- - hosts: ck-node1 vars: var1: - [a,b,c] - [test1,test2] tasks: - debug: msg: "{{item}}" with_cartesian: "{{var1}}" -------------------------------------------------------------------------------------------------------- # 使用loop+filter方式實現。 --- - hosts: ck-node1 vars: var1: - [a,b,c] - [test1,test2] tasks: - debug: msg: "{{item}}" loop: "{{var1[0] | product(var1[1]) | list}}"
-
with_sequence:按照順序生成數字序列。
舉例1: # with_方式實現。 --- - hosts: ck-node1 tasks: - debug: msg: "{{item}}" with_sequence: start=1 end=5 stride=1 # 從1開始,到5結束,步長為1。此種寫法等於count=5 -------------------------------------------------------------------------------------------------------- # 使用loop+filter方式實現。 --- - hosts: ck-node1 tasks: - debug: msg: "{{item}}" loop: "{{range(1,5,1) | list}}" PS:步長省略時默認值為1,當end值小於start值時,步長不可省略。rangge函數的操作范圍不包含結束,也就是不包含5。 # 舉例2:格式化功能。 0 18:19:10 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat aaa.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{item}}" with_sequence: start=1 end=5 stride=1 format="number is %0.2f" -------------------------------------------------------------------------------------------------------- # 使用loop+filter方式實現。 --- - hosts: ck-node1 tasks: - debug: msg: "{{'number is %0.2f' | format(item)}}" loop: "{{range(1,5,1) | list}}"
-
with_dict:接收變量信息,並將變量的信息以字典的形式輸出。
# with_方式實現。 --- - hosts: ck-node1 vars: users: bob: male zoe: female tasks: - debug: msg: "{{item}}" with_dict: "{{users}}" -------------------------------------------------------------------------------------------------------- # 使用loop+filter方式實現。 --- - hosts: ck-node1 vars: users: bob: male zoe: female tasks: - debug: msg: "{{item.key}} is {{item.value}}" loop: "{{ users | dict2items }}"
-
with_random_choice:從列表的多個值中隨機返回一個值。
# with_方式實現。 --- - hosts: ck-node1 tasks: - debug: msg: "{{item}}" with_random_choice: [1,aa,32,bb] -------------------------------------------------------------------------------------------------------- # 使用random函數可以替代with_random_choice,由於random函數是隨機取出列表中的一個值,並不涉及循環操作,所以並不用使用loop關鍵字。 --- - hosts: ck-node1 vars: var1: [1,aa,32,bb] tasks: - debug: msg: "{{var1 | random}}"
-
with_subelements:結合變量的子單元處理數據,使用時需要指定一個子元素,該子元素必須是一個列表。
# with_方式實現。 --- - hosts: ck-node1 vars: users: - name: bob gender: male hobby: ['swimming','running'] - name: zoe gender: female hobby: ['music'] tasks: - debug: msg: "{{item.0.name}}'s hobby is {{item.1}}" with_subelements: - "{{users}}" - hobby # 指定子元素 -------------------------------------------------------------------------------------------------------- # 使用loop+filter方式實現。 --- - hosts: ck-node1 vars: users: - name: bob gender: male hobby: ['swimming','running'] - name: zoe gender: female hobby: ['music'] tasks: - debug: msg: "{{item.0.name}}'s hobby is {{item.1}}" loop: "{{users | subelements('hobby')}}"
loop_control
在介紹with_indexed_items,已經初步接觸loop_control,它用於控制循環的行為,比如上面的index_var選項可以在遍歷列表時,將元素的索引寫入對應的變量中,下面再介紹一些loop_control的其它選項。
-
pause:用於設置每次循環的時間間隔,以秒為單位。
0 19:31:05 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test1.yaml --- - hosts: ck-node1 tasks: - debug: msg: "{{item}}" loop: [aa,bb,cc] # 寫在下面也可以。 #- aa #- bb #- cc loop_control: pause: 2
-
label:簡化輸出結果,只輸出label指定的標簽。
0 19:45:10 root@ck-ansible,172.16.2.9:/server/ops_ansible # cat test2.yaml --- - hosts: ck-node1 vars: users: bob_info: name: bob sex: male tel: 17621886666 zoe_info: name: zoe sex: female tel: 17621886667 tasks: - debug: msg: "{{item.key}}" loop: "{{users | dict2items}}" loop_control: label: "{{item.key}}"
- loop_var:結合include在第10章節的第5例中講解。
寫作不易,轉載請注明出處,謝謝~~