From 9ea54fcaafc90ac02803477027ff91acbc8a39c8 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sat, 12 Oct 2013 17:39:28 -0400 Subject: [PATCH] Fixes a dev branch glitch where a conditional could be applied to multiple tasks. Also improves the errors on parsing bad conditionals to make them less confusing to users. --- lib/ansible/playbook/play.py | 22 +++++++++++++++++++--- lib/ansible/playbook/task.py | 2 +- lib/ansible/utils/__init__.py | 6 ++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index f965770c90..b684fb7f28 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -401,15 +401,29 @@ class Play(object): # ************************************************* - def _load_tasks(self, tasks, vars={}, default_vars={}, sudo_vars={}, additional_conditions=[], original_file=None, role_name=None): + def _load_tasks(self, tasks, vars=None, default_vars=None, sudo_vars=None, additional_conditions=None, original_file=None, role_name=None): ''' handle task and handler include statements ''' results = [] if tasks is None: # support empty handler files, and the like. tasks = [] + if additional_conditions is None: + additional_conditions = [] + if vars is None: + vars = {} + if default_vars is None: + default_vars = {} + if sudo_vars is None: + sudo_vars = {} + + old_conditions = list(additional_conditions) for x in tasks: + + # prevent assigning the same conditions to each task on an include + included_additional_conditions = list(old_conditions) + if not isinstance(x, dict): raise errors.AnsibleError("expecting dict; got: %s" % x) @@ -490,9 +504,11 @@ class Play(object): for x in data: if 'include' in x: x['role_name'] = new_role - results += self._load_tasks(data, mv, default_vars, included_sudo_vars, included_additional_conditions, original_file=include_filename, role_name=new_role) + loaded = self._load_tasks(data, mv, default_vars, included_sudo_vars, list(included_additional_conditions), original_file=include_filename, role_name=new_role) + results += loaded elif type(x) == dict: - results.append(Task(self,x,module_vars=task_vars,default_vars=default_vars,additional_conditions=additional_conditions,role_name=role_name)) + task = Task(self,x,module_vars=task_vars,default_vars=default_vars,additional_conditions=list(additional_conditions),role_name=role_name) + results.append(task) else: raise Exception("unexpected task type") diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index d2a0453608..67c2e1d3cd 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -41,7 +41,7 @@ class Task(object): 'sudo_pass', 'when', 'connection', 'environment', 'args', 'any_errors_fatal', 'changed_when', 'failed_when', 'always_run', 'delay', 'retries', 'until' ] - + def __init__(self, play, ds, module_vars=None, default_vars=None, additional_conditions=None, role_name=None): ''' constructor loads from a task or handler datastructure ''' diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index dd26dbc8a6..bef8dbe140 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -165,6 +165,7 @@ def is_changed(result): def check_conditional(conditional, basedir, inject, fail_on_undefined=False, jinja2=False): + if jinja2: conditional = "jinja2_compare %s" % conditional @@ -174,6 +175,7 @@ def check_conditional(conditional, basedir, inject, fail_on_undefined=False, jin if conditional in inject and str(inject[conditional]).find('-') == -1: conditional = inject[conditional] conditional = template.template(basedir, conditional, inject, fail_on_undefined=fail_on_undefined) + original = conditional.replace("jinja2_compare ","") # a Jinja2 evaluation that results in something Python can eval! presented = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % conditional conditional = template.template(basedir, presented, inject) @@ -188,13 +190,13 @@ def check_conditional(conditional, basedir, inject, fail_on_undefined=False, jin elif conditional.find("is defined") != -1: return False else: - raise errors.AnsibleError("error while evaluating conditional: %s" % conditional) + raise errors.AnsibleError("error while evaluating conditional: %s" % original) elif val == "True": return True elif val == "False": return False else: - raise errors.AnsibleError("unable to evaluate conditional: %s" % conditional) + raise errors.AnsibleError("unable to evaluate conditional: %s" % original) if not isinstance(conditional, basestring): return conditional