第十五章 Ansibleplaybook错误处理

一、playbook忽略错误

默认playbook会检测task执行的返回状态,如果遇到错误则会立即终止playbook的后续task执行,然而有些时候playbook即使执行错误了也要让其继续执行。

加入参数:ignore_errors:yes 忽略错误

二、playbook忽略错误使用

    - name: Get PHP Install status
      shell: "rpm -qa | grep php"
      ignore_errors: yes
      register: get_php_install_status

    - name: Install PHP Server
      shell: yum localinstall -y /tmp/*.rpm
      when:
        - ansible_fqdn is match "web*"
        - get_php_install_status.rc != 0

三、playbook错误处理概述

Ansible 通常默认会确保检测模块和命令的返回码并且会快速失败 – 专注于一个错误除非你另作打算.

有时一条命令会返回 0 但那不是报错.有时命令不会总是报告它 ‘改变’ 了远程系统.

四、强制调用handlers

[root@m01 ~]# cat handler.yml 
- hosts: web_group
  force_handlers: yes
  tasks:
    - name: config httpd server
      template:
        src: ./httpd.j2
        dest: /etc/httpd/conf
      notify: 
        - Restart Httpd Server
        - Restart PHP Server

    - name: Install Http Server
      yum:
        name: htttpd
        state: present

    - name: start httpd server
      service:
        name:httpd
        state: started
        enabled: yes

  handlers:
    - name: Restart Httpd Server
      systemd:
        name: httpd
        state: restarted 

    - name: Restart PHP Server
      systemd:
        name: php-fpm
        state: restarted

五、抑制changed

#被管理主机没有发生变化,可以使用参数将change状态改为ok
    - name: Get PHP Install status
      shell: "rpm -qa | grep php"
      ignore_errors: yes
      changed_when: false
      register: get_php_install_status
原文地址:https://www.cnblogs.com/jhno1/p/15723283.html