From ed5ac932a5f8485b107fe5412b32c5d0f51ad8a3 Mon Sep 17 00:00:00 2001 From: Daniel Donckers Date: Tue, 15 Sep 2015 07:54:55 -0600 Subject: [PATCH 1/2] Backport fix for properly use local variables from templates including other templates to 1.9 Fixes #6653 --- lib/ansible/utils/template.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index 9426e254eb5..eae6e5f3f4e 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -15,12 +15,15 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . +from __future__ import print_function +import sys import os import re import codecs import jinja2 from jinja2.runtime import StrictUndefined from jinja2.exceptions import TemplateSyntaxError +from jinja2.utils import missing import yaml import json from ansible import errors @@ -157,16 +160,23 @@ class _jinja2_vars(object): extras is a list of locals to also search for variables. ''' - def __init__(self, basedir, vars, globals, fail_on_undefined, *extras): + def __init__(self, basedir, vars, globals, fail_on_undefined, locals=None, *extras): self.basedir = basedir self.vars = vars self.globals = globals self.fail_on_undefined = fail_on_undefined self.extras = extras + self.locals = dict() + if isinstance(locals, dict): + for key, val in locals.iteritems(): + if key[:2] == 'l_' and val is not missing: + self.locals[key[2:]] = val def __contains__(self, k): if k in self.vars: return True + if k in self.locals: + return True for i in self.extras: if k in i: return True @@ -177,6 +187,8 @@ class _jinja2_vars(object): def __getitem__(self, varname): from ansible.runner import HostVars if varname not in self.vars: + if varname in self.locals: + return self.locals[varname] for i in self.extras: if varname in i: return i[varname] @@ -200,7 +212,7 @@ class _jinja2_vars(object): ''' if locals is None: return self - return _jinja2_vars(self.basedir, self.vars, self.globals, self.fail_on_undefined, locals, *self.extras) + return _jinja2_vars(self.basedir, self.vars, self.globals, self.fail_on_undefined, locals=locals, *self.extras) class J2Template(jinja2.environment.Template): ''' From 57389d55b1722ec4358172fb4c58a61c513148f8 Mon Sep 17 00:00:00 2001 From: Daniel Donckers Date: Wed, 16 Sep 2015 10:31:54 -0600 Subject: [PATCH 2/2] Removing unnecessary import --- lib/ansible/utils/template.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index eae6e5f3f4e..d048686f537 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -15,7 +15,6 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -from __future__ import print_function import sys import os import re