From f7ade8e61c11e1b465e56be3ac18bdcc3b8163f4 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 19 Aug 2020 12:55:17 -0400 Subject: [PATCH] allow per conditonal item debugging (#70966) * allow per conditonal item debugging * offloaded a bit from _check_c --- changelogs/fragments/which_when_false.yml | 2 ++ lib/ansible/playbook/conditional.py | 34 ++++++++++++++--------- 2 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 changelogs/fragments/which_when_false.yml diff --git a/changelogs/fragments/which_when_false.yml b/changelogs/fragments/which_when_false.yml new file mode 100644 index 00000000000..cb322508972 --- /dev/null +++ b/changelogs/fragments/which_when_false.yml @@ -0,0 +1,2 @@ +minor_changes: + - Add which conditional is being evaluated at each step when debugging. diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py index efcacb34bd5..fe5e353d64d 100644 --- a/lib/ansible/playbook/conditional.py +++ b/lib/ansible/playbook/conditional.py @@ -88,16 +88,30 @@ class Conditional: if hasattr(self, '_ds'): ds = getattr(self, '_ds') + result = True try: for conditional in self.when: - if not self._check_conditional(conditional, templar, all_vars): - return False - except Exception as e: - raise AnsibleError( - "The conditional check '%s' failed. The error was: %s" % (to_native(conditional), to_native(e)), obj=ds - ) - return True + # do evaluation + if conditional is None or conditional == '': + res = True + elif isinstance(conditional, bool): + res = conditional + else: + res = self._check_conditional(conditional, templar, all_vars) + + # only update if still true, preserve false + if result: + result = res + + display.debug("Evaluated conditional (%s): %s " % (conditional, res)) + if not result: + break + + except Exception as e: + raise AnsibleError("The conditional check '%s' failed. The error was: %s" % (to_native(conditional), to_native(e)), obj=ds) + + return result def _check_conditional(self, conditional, templar, all_vars): ''' @@ -107,12 +121,6 @@ class Conditional: ''' original = conditional - if conditional is None or conditional == '': - return True - - # this allows for direct boolean assignments to conditionals "when: False" - if isinstance(conditional, bool): - return conditional if templar.is_template(conditional): display.warning('conditional statements should not include jinja2 '