Commit ed5ac932 by Daniel Donckers

Backport fix for properly use local variables from templates including other templates to 1.9

Fixes #6653
parent d2d3162a
...@@ -15,12 +15,15 @@ ...@@ -15,12 +15,15 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import sys
import os import os
import re import re
import codecs import codecs
import jinja2 import jinja2
from jinja2.runtime import StrictUndefined from jinja2.runtime import StrictUndefined
from jinja2.exceptions import TemplateSyntaxError from jinja2.exceptions import TemplateSyntaxError
from jinja2.utils import missing
import yaml import yaml
import json import json
from ansible import errors from ansible import errors
...@@ -157,16 +160,23 @@ class _jinja2_vars(object): ...@@ -157,16 +160,23 @@ class _jinja2_vars(object):
extras is a list of locals to also search for variables. 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.basedir = basedir
self.vars = vars self.vars = vars
self.globals = globals self.globals = globals
self.fail_on_undefined = fail_on_undefined self.fail_on_undefined = fail_on_undefined
self.extras = extras 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): def __contains__(self, k):
if k in self.vars: if k in self.vars:
return True return True
if k in self.locals:
return True
for i in self.extras: for i in self.extras:
if k in i: if k in i:
return True return True
...@@ -177,6 +187,8 @@ class _jinja2_vars(object): ...@@ -177,6 +187,8 @@ class _jinja2_vars(object):
def __getitem__(self, varname): def __getitem__(self, varname):
from ansible.runner import HostVars from ansible.runner import HostVars
if varname not in self.vars: if varname not in self.vars:
if varname in self.locals:
return self.locals[varname]
for i in self.extras: for i in self.extras:
if varname in i: if varname in i:
return i[varname] return i[varname]
...@@ -200,7 +212,7 @@ class _jinja2_vars(object): ...@@ -200,7 +212,7 @@ class _jinja2_vars(object):
''' '''
if locals is None: if locals is None:
return self 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): class J2Template(jinja2.environment.Template):
''' '''
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment