Ansible is a system for running tasks on one or more hosts, and ensuring that operators understand whether changes have occurred (and indeed whether any issues were encountered). As a result, Ansible tasks result in one of four possible statuses: ok, changed, failed, or skipped. These statuses perform a number of important functions.
From the perspective of an operator running an Ansible playbook, they provide oversight of the Ansible run that completed—whether anything changed or not, and whether there were any failures that need addressing. In addition, they determine the flow of the playbook—for example, if a task results in a changed status, a handler might be triggered in the playbook.
Similarly, if a task results in a failed status, the default behavior of Ansible is not to attempt any further tasks on that host. Tasks can also make use of conditionals that check the status of previous tasks to control operations. As a result, these statuses, or task conditions, are central to just about everything Ansible does, and it is important to understand how to work with them and hence control the flow of a playbook—especially, for example, in a failure condition.
In this chapter, we'll explore this in detail, focusing specifically on the following topics:
- Controlling what defines a failure
- Recovering gracefully from a failure
- Controlling what defines a change
- Iterating over a set of tasks using loops