block

block 可以将多个task任务组合在一起执行,支持when判断,支持类似python中的try 语句。

python 语法举例:

try:
    print(1/0)
except:
    print(2)
finally:
    print(3)

block 举例:

rescue 字面意识救援

- hosts: all
  tasks:
  - block:
    - name: restart docker 
      service:
        name: docker
        state: restarted
    rescue:
    - name: 如果失败执行我
      debug:
        msg: "block 中的任务失败了,现在该执行我rescue"
    always:
    - name: check the docker service
      shell:
        systemctl status docker
      register: result
      when: 
      - ansible_os_family == "RedHat"
      - ansible_distribution_major_version == "7"
      
    - name: debug
      debug:
        msg: docker 服务在运行
      when: "'running' in result.stdout"
posted @ 2023-01-25 14:05  mingtian是吧  阅读(78)  评论(0编辑  收藏  举报