Commit 5f69c557 by Michael DeHaan

Fix for the way host variables were being swallowed, plus some overall…

Fix for the way host variables were being swallowed, plus some overall simplification.   Deciding what
dictionary to use for templating now happens in exactly one place (executor_internal) and the "inject"
dictionary is passed to what needs it.
parent 9ef7168a
......@@ -281,6 +281,7 @@ class PlayBook(object):
# let runner template out future commands
setup_ok = setup_results.get('contacted', {})
for (host, result) in setup_ok.iteritems():
facts = result.get('ansible_facts', {})
self.SETUP_CACHE[host] = result.get('ansible_facts', {})
return setup_results
......
......@@ -45,7 +45,7 @@ class Play(object):
raise errors.AnsibleError('hosts declaration is required')
elif isinstance(hosts, list):
hosts = ';'.join(hosts)
hosts = utils.template(hosts, playbook.extra_vars, {})
hosts = utils.template(hosts, playbook.extra_vars)
self._ds = ds
self.playbook = playbook
......
......@@ -192,14 +192,17 @@ def varReplace(raw, vars):
return ''.join(done)
def _template(text, vars, setup_cache=None):
def _template(text, vars):
''' run a text buffer through the templating engine '''
if vars is None:
raise Exception('vars is none')
vars = vars.copy()
vars['hostvars'] = setup_cache
# FIXME: do this in runner code
vars['hostvars'] = vars.get('setup_cache', {})
return varReplace(unicode(text), vars)
def template(text, vars, setup_cache=None):
def template(text, vars):
''' run a text buffer through the templating engine until it no longer changes '''
prev_text = ''
......@@ -209,21 +212,22 @@ def template(text, vars, setup_cache=None):
if (depth > 20):
raise errors.AnsibleError("template recursion depth exceeded")
prev_text = text
text = _template(text, vars, setup_cache)
text = _template(text, vars)
return text
def template_from_file(basedir, path, vars, setup_cache):
def template_from_file(basedir, path, vars):
''' run a file through the templating engine '''
environment = jinja2.Environment(loader=jinja2.FileSystemLoader(basedir), trim_blocks=False)
data = codecs.open(path_dwim(basedir, path), encoding="utf8").read()
t = environment.from_string(data)
# FIXME: possibly a bit inefficient here, do this in runner code
vars = vars.copy()
vars['hostvars'] = setup_cache
vars['hostvars'] = vars.get('setup_cache',{})
res = t.render(vars)
if data.endswith('\n') and not res.endswith('\n'):
res = res + '\n'
return template(res, vars, setup_cache)
return template(res, vars)
def parse_yaml(data):
''' convert a yaml string to a data structure '''
......
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