ignore_errors忽略错误

  • 默认情况下,ansible在遇到错误时会立刻停止playbook,后续所有的任务都不会再执行
    ansible的error处理机制_单任务
---
- name: error处理演示剧本
  hosts: all
  tasks:
    - name: 开启一个不存在的服务
      service: 
        name: noname
        state: started
    - name: 创建一个文件
      file:
        path: /tmp/service.txt
        state: touch

  • 使用ignore_errors可以忽略错误,继续后续的任务
    ansible的error处理机制_ansible_02

单任务忽略错误

---
- name: 单任务error处理演示剧本
  hosts: all
  tasks:
    - name: 开启一个不存在的服务
      service: 
        name: noname
        state: started
      ignore_errors: yes
    - name: 创建一个文件
      file:
        path: /tmp/service.txt
        state: touch

  • 忽略错误后的执行结果
qiaofei@omaidb play % ansible-playbook error.yml

PLAY [error处理演示剧本] *************************************************************

TASK [Gathering Facts] *********************************************************
ok: [c8]
ok: [guajibao]

TASK [开启一个不存在的服务] **************************************************************
fatal: [guajibao]: FAILED! => {"changed": false, "msg": "Could not find the requested service noname: host"}
...ignoring
fatal: [c8]: FAILED! => {"changed": false, "msg": "Could not find the requested service noname: host"}
...ignoring

TASK [创建一个文件] ******************************************************************
changed: [guajibao]
changed: [c8]

PLAY RECAP *********************************************************************
c8                         : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   
guajibao                   : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   

全局忽略错误

单任务忽略错误,只能忽略一个任务的错误,全局忽略错误,不论哪一步出现错误都会忽略错误并继续执行脚本。

---
- name: 全局error处理演示剧本
  hosts: all
  ignore_errors: yes # 全局忽略错误
  tasks:
    - name: 开启一个不存在的服务
      service: 
        name: noname
        state: started
      
    - name: 创建一个文件
      file:
        path: /tmp/service.txt
        state: touch