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