> For the complete documentation index, see [llms.txt](https://ansible-book.gitbook.io/ansible-first-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ansible-book.gitbook.io/ansible-first-book/ansible-jin-jie/ansible-de-jiao-ben-playbook/playbok-zhong-de-luo-ji-kong-zhi-yu-ju/kuai-yu-ju-block.md).

# 块语句block

多个action组装成块，可以根据不同条件执行一段语句 ：

```yaml
 tasks:
     - block:
         - yum: name={{ item }} state=installed
           with_items:
             - httpd
             - memcached

         - template: src=templates/src.j2 dest=/etc/foo.conf

         - service: name=bar state=started enabled=True

       when: ansible_distribution == 'CentOS'
       become: true
       become_user: root
```

组装成块处理异常更方便：

```yaml
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"
```
