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",