Ansible 小手册系列 十五(Blocks 分组)

当我们想在满足一个条件下,执行多个任务时,就需要分组了。而不再每个任务都要用when。

  tasks: 
   - block:
     - command: echo 1
     - shell: echo 2
     - raw: echo 3
     when: ansible_distribution == 'CentOS'

错误处理

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中的任务在执行中,如果有任何错误,将执行rescue中的任务。 无论在block和rescue中发生或没有发生错误,always部分都运行。

发生错误后,运行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'

测试的时候,设置meta: flush_handlers时,handlers 不执行,rescue的任务还是执行的。



posted @ 2018-03-26 15:13  wanstack  阅读(336)  评论(0编辑  收藏  举报