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'] }}
pull/191/merge
Michael DeHaan 13 years ago
parent 767282df2a
commit 533c2c6126

@ -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)

@ -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)

@ -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

@ -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 '''

Loading…
Cancel
Save