大网数据广告活动
服务器运维问答

Ansible Post Tasks Execution Condition

· 52CE 我爱测

I'd like to know, in which condition an Ansible post_task will be executed? If the pre-tasks or roles had any failure, would the post-tasks be executed? For example, I'd like to clean up the server if and only if it is stopped. I…

问题原文

原帖链接 · Mincong Huang · CC BY-SA 3.0

I'd like to know, in which condition an Ansible post_task will be executed? If the pre-tasks or roles had any failure, would the post-tasks be executed?

For example, I'd like to clean up the server if and only if it is stopped. In the following script, will the server_dir be removed if server is not stopped correctly?

---

- hosts: webservers

  pre_tasks:
    - shell: echo 'stopping server...'

  roles:
    - { role: stop_server }

  post_tasks:
    - file: path="{{ server_dir }}" state=absent

已采纳解答

原帖链接 · Henrik Pingel · CC BY-SA 3.0

Tasks under post_tasks will not be executed when the previous tasks failed (meaning that the return code of the task isn't 0). That is the default behavior of Ansible and part of the design of Ansible:

Ansible is actually designed to be a “fail-fast” and ordered system [...]

Ansible test_strategies

fail-fast means Ansible will stop the execution of the whole play as soon as one task fails (as long as you don't set ignore_errors: True parameter for the task.)

The documentation on post_tasks is incomplete but if you write yourself a small test playbook and role you will see that Ansible behaves as expected.

Which makes sense because you might want to use pre_tasks and post_tasks to remove and add a node from and to a cluster and you don't want your node to be added to a cluster when the role didn't executed without errors.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。