From 421d67f1ee6e03d9768c5ab966ed3ee8fa3e36df Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Wed, 7 Nov 2018 21:33:54 +0100 Subject: [PATCH] conditional: fix direct boolean "shortcut" (#47941) * conditional: fix direct boolean "shortcut" * Add unit tests --- lib/ansible/playbook/conditional.py | 8 ++++---- test/units/playbook/test_conditional.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/ansible/playbook/conditional.py b/lib/ansible/playbook/conditional.py index 874ba341855..0e846240dc7 100644 --- a/lib/ansible/playbook/conditional.py +++ b/lib/ansible/playbook/conditional.py @@ -92,10 +92,6 @@ class Conditional: ds = getattr(self, '_ds') try: - # this allows for direct boolean assignments to conditionals "when: False" - if isinstance(self.when, bool): - return self.when - for conditional in self.when: if not self._check_conditional(conditional, templar, all_vars): return False @@ -117,6 +113,10 @@ class 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('when statements should not include jinja2 ' 'templating delimiters such as {{ }} or {%% %%}. ' diff --git a/test/units/playbook/test_conditional.py b/test/units/playbook/test_conditional.py index 4e392843fd6..668a66c7477 100644 --- a/test/units/playbook/test_conditional.py +++ b/test/units/playbook/test_conditional.py @@ -1,6 +1,7 @@ from units.compat import unittest from units.mock.loader import DictDataLoader +from units.compat.mock import MagicMock from ansible.plugins.strategy import SharedPluginLoaderObj from ansible.template import Templar @@ -33,6 +34,20 @@ class TestConditional(unittest.TestCase): ret = self._eval_con(when, {}) self.assertTrue(ret) + def test_true_boolean(self): + self.cond.when = [True] + m = MagicMock() + ret = self.cond.evaluate_conditional(m, {}) + self.assertTrue(ret) + self.assertFalse(m.is_template.called) + + def test_false_boolean(self): + self.cond.when = [False] + m = MagicMock() + ret = self.cond.evaluate_conditional(m, {}) + self.assertFalse(ret) + self.assertFalse(m.is_template.called) + def test_undefined(self): when = [u"{{ some_undefined_thing }}"] self.assertRaisesRegexp(errors.AnsibleError, "The conditional check '{{ some_undefined_thing }}' failed",