# Topic: Ansible playbook中的循環(huán)
# State:
# 1. 通過使用循環(huán)可以讓重復(fù)的工作自動來完成
# 2. Ansible playbook 支持超過20中循環(huán)
# Testing environment:
# OS: RHEL6.6
# Python: python2.7.5
# Ansible: ansible 2.3.1.0
# Nodes: 3
#
- 標(biāo)準(zhǔn)循環(huán):
1.1 介紹:
Standard loop 是使用最多的循環(huán)方式棱貌,可以直接減少編寫task的數(shù)據(jù)咬展,使得重復(fù)的task使用一個task即可完成
例如要安裝10個軟件,如果不使用循環(huán)岸梨,就需要編寫10個task, 如果使用循環(huán)編寫一個就夠了元暴。
1.2 用法:- 關(guān)鍵字: with_items
- 說明:
with_items 數(shù)據(jù)結(jié)構(gòu)對應(yīng)的是python或是yaml中的列表篷扩,每個列表項也可以是字典,每次循環(huán)到的當(dāng)前列表項
使用item來引用.
1.3 示例1:
- 標(biāo)準(zhǔn)循環(huán):
# standard loop
-
name: stanard loop
remote_user: root
hosts: tomcat
tasks:- name: loop list
debug: msg={{ item }}
with_items:- I
- am
- Michael
- Lin
- name: loop list of python' dic
debug: msg="{{ item.key }} is {{ item.value }}"
with_items:- { key: 'name', value: 'Michael' }
- { key: 'gender', value: 'male' }
- { key: 'age', value: '30' }
- name: loop list of YAML's map
debug: msg="{{ item['key'] }} is {{ item['value'] }}"
with_items:- key: name
value: Michael - key: gender
value: female - key: age
value: 30
...
- key: name
- name: loop list
- 嵌套循環(huán):
2.1 介紹:
嵌套循環(huán)可以將多個列表或是字典項進(jìn)行排序組合后形成多個列表茉盏,每一個列表都是一個item, 該item可以是對當(dāng)前組合的引用
嵌套循環(huán)可以將多個集合進(jìn)行合并.
2.2 用法:- 關(guān)鍵字:
with_nested - 說明:
with_nested的數(shù)據(jù)結(jié)構(gòu)是一個python或是YAML列表鉴未,這些列表可以經(jīng)過排序組合后形成多個列表枢冤,每個循環(huán)到的列表都被
引用.
2.3 示例1:
- 關(guān)鍵字:
- 嵌套循環(huán):
# nested loop
- name: nested loop
hosts: tomcat
remote_user: root
gather_facts: false
tasks:- name: "nest loop three list"
debug: msg="{{ item[0] }}''{{ item[1] }}'' {{ item[2]}}"
with_nested:- ['A', 'B', 'C']
- ['1', '2', '3']
- ['*', '$', '@']
- name: " nest loop tow map"
debug: msg="{{ item[0] }}{{item[1]}}{{item[2]}}"
with_nested:- {'a', 'b', 'c'}
- {'1', '2', '3'}
- {'#', '%', '!'}
...
- name: "nest loop three list"
- 散列循環(huán):
3.1 介紹:
散列循環(huán)示相對于標(biāo)準(zhǔn)循環(huán)只能使用列表而言的,也就是說刪了循環(huán)完成支持哈希散列铜秆。
3.2 用法:- 關(guān)鍵字:
with_dict - 說明:
with_dict引用的數(shù)據(jù)結(jié)構(gòu)是一個散列掏导,而不是序列。
3.3 示例:
- 關(guān)鍵字:
- 散列循環(huán):
# dict loop example
- name: loop dict
hosts: tomcat
gather_facts: no
vars:
user:
michael:
age: 30
sex: male
tom:
age: 32
sex: female
task:- name: dict loop
debug: msg="{{ item.key }} ' ' {{ item.value.age }} ' ' {{item.value.sex}}"
with_dict: user
- name: dict loop
- 文件匹配循環(huán):
4.1 介紹:
文件循環(huán)用于對匹配的文件進(jìn)行循環(huán)操作羽峰。
4.2 用法:- 關(guān)鍵字:
with_fileglob - 說明:
with_fileglob 引用的數(shù)據(jù)結(jié)構(gòu)可以是列表(列表為測試), item指向當(dāng)前匹配到的文件
4.3 示例:
- 關(guān)鍵字:
- 文件匹配循環(huán):
# file loop example
-
name: loop file
hosts: tomcat
remote_user: root
gather_facts: no
tasks:- name: loop files of the current path
debug: msg={{ item }}
with_fileglob:- ./*.yaml
- ./*.yml
- name: loop files of the current path
- 隨機(jī)循環(huán):
5.1 介紹:
隨機(jī)循環(huán)就是從一個集合中隨機(jī)選擇一個項來執(zhí)行操作,記住是選擇一個!
5.2 用法:- 關(guān)鍵字:
with_random_choice - 說明:
item引用的數(shù)據(jù)結(jié)構(gòu)可以是一個列表(哈希散列沒有測試)
5.3 示例:
- 關(guān)鍵字:
- 隨機(jī)循環(huán):
# random loop example
-
name: random loop the list
hosts: ansible1
remote_user: root
gather_facts: no
tasks:- name: random loop
debug: msg={{ item }}
with_random_choice:
['A', 'B', 'C' ]
- name: random loop
- 條件判斷循環(huán):
6.1 介紹:
條件判斷用來檢測task的執(zhí)行結(jié)果是否符合條件添瓷,如果符合就不再執(zhí)行梅屉,否則繼續(xù)執(zhí)行
6.2 用法:- 關(guān)鍵字:
until - 說明:
until關(guān)鍵字后面跟的是循環(huán)停止的條件
6.3 示例:
- 關(guān)鍵字:
- 條件判斷循環(huán):
# loop until example
-
name: loop until
hosts: tomcat
remote_user: root
gather_facts: no
tasks:- name: loop the file state
shell: cat /etc/ansible/hosts
register: info
until: info.stdout.startswith('ansi')
retries: 5
delay: 5
- name: loop the file state
- 文件優(yōu)先循環(huán):
7.1 介紹:
將第一匹配的文件傳入item
7.2 用法:- 關(guān)鍵字:
with_first_found - 說明:
7.3 示例:
- 關(guān)鍵字:
- 文件優(yōu)先循環(huán):
-
hosts: tomcat
remote_user: root
tasks:- name: INTERFACES | Create Ansible header for /etc/network/interfaces
template:
src: "{{ item }}"
dest: "/etc/foo.conf"
with_first_found:- "{{ ansible_virtualization_type }}_foo.conf"
- "default_foo.conf"
- name: INTERFACES | Create Ansible header for /etc/network/interfaces
- register循環(huán):
8.1 介紹:
該循循環(huán)是在register中存儲多個task執(zhí)行結(jié)果,然后遍歷register中的值
8.2 用法:- 關(guān)鍵字:
- 說明:
當(dāng)循環(huán)完成后使用register來存儲結(jié)果, 然后使用jinja2的語法來遍歷結(jié)果
8.3 示例:
- register循環(huán):
# register loop example
- name: register loop
hosts: tomcat
remote_user: root
tasks:- name: print all the register
shell: "{{ item }}"
with_items:- uname
- whoami
register: its
- name: print its
debug: msg="{% for i in its.results %} {{ i.stdout }} {% endfor %}"
- name: print all the register
...