Ansible——处理任务失败

忽略任务失败

Ansible 默认会检查命令和模块的返回状态,并进行相应的错误处理,默认是遇到错误就中断 playbook 的执行,这些默认行为都是可以改变的,可以通过 ignore_errors 忽略返回状态码

[root@localhost project]# !vim
vim ceshi1.yml 
---
- hosts: 192.168.190.134
  tasks:
    - command: ls /sdadadsdad            这个文件是不存在的,如果正常执行playbook会出错并不会执行下面的task
      ignore_errors: yes                 忽略错误
    - command: echo 'hello'               继续执行此task

TASK [command] *****************************************************************************************
fatal: [192.168.190.134]: FAILED! => {"changed": true, "cmd": ["ls", "/sdadadsdad"], "delta": "0:00:00.003481", "end": "2020-09-08 09:23:22.735623", "msg": "non-zero return code", "rc": 2, "start": "2020-09-08 09:23:22.732142", "stderr": "ls: cannot access '/sdadadsdad': No such file or directory", "stderr_lines": ["ls: cannot access '/sdadadsdad': No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [command] *****************************************************************************************
changed: [192.168.190.134]

强制执行任务

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

[root@localhost project]# !vim
vim ceshi1.yml 
---
- hosts: 192.168.190.134
  force_handlers: yes            强制执行handlers
  tasks:
    - name: show the date
      template:
        src: template/date.j2
        dest: /tmp
      notify:
          restart apache
    - name: error task
      command: ls /sdsdsds
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted


TASK [error task] **************************************************************************************
fatal: [192.168.190.134]: FAILED! => {"changed": true, "cmd": ["ls", "/sdsdsds"], "delta": "0:00:00.003698", "end": "2020-09-08 09:36:44.778096", "msg": "non-zero return code", "rc": 2, "start": "2020-09-08 09:36:44.774398", "stderr": "ls: cannot access '/sdsdsds': No such file or directory", "stderr_lines": ["ls: cannot access '/sdsdsds': No such file or directory"], "stdout": "", "stdout_lines": []}

RUNNING HANDLER [restart apache] ***********************************************************************  handlers成功执行。
changed: [192.168.190.134]

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

指定任务失败条件

方法一: 使用failed_when关键字

vim ceshi1.yml 
---
- hosts: 192.168.190.134
  tasks:
    - name: test
      script: files/test.sh            脚本在执行时,就算脚本里有错误,最后也是执行ok或者changed
      register: result
      failed_when: "'No such file or directory' in result.stdout"  加上报错的条件。


成功报错。
TASK [test] ********************************************************************************************  
fatal: [192.168.190.134]: FAILED! => {"changed": true, "failed_when_result": true, "rc": 0, "stderr": "Shared connection to 192.168.190.134 closed.
", "stderr_lines": ["Shared connection to 192.168.190.134 closed."], "stdout": "ls: cannot access 'sdsdsdsdsdsds': No such file or directory
anaconda-ks.cfg
", "stdout_lines": ["ls: cannot access 'sdsdsdsdsdsds': No such file or directory", "anaconda-ks.cfg"]}

方法二:使用fail模块进行报错条件

[root@localhost project]# !vim
vim ceshi1.yml 
---
- hosts: 192.168.190.134
  tasks:
    - name: test
      script: files/test.sh
      register: result
      
    - name: fail control
      fail:
        msg: "you are a fool"       明确错误提示。
      when:
        "'No such file or directory' in result.stdout"      


TASK [fail control] ************************************************************************************
fatal: [192.168.190.134]: FAILED! => {"changed": false, "msg": "you are a fool"}

指定任务何时报告"Changed"结果

利用changed_when关键字来确定是否报告changed结果

[root@localhost project]# vim ceshi1.yml 
---
- hosts: 192.168.190.134
  tasks:
    - name: useradd
      user:
        name: test1
        state: present
      changed_when: True       无论是否成功执行。报告返回结果都为changed.如果为False,在task没有错误的情况下,始终返回ok。

ansible 块和错误处理

三种关键字:

  • block:定义要运行的主要任务
  • rescue:定义要在 block 子句中定义的任务失败时运行的任务
  • always:定义始终独立运行的任务
---
- hosts: 192.168.190.134
  vars:
    file_name: test1
  tasks:
    - name: test block
      block:
        - name: block1
          command: ls /tmp
        - name: block2
          command: ls /tmp/{{ file_name }}               若文件不存在,执行rescue
      rescue:
        - name: touch the {{ file_name }}                 创建此文件
          file:
            name: "{{ file_name }}"
            state: touch
      always:                                            无论block成功与否,都执行always
        - name: print
          command: echo "(@^0^@)/"
          register: result
        - debug:
            msg: " {{ result.stdout }} "                              
原文地址:https://www.cnblogs.com/sawyer95/p/13631343.html