From 533c2c61269e0a4888a60e5843ebb1682c9257e8 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sat, 21 Apr 2012 12:45:37 -0400 Subject: Make it possible to use facts from hosts in templates for other hosts. It works like this: {{ hostvars['127.0.0.1']['ansible_eth0']['ipv4']['address'] }} --- hacking/test-module | 6 +++--- lib/ansible/playbook.py | 10 +++++----- lib/ansible/runner.py | 12 ++++++------ lib/ansible/utils.py | 9 +++++---- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/hacking/test-module b/hacking/test-module index 971b56a11c..ce31b3bffc 100755 --- a/hacking/test-module +++ b/hacking/test-module @@ -30,7 +30,7 @@ import sys import os import subprocess import traceback -import ansible.utils +from ansible import utils try: import json @@ -70,7 +70,7 @@ try: print "***********************************" print "RAW OUTPUT" print out - results = ansible.utils.parse_json(out) + results = utils.parse_json(out) except: print "***********************************" @@ -82,7 +82,7 @@ except: print "***********************************" print "PARSED OUTPUT" -print results +print utils.bigjson(results) sys.exit(0) diff --git a/lib/ansible/playbook.py b/lib/ansible/playbook.py index 1b8e557791..ed9452ecd6 100755 --- a/lib/ansible/playbook.py +++ b/lib/ansible/playbook.py @@ -207,10 +207,10 @@ class PlayBook(object): if action is None: raise errors.AnsibleError('action is required') produced_task = task.copy() - produced_task['action'] = utils.template(action, dict(item=item)) - produced_task['name'] = utils.template(name, dict(item=item)) + produced_task['action'] = utils.template(action, dict(item=item), SETUP_CACHE) + produced_task['name'] = utils.template(name, dict(item=item), SETUP_CACHE) if only_if: - produced_task['only_if'] = utils.template(only_if, dict(item=item)) + produced_task['only_if'] = utils.template(only_if, dict(item=item), SETUP_CACHE) new_tasks2.append(produced_task) else: new_tasks2.append(task) @@ -465,7 +465,7 @@ class PlayBook(object): found = False sequence = [] for real_filename in filename: - filename2 = utils.path_dwim(self.basedir, utils.template(real_filename, cache_vars)) + filename2 = utils.path_dwim(self.basedir, utils.template(real_filename, cache_vars, SETUP_CACHE)) sequence.append(filename2) if os.path.exists(filename2): found = True @@ -481,7 +481,7 @@ class PlayBook(object): ) else: - filename2 = utils.path_dwim(self.basedir, utils.template(filename, cache_vars)) + filename2 = utils.path_dwim(self.basedir, utils.template(filename, cache_vars, SETUP_CACHE)) if not os.path.exists(filename2): raise errors.AnsibleError("no file matched for vars_file import: %s" % filename2) data = utils.parse_yaml_from_file(filename2) diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index c305126b34..c7ef3663bf 100755 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -268,7 +268,7 @@ class Runner(object): ''' runs a module that has already been transferred ''' inject = self.setup_cache.get(conn.host,{}) - conditional = utils.double_template(self.conditional, inject) + conditional = utils.double_template(self.conditional, inject, self.setup_cache) if not eval(conditional): return [ utils.smjson(dict(skipped=True)), None, 'skipped' ] @@ -281,7 +281,7 @@ class Runner(object): if type(args) == dict: args = utils.bigjson(args) - args = utils.template(args, inject) + args = utils.template(args, inject, self.setup_cache) module_name_tail = remote_module_path.split("/")[-1] @@ -369,7 +369,7 @@ class Runner(object): # apply templating to source argument inject = self.setup_cache.get(conn.host,{}) - source = utils.template(source, inject) + source = utils.template(source, inject, self.setup_cache) # transfer the file to a remote tmp location tmp_src = tmp + source.split('/')[-1] @@ -456,7 +456,7 @@ class Runner(object): # apply templating to source argument so vars can be used in the path inject = self.setup_cache.get(conn.host,{}) - source = utils.template(source, inject) + source = utils.template(source, inject, self.setup_cache) (host, ok, data, err) = (None, None, None, None) @@ -491,7 +491,7 @@ class Runner(object): source_data = file(utils.path_dwim(self.basedir, source)).read() resultant = '' try: - resultant = utils.template(source_data, inject) + resultant = utils.template(source_data, inject, self.setup_cache) except Exception, e: return (host, False, dict(failed=True, msg=str(e)), '') xfered = self._transfer_str(conn, tmp, 'source', resultant) @@ -537,7 +537,7 @@ class Runner(object): return [ host, False, "FAILED: %s" % str(e), None ] cache = self.setup_cache.get(host, {}) - module_name = utils.template(self.module_name, cache) + module_name = utils.template(self.module_name, cache, self.setup_cache) tmp = self._get_tmp_path(conn) result = None diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index d1bb9490e6..ac4228f2c1 100755 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -33,7 +33,6 @@ except ImportError: from ansible import errors import ansible.constants as C - ############################################################### # UTILITY FUNCTIONS FOR COMMAND LINE TOOLS ############################################################### @@ -239,14 +238,16 @@ def varReplace(raw, vars): return ''.join(done) -def template(text, vars): +def template(text, vars, setup_cache): ''' run a text buffer through the templating engine ''' + vars = vars.copy() text = varReplace(str(text), vars) + vars['hostvars'] = setup_cache template = jinja2.Template(text) return template.render(vars) -def double_template(text, vars): - return template(template(text, vars), vars) +def double_template(text, vars, setup_cache): + return template(template(text, vars, setup_cache), vars, setup_cache) def template_from_file(path, vars): ''' run a file through the templating engine ''' -- cgit v1.2.1