Skip to content

Commit 19c3ffa

Browse files
S1ructuresamccannoraNod
authored
Update playbooks_error_handling.rst (#2095)
* Update playbooks_error_handling.rst Add example for using own variables in conditions * Apply suggestions from code review Co-authored-by: Don Naro <[email protected]> --------- Co-authored-by: Sandra McCann <[email protected]> Co-authored-by: Don Naro <[email protected]>
1 parent ac0bf97 commit 19c3ffa

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

docs/docsite/rst/playbook_guide/playbooks_error_handling.rst

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,30 @@ You can also combine multiple conditions to override "changed" result.
172172
- '"ERROR" in result.stderr'
173173
- result.rc == 2
174174
175+
You can reference simple variables in conditionals to avoid repeating certain terms, as in the following example:
176+
177+
.. code-block:: yaml
178+
179+
- name: Example playbook
180+
hosts: myHosts
181+
vars:
182+
log_path: /home/ansible/logfolder/
183+
log_file: log.log
184+
185+
tasks:
186+
- name: Create empty log file
187+
ansible.builtin.shell: mkdir {{ log_path }} || touch {{log_path }}{{ log_file }}
188+
register: tmp
189+
changed_when:
190+
- tmp.rc == 0
191+
- 'tmp.stderr != "mkdir: cannot create directory ‘" ~ log_path ~ "’: File exists"'
192+
175193
.. note::
194+
Notice the missing double curly braces ``{{ }}`` around the ``log_path`` variable in the ``changed_when`` statement.
195+
196+
Just like ``when`` these two conditionals do not require templating delimiters (``{{ }}``) because they are raw Jinja2 expressions.
176197

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

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

0 commit comments

Comments
 (0)