mirror of
https://github.com/ansible/ansible.git
synced 2026-08-03 00:22:10 +02:00
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
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user