What does a task do if the managed host is already in the desired state, and what happens when a task fails?
If the state is already correct, the task does nothing. If a task fails, Ansible by default stops running the rest of the playbook for that host.
A task that uses a module generally ensures that one aspect of the machine is in a certain state, for example:
- a file exists with specific permissions and content, or
- a particular file system is mounted.
If the system is not in that state, the task changes it. If it already is, the task does nothing.
When a task fails (say the package repository is unreachable), Ansible's default is to abort the remaining tasks for that host. Other hosts in the same play continue. This prevents later tasks from building on a broken earlier step, such as starting a service whose package never installed. You can change this behaviour (e.g. ignore_errors, block/rescue), but the safe default is to stop.
Go deeper:
Error handling in playbooks (official docs) — the per-host stop-on-failure default,
ignore_errors,any_errors_fatal.Blocks (official docs) —
block/rescue/alwaysfor try-catch style recovery.