Skip to content

Update playbooks_error_handling.rst #2095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion docs/docsite/rst/playbook_guide/playbooks_error_handling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,30 @@ You can also combine multiple conditions to override "changed" result.
- '"ERROR" in result.stderr'
- result.rc == 2

You can reference simple variables in conditionals to avoid repeating certain terms, as in the following example:

.. code-block:: yaml

- name: Example playbook
hosts: myHosts
vars:
log_path: /home/ansible/logfolder/
log_file: log.log

tasks:
- name: Create empty log file
ansible.builtin.shell: mkdir {{ log_path }} || touch {{log_path }}{{ log_file }}
register: tmp
changed_when:
- tmp.rc == 0
- 'tmp.stderr != "mkdir: cannot create directory ‘" ~ log_path ~ "’: File exists"'

.. note::
Notice the missing double curly braces ``{{ }}`` around the ``log_path`` variable in the ``changed_when`` statement.

Just like ``when`` these two conditionals do not require templating delimiters (``{{ }}``) because they are raw Jinja2 expressions.

Just like ``when`` these two conditionals do not require templating delimiters (``{{ }}``) as they are implied.
If you still use them, ansible will raise a warning that conditional statements should not include jinja2 templating delimiters.

See :ref:`controlling_what_defines_failure` for more conditional syntax examples.

Expand Down