Skip to content

Commit 7cefbd2

Browse files
Improve failed_when documentation and fix minor errors (#3249)
* Improve failed_when documentation and fix minor errors - Add clarifying example for 'or' operator in failed_when conditions - Fix template syntax: remove extra space in Jinja2 braces Signed-off-by: Piyush Malik <[email protected]> * Update docs/docsite/rst/playbook_guide/playbooks_error_handling.rst Co-authored-by: Felix Fontein <[email protected]> --------- Signed-off-by: Piyush Malik <[email protected]> Co-authored-by: Felix Fontein <[email protected]>
1 parent 86f9d43 commit 7cefbd2

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

docs/docsite/rst/playbook_guide/playbooks_error_handling.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ And at the playbook level:
6161
Resetting unreachable hosts
6262
===========================
6363

64-
If Ansible cannot connect to a host, it marks that host as 'UNREACHABLE' and removes it from the list of active hosts for the run. You can use `meta: clear_host_errors` to reactivate all hosts, so subsequent tasks can try to reach them again.
65-
64+
If Ansible cannot connect to a host, it marks that host as 'UNREACHABLE' and removes it from the list of active hosts for the run. You can use ``meta: clear_host_errors`` to reactivate all hosts, so subsequent tasks can try to reach them again.
6665

6766
.. _handlers_and_failure:
6867

@@ -87,7 +86,16 @@ the handler from running, such as a host becoming unreachable.)
8786
Defining failure
8887
================
8988

90-
Ansible lets you define what "failure" means in each task using the ``failed_when`` conditional. As with all conditionals in Ansible, lists of multiple ``failed_when`` conditions are joined with an implicit ``and``, meaning the task only fails when *all* conditions are met. If you want to trigger a failure when any of the conditions is met, you must define the conditions in a string with an explicit ``or`` operator.
89+
Ansible lets you define what "failure" means in each task using the ``failed_when`` conditional. As with all conditionals in Ansible, lists of multiple ``failed_when`` conditions are joined with an implicit ``and``, meaning the task only fails when *all* conditions are met. If you want to trigger a failure when *any* of the conditions is met, you must define the conditions in a single string with an explicit ``or`` operator.
90+
91+
For example, to fail when either of two conditions is true:
92+
93+
.. code-block:: yaml
94+
95+
- name: Fail task when either condition is met
96+
ansible.builtin.command: /usr/bin/example-command
97+
register: command_result
98+
failed_when: command_result.rc != 0 or 'ERROR' in command_result.stdout
9199
92100
You may check for failure by searching for a word or phrase in the output of a command
93101

@@ -184,7 +192,7 @@ You can reference simple variables in conditionals to avoid repeating certain te
184192
185193
tasks:
186194
- name: Create empty log file
187-
ansible.builtin.shell: mkdir {{ log_path }} || touch {{log_path }}{{ log_file }}
195+
ansible.builtin.shell: mkdir {{ log_path }} || touch {{ log_path }}{{ log_file }}
188196
register: tmp
189197
changed_when:
190198
- tmp.rc == 0

0 commit comments

Comments
 (0)