Skip to content

Commit e61bff0

Browse files
authored
Update playbooks_error_handling.rst
Add example for using own variables in conditions
1 parent d6eb6c9 commit e61bff0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

docs/docsite/rst/playbook_guide/playbooks_error_handling.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ If you have too many conditions to fit neatly into one line, you can split it in
136136
(ret.stderr != '') or
137137
(ret.rc == 10)
138138
139+
If you want to introduce your own variables, to avoid repeating a certain term, you can simply reference them in your conditionals
140+
141+
-- code-block:: yaml
142+
143+
- name: Example playbook
144+
hosts: myHosts
145+
vars:
146+
log_path: /home/ansible/logfolder/
147+
log_file: log.log
148+
149+
tasks:
150+
- name: Create empty log file
151+
ansible.builtin.shell: mkdir {{ log_path }} || touch {{log_path }}{{ log_file }}
152+
register: tmp
153+
changed_when:
154+
- tmp.rc == 0
155+
- 'tmp.stderr != "mkdir: cannot create directory ‘" ~ log_path ~ "’: File exists"'
156+
157+
-- note::
158+
Notice the missing ``{{ }}`` around log_path. Conditionals are already executed in a templating context, which makes the ``{{ }}`` superflous.
159+
160+
If you still use ansible will raise a warning ``[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}.``
161+
139162
.. _override_the_changed_result:
140163

141164
Defining "changed"

0 commit comments

Comments
 (0)