剧本中的错误处理
当 Ansible 从命令接收到非零返回值或从模块接收到失败时,默认情况下它会停止在该主机上的执行,并继续在其他主机上执行。但是,在某些情况下,您可能需要不同的行为。有时非零返回值表示成功。有时您希望一台主机的失败会停止所有主机上的执行。Ansible 提供了工具和设置来处理这些情况,并帮助您获得所需的行为、输出和报告。
忽略失败的命令
默认情况下,当任务在一台主机上失败时,Ansible 会停止在该主机上执行任务。您可以使用 ignore_errors
来继续执行,尽管失败了。
- name: Do not count this as a failure
ansible.builtin.command: /bin/false
ignore_errors: true
ignore_errors
指令仅在任务可以运行并返回“failed”值时才有效。它不会让 Ansible 忽略未定义的变量错误、连接失败、执行问题(例如,缺少包)或语法错误。
忽略无法访问的主机错误
版本 2.7 中的新增功能。
您可以使用 ignore_unreachable
关键字来忽略由于主机实例为“UNREACHABLE”而导致的任务失败。Ansible 会忽略任务错误,但会继续对无法访问的主机执行以后的任务。例如,在任务级别
- name: This executes, fails, and the failure is ignored
ansible.builtin.command: /bin/true
ignore_unreachable: true
- name: This executes, fails, and ends the play for this host
ansible.builtin.command: /bin/true
以及在剧本级别
- hosts: all
ignore_unreachable: true
tasks:
- name: This executes, fails, and the failure is ignored
ansible.builtin.command: /bin/true
- name: This executes, fails, and ends the play for this host
ansible.builtin.command: /bin/true
ignore_unreachable: false
重置无法访问的主机
如果 Ansible 无法连接到主机,它会将该主机标记为“UNREACHABLE”,并将其从运行的活动主机列表中删除。您可以使用 meta: clear_host_errors 重新激活所有主机,以便后续任务可以再次尝试连接。
处理程序和失败
Ansible 在每个剧本结束时运行 处理程序。如果一个任务通知一个处理程序,但另一个任务在剧本中稍后失败,默认情况下,处理程序不会在该主机上运行,这可能会使主机处于意外状态。例如,一个任务可以更新配置文件并通知处理程序重新启动一些服务。如果在同一剧本中后面的任务失败,配置文件可能会被更改,但服务不会被重新启动。
您可以使用 --force-handlers
命令行选项,通过在剧本中包含 force_handlers: True
,或者通过将 force_handlers = True
添加到 ansible.cfg 来更改此行为。当强制处理程序时,Ansible 会在所有主机上运行所有已通知的处理程序,即使是任务失败的主机。(请注意,某些错误仍可能阻止处理程序运行,例如主机变得无法访问。)
定义失败
Ansible 允许您使用 failed_when
条件在每个任务中定义“失败”的含义。与 Ansible 中的所有条件一样,多个 failed_when
条件的列表以隐式 and
连接,这意味着任务仅在所有条件都满足时才会失败。如果您希望在任何条件满足时触发失败,则必须使用显式 or
运算符在字符串中定义条件。例如
您可以通过在命令的输出中搜索词语或短语来检查失败
- name: Fail task when the command error output prints FAILED
ansible.builtin.command: /usr/bin/example-command -x -y -z
register: command_result
failed_when: "'FAILED' in command_result.stderr"
或根据返回值
- name: Fail task when both files are identical
ansible.builtin.raw: diff foo/file1 bar/file2
register: diff_cmd
failed_when: diff_cmd.rc == 0 or diff_cmd.rc >= 2
您还可以组合多个失败条件。此任务将在两个条件都为真时失败
- name: Check if a file exists in temp and fail task if it does
ansible.builtin.command: ls /tmp/this_should_not_be_here
register: result
failed_when:
- result.rc == 0
- '"No such" not in result.stdout'
如果您希望任务仅在满足一个条件时失败,请将 failed_when
定义更改为
failed_when: result.rc == 0 or "No such" not in result.stdout
如果您有太多条件不适合整齐地放在一行中,可以使用 >
将其拆分为多行 YAML 值。
- name: example of many failed_when conditions with OR
ansible.builtin.shell: "./myBinary"
register: ret
failed_when: >
("No such file or directory" in ret.stdout) or
(ret.stderr != '') or
(ret.rc == 10)
定义“已变更”
Ansible 允许您使用 changed_when
条件来定义某个任务何时“更改”了远程节点。这使您可以根据返回值或输出确定是否应在 Ansible 统计信息中报告更改,以及是否应触发处理程序。与 Ansible 中的所有条件一样,多个 changed_when
条件的列表以隐式 and
连接,这意味着任务仅在所有条件都满足时才会报告更改。如果您希望在任何条件满足时报告更改,则必须使用显式 or
运算符在字符串中定义条件。例如
tasks:
- name: Report 'changed' when the return code is not equal to 2
ansible.builtin.shell: /usr/bin/billybass --mode="take me to the river"
register: bass_result
changed_when: "bass_result.rc != 2"
- name: This will never report 'changed' status
ansible.builtin.shell: wall 'beep'
changed_when: False
- name: This task will always report 'changed' status
ansible.builtin.command: /path/to/command
changed_when: True
您还可以组合多个条件来覆盖“已更改”结果。
- name: Combine multiple conditions to override 'changed' result
ansible.builtin.command: /bin/fake_command
register: result
ignore_errors: True
changed_when:
- '"ERROR" in result.stderr'
- result.rc == 2
注意
与 when
一样,这两个条件不需要模板定界符 ({{ }}
),因为它们是隐式的。
请参阅 定义失败,以获取更多条件语法示例。
确保命令和 shell 成功
command 和 shell 模块关心返回值,因此如果您有一个成功退出代码不为零的命令,您可以执行以下操作
tasks:
- name: Run this command and ignore the result
ansible.builtin.shell: /usr/bin/somecommand || /bin/true
在所有主机上中止剧本
有时您希望一台主机的失败,或一定比例的主机的失败,会中止所有主机上的整个剧本。您可以使用 any_errors_fatal
在第一次失败发生后停止剧本执行。为了更细致地控制,您可以使用 max_fail_percentage
在一定百分比的主机失败后中止运行。
在第一个错误时中止:any_errors_fatal
如果您设置了 any_errors_fatal
并且一个任务返回错误,Ansible 会完成所有主机上当前批次中的致命任务,然后停止在所有主机上执行剧本。不会执行后续任务和剧本。您可以通过将 rescue 部分 添加到块来从致命错误中恢复。您可以在剧本或块级别设置 any_errors_fatal
。
- hosts: somehosts
any_errors_fatal: true
roles:
- myrole
- hosts: somehosts
tasks:
- block:
- include_tasks: mytasks.yml
any_errors_fatal: true
当所有任务必须 100% 成功才能继续执行剧本时,您可以使用此功能。例如,如果您在多个数据中心中运行一台机器上的服务,并且使用负载均衡器将流量从用户传递到服务,那么您希望在停止服务进行维护之前禁用所有负载均衡器。为了确保禁用负载均衡器的任务中发生的任何错误都会停止所有其他任务
---
- hosts: load_balancers_dc_a
any_errors_fatal: true
tasks:
- name: Shut down datacenter 'A'
ansible.builtin.command: /usr/bin/disable-dc
- hosts: frontends_dc_a
tasks:
- name: Stop service
ansible.builtin.command: /usr/bin/stop-software
- name: Update software
ansible.builtin.command: /usr/bin/upgrade-software
- hosts: load_balancers_dc_a
tasks:
- name: Start datacenter 'A'
ansible.builtin.command: /usr/bin/enable-dc
在此示例中,仅当所有负载均衡器都成功禁用后,Ansible 才会开始前端的软件升级。
设置最大失败百分比
默认情况下,Ansible 会持续执行任务,直到所有主机都执行完毕或失败。在某些情况下,例如执行滚动更新时,您可能希望在达到一定失败阈值时中止剧本。为了实现这一点,您可以在剧本中设置最大失败百分比。
---
- hosts: webservers
max_fail_percentage: 30
serial: 10
当您将 max_fail_percentage
设置与 serial 一起使用时,它适用于每批主机。在上面的示例中,如果第一批(或任何一批)服务器中的 10 台服务器中有超过 3 台服务器失败,则剧本的其余部分将被中止。
注意
设置的百分比必须超过,而不是等于。例如,如果 serial 设置为 4,并且您希望在 2 个系统失败时中止剧本,则将 max_fail_percentage 设置为 49 而不是 50。
控制块中的错误
您还可以使用块来定义对任务错误的响应。这种方法类似于许多编程语言中的异常处理。有关详细信息和示例,请参见 使用块处理错误。