diff options
author | Alicia Cozine <879121+acozine@users.noreply.github.com> | 2023-03-16 14:47:01 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-16 15:47:01 -0400 |
commit | 507fd1bd60f529681c96fa526ffa7835e122ddcb (patch) | |
tree | 37f7234a54d902ea6e91bc0901e445bf7625dcea | |
parent | 0a6333f7d0038372b2ac00cff3665501e3c2fd59 (diff) | |
download | ansible-507fd1bd60f529681c96fa526ffa7835e122ddcb.tar.gz |
Document debugging conditionals (#80239)
##### SUMMARY
Add a section to the docs describing how to debug conditional statements - how to get Ansible to show you whether your `when:` clause evaluates to `true` or `false`.
I ran into trouble with this and couldn't find anything in the docs. Thought I'd add it.
##### ISSUE TYPE
- Docs Pull Request
+label: docsite_pr
-rw-r--r-- | docs/docsite/rst/playbook_guide/playbooks_conditionals.rst | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/docs/docsite/rst/playbook_guide/playbooks_conditionals.rst b/docs/docsite/rst/playbook_guide/playbooks_conditionals.rst index e1bf42d5cb..1de9ec39c7 100644 --- a/docs/docsite/rst/playbook_guide/playbooks_conditionals.rst +++ b/docs/docsite/rst/playbook_guide/playbooks_conditionals.rst @@ -464,6 +464,44 @@ For example, you can template out a configuration file that is very different be - default.conf mypaths: ['search_location_one/somedir/', '/opt/other_location/somedir/'] +.. _debugging_conditionals: + +Debugging conditionals +====================== + +If your conditional ``when`` statement is not behaving as you intended, you can add a ``debug`` statement to determine if the condition evaluates to ``true`` or ``false``. A common cause of unexpected behavior in conditionals is testing an integer as a string or a string as an integer. To debug a conditional statement, add the entire statement as the ``var:`` value in a ``debug`` task. Ansible then shows the test and how the statement evaluates. For example, here is a set of tasks and sample output: + +.. code-block:: yaml + + - name: check value of return code + ansible.builtin.debug: + var: bar_status.rc + + - name: check test for rc value as string + ansible.builtin.debug: + var: bar_status.rc == "127" + + - name: check test for rc value as integer + ansible.builtin.debug: + var: bar_status.rc == 127 + +.. code-block:: ansible-output + + TASK [check value of return code] ********************************************************************************* + ok: [foo-1] => { + "bar_status.rc": "127" + } + + TASK [check test for rc value as string] ************************************************************************** + ok: [foo-1] => { + "bar_status.rc == \"127\"": false + } + + TASK [check test for rc value as integer] ************************************************************************* + ok: [foo-1] => { + "bar_status.rc == 127": true + } + .. _commonly_used_facts: Commonly-used facts |