RedHat Linux 8 中ansible程序 part5 处理失败任务

如何处理任务失败

忽略任务失败

通常 playbook 遇到错误会中止执行,但是有时我们想要失败时也继续执行
关键字:ignore_errors: yes ,确认忽略错误
例:
安装一个不存在的安装包,如果有错误执行 ignore_errors 忽略错误

---
- name: Test
  hosts: all
  tasks:
    - name: Install
      yum:
        name: xxx
        state: latest
      ignore_errors: yes
...

在这里插入图片描述

任务失败后强制执行处理程序

通常任务失败,play 会终止,那么收到 play 中之前任务通知的处理程序将不会运行,如果要运行,
需要使用关键字:force_handlers:yes ,确认强制执行handlers

---
- name: Test
  hosts: all
  force_handlers: yes
  tasks:
    - name: Test notify
      command: /bin/true
      notify: restart apache

    - name: Install
      yum:
        name: xxx
        state: latest

  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
...

执行后就可以看到就算遇到错误,下面的change执行也会继续
在这里插入图片描述

如果我们将 force_handlers: yes 注释掉,再执行playbook就可以发现Install出现错误后就停止执行了
在这里插入图片描述
注意: 处理程序会在任务报告 changed 结果时获得通知,ok 或者 failed 都不会

指定任务失败条件,指定任务报告 Changed 结果

指定任务失败条件
关键字:failed_when
实验: 编写剧本,查看受管主机apache服务的状态,如果是运行状态则任务失败

---
- name: Test
  hosts: localhost
  tasks:
    - name: Check state of Apache
      command: /usr/bin/systemctl is-active httpd
      register: results_httpd
      failed_when: "'active' in results_httpd.stdout"
...

明显执行后检查到受管主机上httpd服务是开启的,所以任务失败
在这里插入图片描述
fail模块也可以指定任务失败条件,并且可以更加明确指出错误内容

---
- name: Test
  hosts: localhost
  tasks:
    - name: Check state of Apache
      command: /usr/bin/systemctl is-active httpd
      register: results_httpd

    - name: Report
      fail:
        msg: "apache is active"
      when: "'active' in results_httpd.stdout"
...

在这里插入图片描述
.注意: 处理任务失败,即使成功的任务也可以标记为失败

指定任务报告 Changed 结果
关键字:changed_when
changed_when: true,表示确认将操作视为Changed操作
changed_when: false,表示不降操作视为Changed操作
编辑一个剧本,查看apache状态,changed_when: false

---
- name: Test
  hosts: localhost
  tasks:
    - name: Check state of Apache
      command: /usr/bin/systemctl is-active httpd
      changed_when: true
...

执行后结果没有Changed结果
在这里插入图片描述

ansible 块和错误处理

三种关键字:
block:定义要运行的主要任务
rescue:定义要在 block 子句中定义的任务失败时运行的任务
always:定义时中独立运行的任务

在执行剧本时,碰到会失败的任务,可以写如关键字 ignore_errors: yes 去忽略错误,除此之外,我们还可以使用block、rescue、always 去将任务分块然后操作
实验:
编写剧本,安装apache和mariadb服务,并且设置启动mariadb,将apache的安装包名称写错

---
- name: Task failure
  hosts: usersevers
  vars:
    web_pkg: http
    db_pkg: mariadb-server
    db_service: mariadb
  tasks:
    - name: Set up
      block:
        - name: Install {{ web_pkg }} packages
          yum:
            name: "{{ web_pkg }}"
            state: present

      rescue:
        - name: Install {{ db_pkg }} packages
          yum:
            name: "{{ db_pkg }}"
            state: present

      always:
        - name: Start {{ db_service }} service
          service:
            name: "{{ db_service }}"
            state: started
...

执行后 Install http 任务失败后,后面的任务会继续去执行
在这里插入图片描述
将apache的安装包名称改为正确,执行剧本后会在 Install http 成功后跳过安装 Install mariadb 的任务
在这里插入图片描述
所以看来只有在block下的任务失败时,才会执行rescue下的任务,还可以通过给 block 里添加 failed_when:web_pkg == “httpd” 的关键字,使之变为失败任务。
有报错,但是其实已经安装了 httpd 包,failed_when 关键字只是改变了任务的执行状态,没 有改变任务本身。

块用于将任务分组为单元,通过任务是否成功来确定执行其他任务与否

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Howei__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值