From 5eb092b3311025403ffb07073558861a1c5d3963 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Thu, 6 Aug 2015 17:19:16 -0400 Subject: [PATCH] Handle top-level vars for include tasks to match v1 syntax The "streamlined" syntax will be deprecated at some point in the future. Fixes #11882 --- lib/ansible/playbook/base.py | 7 +++++++ lib/ansible/playbook/task.py | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py index ce0aee735d4..62af94dd740 100644 --- a/lib/ansible/playbook/base.py +++ b/lib/ansible/playbook/base.py @@ -65,6 +65,13 @@ class Base: # and initialize the base attributes self._initialize_base_attributes() + try: + from __main__ import display + self._display = display + except ImportError: + from ansible.utils.display import Display + self._display = Display() + # The following three functions are used to programatically define data # descriptors (aka properties) for the Attributes of all of the playbook # objects (tasks, blocks, plays, etc). diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index 68a399027a1..5ae056f604f 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -162,6 +162,16 @@ class Task(Base, Conditional, Taggable, Become): new_ds['args'] = args new_ds['delegate_to'] = delegate_to + # we handle any 'vars' specified in the ds here, as we may + # be adding things to them below (special handling for includes). + # When that deprecated feature is removed, this can be too. + if 'vars' in ds: + if not isinstance(ds['vars'], dict): + raise AnsibleError("vars specified on a task must be a dictionary (got a %s)" % type(ds['vars']), obj=new_ds) + new_ds['vars'] = ds.pop('vars') + else: + new_ds['vars'] = dict() + for (k,v) in ds.iteritems(): if k in ('action', 'local_action', 'args', 'delegate_to') or k == action or k == 'shell': # we don't want to re-assign these values, which were @@ -170,7 +180,15 @@ class Task(Base, Conditional, Taggable, Become): elif k.replace("with_", "") in lookup_loader: self._preprocess_loop(ds, new_ds, k, v) else: - new_ds[k] = v + # pre-2.0 syntax allowed variables for include statements at the + # top level of the task, so we move those into the 'vars' dictionary + # here, and show a deprecation message as we will remove this at + # some point in the future. + if action == 'include' and k not in self._get_base_attributes(): + self._display.deprecated("Specifying include variables at the top-level of the task is deprecated. Please see:\nhttp://docs.ansible.com/ansible/playbooks_roles.html#task-include-files-and-encouraging-reuse\n\nfor currently supported syntax regarding included files and variables") + new_ds['vars'][k] = v + else: + new_ds[k] = v return super(Task, self).preprocess_data(new_ds)