From 28cf4bc00b263163cc1ed7844f9aef63c9e1893f Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 1 Feb 2016 18:54:09 -0500 Subject: [PATCH 1/2] fix incorrect environment processing it was assumed it could only be a dict or string (it starts out as a list) also a 2nd assumption that bare vars only would appear in one of the dict keys. removed deprecation warnings from here as they should be signaled in the bare conversion itself. --- lib/ansible/playbook/task.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index 6bd3caaca5b..4328602f593 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -244,11 +244,21 @@ class Task(Base, Conditional, Taggable, Become): if value is None: return dict() - for env_item in value: - if isinstance(env_item, (string_types, AnsibleUnicode)) and env_item in templar._available_variables.keys(): - display.deprecated("Using bare variables for environment is deprecated." - " Update your playbooks so that the environment value uses the full variable syntax ('{{foo}}')") - break + elif isinstance(value, list): + if len(value) == 1: + return templar.template(value[0], convert_bare=True) + else: + env = [] + for env_item in value: + if isinstance(env_item, (string_types, AnsibleUnicode)) and env_item in templar._available_variables.keys(): + env[env_item] = templar.template(env_item, convert_bare=True) + elif isinstance(value, dict): + env = dict() + for env_item in value: + if isinstance(env_item, (string_types, AnsibleUnicode)) and env_item in templar._available_variables.keys(): + env[env_item] = templar.template(value[env_item], convert_bare=True) + + # at this point it should be a simple string return templar.template(value, convert_bare=True) def _post_validate_changed_when(self, attr, value, templar): From dc15eb806e1b5c040aedb0565218d26c369eb453 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 1 Feb 2016 18:59:14 -0500 Subject: [PATCH 2/2] deprecate all bare variable conversions (not debug) now deprecation message appears with variable name in all spots where this occurs debug's var= option is excluded as this is only place where bare variables shold actually be accepted. --- lib/ansible/plugins/action/debug.py | 2 +- lib/ansible/template/__init__.py | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/ansible/plugins/action/debug.py b/lib/ansible/plugins/action/debug.py index 2af20eddfc4..f07d8ea5d05 100644 --- a/lib/ansible/plugins/action/debug.py +++ b/lib/ansible/plugins/action/debug.py @@ -46,7 +46,7 @@ class ActionModule(ActionBase): elif 'var' in self._task.args: try: - results = self._templar.template(self._task.args['var'], convert_bare=True, fail_on_undefined=True) + results = self._templar.template(self._task.args['var'], convert_bare=True, fail_on_undefined=True, bare_deprecated=False) if results == self._task.args['var']: raise AnsibleUndefinedVariable except AnsibleUndefinedVariable: diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 8ce2358eb1e..fdc8eba720e 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -46,6 +46,12 @@ except ImportError: from numbers import Number +try: + from __main__ import display +except ImportError: + from ansible.utils.display import Display + display = Display() + __all__ = ['Templar'] # A regex for checking to see if a variable we're trying to @@ -269,7 +275,7 @@ class Templar: self._available_variables = variables self._cached_result = {} - def template(self, variable, convert_bare=False, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None, convert_data=True, static_vars = [''], cache = True): + def template(self, variable, convert_bare=False, preserve_trailing_newlines=True, escape_backslashes=True, fail_on_undefined=None, overrides=None, convert_data=True, static_vars = [''], cache = True, bare_deprecated=True): ''' Templates (possibly recursively) any given data as input. If convert_bare is set to True, the given data will be wrapped as a jinja2 variable ('{{foo}}') @@ -290,7 +296,7 @@ class Templar: try: if convert_bare: - variable = self._convert_bare_variable(variable) + variable = self._convert_bare_variable(variable, bare_deprecated=bare_deprecated) if isinstance(variable, string_types): result = variable @@ -366,7 +372,7 @@ class Templar: ''' return self.environment.block_start_string in data or self.environment.variable_start_string in data - def _convert_bare_variable(self, variable): + def _convert_bare_variable(self, variable, bare_deprecated): ''' Wraps a bare string, which may have an attribute portion (ie. foo.bar) in jinja2 variable braces so that it is evaluated properly. @@ -376,6 +382,9 @@ class Templar: contains_filters = "|" in variable first_part = variable.split("|")[0].split(".")[0].split("[")[0] if (contains_filters or first_part in self._available_variables) and self.environment.variable_start_string not in variable: + if bare_deprecated: + display.deprecated("Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax ('%s%s%s')" % + (self.environment.variable_start_string, variable, self.environment.variable_end_string)) return "%s%s%s" % (self.environment.variable_start_string, variable, self.environment.variable_end_string) # the variable didn't meet the conditions to be converted,