From a45494a8969c0eb1c3540eb7a75ef56dd3c1383b Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 11 Oct 2013 19:04:26 -0400 Subject: [PATCH] Add warnings feature. --- lib/ansible/playbook/task.py | 6 ++++++ lib/ansible/utils/__init__.py | 10 ++++++++++ lib/ansible/utils/template.py | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index d401a67ac8..34945704f8 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -81,6 +81,10 @@ class Task(object): # code to allow "with_glob" and to reference a lookup plugin named glob elif x.startswith("with_"): + + if isinstance(ds[x], basestring) and '{{' in ds[x]: + utils.warning("It is unneccessary to use '{{' in loops, leave variables in loop expressions bare.") + plugin_name = x.replace("with_","") if plugin_name in utils.plugins.lookup_loader: ds['items_lookup_plugin'] = plugin_name @@ -90,6 +94,8 @@ class Task(object): raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) elif x in [ 'changed_when', 'failed_when', 'when']: + if isinstance(ds[x], basestring) and '{{' in ds[x]: + utils.warning("It is unneccessary to use '{{' in conditionals, leave variables in loop expressions bare.") ds[x] = "jinja2_compare %s" % (ds[x]) elif x.startswith("when_"): utils.deprecated("The 'when_' conditional is a deprecated syntax as of 1.2. Switch to using the regular unified 'when' statements as described in ansibleworks.com/docs/.","1.5") diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index dbb14e2e5c..dd26dbc8a6 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -47,6 +47,7 @@ VERBOSITY=0 # list of all deprecation messages to prevent duplicate display deprecations = {} +warnings = {} MAX_FILE_SIZE_FOR_DIFF=1*1024*1024 @@ -969,7 +970,16 @@ def deprecated(msg, version): display(new_msg, color='purple', stderr=True) deprecations[new_msg] = 1 +def warning(msg): + new_msg = "\n[WARNING]: %s" % msg + wrapped = textwrap.wrap(new_msg, 79) + new_msg = "\n".join(wrapped) + "\n" + if new_msg not in warnings: + display(new_msg, color='bright purple', stderr=True) + warnings[new_msg] = 1 + def combine_vars(a, b): + if C.DEFAULT_HASH_BEHAVIOUR == "merge": return merge_hash(a, b) else: diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index 92b77335d9..d7f69b1a1f 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -295,7 +295,7 @@ def legacy_varReplace(basedir, raw, vars, lookup_fatal=True, depth=0, expand_lis result = ''.join(done) if result != raw: - import utils + from ansible import utils utils.deprecated("Legacy variable subsitution, such as using ${foo} or $foo instead of {{ foo }} is currently valid but will be phased out and has been out of favor since version 1.2. This is the last of legacy features on our deprecation list. You may continue to use this if you have specific needs for now","1.6") return result