當(dāng)我們想在滿足一個(gè)條件下窟扑,執(zhí)行多個(gè)任務(wù)時(shí),就需要分組了袋马。而不再每個(gè)任務(wù)都要用when诽里。
tasks:
- block:
- command: echo 1
- shell: echo 2
- raw: echo 3
when: ansible_distribution == 'CentOS'
錯(cuò)誤處理
tasks:
- block:
- debug: msg='i execute normally'
- command: /bin/false
- debug: msg='i never execute, cause ERROR!'
rescue:
- debug: msg='I caught an error'
- command: /bin/false
- debug: msg='I also never execute :-('
always:
- debug: msg="this always executes"
block中的任務(wù)在執(zhí)行中,如果有任何錯(cuò)誤飞蛹,將執(zhí)行rescue中的任務(wù)谤狡。 無論在block和rescue中發(fā)生或沒有發(fā)生錯(cuò)誤,always部分都運(yùn)行卧檐。
發(fā)生錯(cuò)誤后墓懂,運(yùn)行handlers
tasks:
- block:
- debug: msg='i execute normally'
notify: run me even after an error
- command: /bin/false
rescue:
- name: make sure all handlers run
meta: flush_handlers
handlers:
- name: run me even after an error
debug: msg='this handler runs even on error'
測試的時(shí)候,設(shè)置meta: flush_handlers時(shí)霉囚,handlers 不執(zhí)行捕仔,rescue的任務(wù)還是執(zhí)行的。
更多文章請(qǐng)看 Ansible 專題文章總覽