Skip to content

Commit

Permalink
Update playbooks_error_handling.rst
Browse files Browse the repository at this point in the history
Add example for using own variables in conditions
  • Loading branch information
S1ructure authored and Florian Hofsäss committed Nov 3, 2024
1 parent d6eb6c9 commit 473045a
Showing 1 changed file with 22 additions and 1 deletion.
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
If you want to introduce your own variables, to avoid repeating a certain term, you can simply reference them in your conditionals

.. 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 ``{{ }}`` around log_path.

Just like ``when`` these two conditionals do not require templating delimiters (``{{ }}``) as they are implied.

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 ``[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.``

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

Expand Down

0 comments on commit 473045a

Please sign in to comment.