From 62b39d3de535370ae7e98a3fbdcd1799819523ee Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sun, 21 Jul 2013 08:52:00 -0400 Subject: [PATCH] Fix for saving conditionals in variable expressions. --- CHANGELOG.md | 1 + lib/ansible/runner/__init__.py | 3 +-- lib/ansible/runner/action_plugins/group_by.py | 2 +- lib/ansible/utils/__init__.py | 15 +++++++++++---- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aa07bb62b..e88e88ae12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,7 @@ Misc changes: * added Jinja2 filters: skipped, whether a result was skipped * added Jinja2 filters: quote, quotes a string if it needs to be quoted * allow force=yes to affect apt upgrades +* fix for saving conditionals in variable names 1.2.2 "Hear About It Later" (reprise) -- July 4, 2013 diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 7dacfdcb24..9b0860bd41 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -528,8 +528,7 @@ class Runner(object): self.conditional = [ self.conditional ] for cond in self.conditional: - cond = template.template(self.basedir, cond, inject, expand_lists=False) - if not utils.check_conditional(cond): + if not utils.check_conditional(cond, self.basedir, inject): result = utils.jsonify(dict(changed=False, skipped=True)) self.callbacks.on_skipped(host, inject.get('item',None)) return ReturnData(host=host, result=result) diff --git a/lib/ansible/runner/action_plugins/group_by.py b/lib/ansible/runner/action_plugins/group_by.py index 7d2740116d..093196e399 100644 --- a/lib/ansible/runner/action_plugins/group_by.py +++ b/lib/ansible/runner/action_plugins/group_by.py @@ -58,7 +58,7 @@ class ActionModule(object): data = {} data.update(inject) data.update(inject['hostvars'][host]) - if not check_conditional(template.template(self.runner.basedir, self.runner.conditional, data)): + if not check_conditional(self.runner.basedir, self.runner.conditional, data): continue group_name = template.template(self.runner.basedir, args['key'], data) group_name = group_name.replace(' ','-') diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index a5046e12b2..572e101cb1 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -155,7 +155,16 @@ def is_changed(result): return (result.get('changed', False) in [ True, 'True', 'true']) -def check_conditional(conditional): +def check_conditional(conditional, basedir, inject): + + if conditional.startswith("jinja2_compare"): + conditional = conditional.replace("jinja2_compare ","") + # allow variable names + if conditional in inject: + conditional = inject[conditional] + conditional = template.template(basedir, conditional, inject) + # a Jinja2 evaluation that results in something Python can eval! + presented = "{% if " + conditional + " %} True {% else %} False {% endif %}" if not isinstance(conditional, basestring): return conditional @@ -667,9 +676,7 @@ def compile_when_to_only_if(expression): # the stock 'when' without qualification (new in 1.2), assumes Jinja2 terms elif tokens[0] == 'jinja2_compare': - # a Jinja2 evaluation that results in something Python can eval! - presented = "{% if " + " ".join(tokens[1:]).strip() + " %} True {% else %} False {% endif %}" - return presented + return " ".join(tokens) else: raise errors.AnsibleError("invalid usage of when_ operator: %s" % expression)