linux学习

Ansible 中任务块

任务块

playbook中会定义三种块:block、rescue、always,作用方式分别如下:

  • block:定义运行的主任务
  • rescue:定义在block任务失败后,执行的任务
  • always:始种独立执行的任务

block

Ansible通过block关键字,将多个任务组合到一起,合并成为一个逻辑组,可以去执行整个block块的任务组。

如果webservers组中的主机系统发行版是Rocky,则安装并启动nginx,代码示意如下:

[root@pubserver ansible]# vim block1.yml
---
- name: block tasks
  hosts: webservers
  tasks:
    - name: define a group of tasks
      block:
        - name: install nginx
          yum:                      # 安装nginx
            name: nginx
            state: present

        - name: start nginx
          service:                  # 开启nginx服务
            name: nginx
            state: started
            enabled: yes

      when: ansible_distribution == "Rocky"   # 判断发行版是否为Rocky

[root@pubserver ansible]# ansible-playbook block1.yml 

    ..................

PLAY RECAP ********************************************************************************************************************
web1                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web2                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

rescue 和 always

往往也将block和rescue、always联合使用:

  • block中的tests如果正确运行,不会运行rescue

  • block里的tasks如果运行失败,会运行rescue里的tasks

  • block和rescue里的tasks无论是否运行成功,都会运行always里的tasks

    block、rescue、always练习代码示意如下:

[root@pubserver ansible]# vim block2.yml
---
- name: block test
  hosts: webservers
  tasks:
    - name: block rescue always test1
      block:                                # 正确执行
        - name: touch file test1.txt
          file:
            path: /tmp/test1.txt
            state: touch
      rescue:                               # block正确执行,rescue将不执行
        - name: touch file test2.txt
          file:
            path: /tmp/test2.txt
            state: touch
      always:                               # 都执行
        - name: touch file test3.txt
          file:
            path: /tmp/test3.txt
            state: touch
# 运行playbook
[root@pubserver ansible]# ansible-playbook block2.yml 
        ............

PLAY RECAP *****************************************************************************************************************************************
web1                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web2                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

# 检测webservers组主机是否创建文件
[root@web1 ~]# ls -ld /tmp/test*
-rw-r--r--. 1 root root 0 8月  30 21:22 /tmp/test1.txt
-rw-r--r--. 1 root root 0 8月  30 21:22 /tmp/test3.txt
# 其中block正确执行,因此不会执行rescue块,不会创建/tmp/test2.txt
[root@pubserver ansible]# vim block3.yml 
---
- name: block test
  hosts: webservers
  tasks:
    - name: block rescue always test2
      block:                            # 将执行失败
        - name: touch file test11.txt
          file:
            path: /tmp/abcd/test11.txt
            state: touch
      rescue:                           # block 执行失败,rescue将执行
        - name: touch file test22.txt
          file:
            path: /tmp/test22.txt
            state: touch
      always:                           # 都执行
        - name: touch file test33.txt
          file:
            path: /tmp/test33.txt
            state: touch

# 执行playbook
[root@pubserver ansible]# ansible-playbook block3.yml 
    ..............

TASK [touch file test11.txt] ***********************************************************************************************************************
fatal: [web2]: FAILED! => {"changed": false, "msg": "Error, could not touch target: [Errno 2] 没有那个文件或目录: b'/tmp/abcd/test11.txt'", "path": "/tmp/abcd/test11.txt"}
fatal: [web1]: FAILED! => {"changed": false, "msg": "Error, could not touch target: [Errno 2] 没有那个文件或目录: b'/tmp/abcd/test11.txt'", "path": "/tmp/abcd/test11.txt"}

    ..............

PLAY RECAP *****************************************************************************************************************************************
web1                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0   
web2                       : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0

# 检测webservers组主机是否创建文件
[root@web1 ~]# ls -ld /tmp/test??.txt
-rw-r--r--. 1 root root 0 8月  30 21:37 /tmp/test22.txt
-rw-r--r--. 1 root root 0 8月  30 21:37 /tmp/test33.txt
# 其中block没有正确执行,因此执行rescue块,创建/tmp/test22.txt

一条评论

回复 Patrick Conlin 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注