1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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'] }}
This commit is contained in:
Michael DeHaan 2012-04-21 12:45:37 -04:00
parent 767282df2a
commit 533c2c6126
4 changed files with 19 additions and 18 deletions

View file

@ -30,7 +30,7 @@ import sys
import os import os
import subprocess import subprocess
import traceback import traceback
import ansible.utils from ansible import utils
try: try:
import json import json
@ -70,7 +70,7 @@ try:
print "***********************************" print "***********************************"
print "RAW OUTPUT" print "RAW OUTPUT"
print out print out
results = ansible.utils.parse_json(out) results = utils.parse_json(out)
except: except:
print "***********************************" print "***********************************"
@ -82,7 +82,7 @@ except:
print "***********************************" print "***********************************"
print "PARSED OUTPUT" print "PARSED OUTPUT"
print results print utils.bigjson(results)
sys.exit(0) sys.exit(0)

View file

@ -207,10 +207,10 @@ class PlayBook(object):
if action is None: if action is None:
raise errors.AnsibleError('action is required') raise errors.AnsibleError('action is required')
produced_task = task.copy() produced_task = task.copy()
produced_task['action'] = utils.template(action, dict(item=item)) produced_task['action'] = utils.template(action, dict(item=item), SETUP_CACHE)
produced_task['name'] = utils.template(name, dict(item=item)) produced_task['name'] = utils.template(name, dict(item=item), SETUP_CACHE)
if only_if: 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) new_tasks2.append(produced_task)
else: else:
new_tasks2.append(task) new_tasks2.append(task)
@ -465,7 +465,7 @@ class PlayBook(object):
found = False found = False
sequence = [] sequence = []
for real_filename in filename: 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) sequence.append(filename2)
if os.path.exists(filename2): if os.path.exists(filename2):
found = True found = True
@ -481,7 +481,7 @@ class PlayBook(object):
) )
else: 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): if not os.path.exists(filename2):
raise errors.AnsibleError("no file matched for vars_file import: %s" % filename2) raise errors.AnsibleError("no file matched for vars_file import: %s" % filename2)
data = utils.parse_yaml_from_file(filename2) data = utils.parse_yaml_from_file(filename2)

View file

@ -268,7 +268,7 @@ class Runner(object):
''' runs a module that has already been transferred ''' ''' runs a module that has already been transferred '''
inject = self.setup_cache.get(conn.host,{}) 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): if not eval(conditional):
return [ utils.smjson(dict(skipped=True)), None, 'skipped' ] return [ utils.smjson(dict(skipped=True)), None, 'skipped' ]
@ -281,7 +281,7 @@ class Runner(object):
if type(args) == dict: if type(args) == dict:
args = utils.bigjson(args) 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] module_name_tail = remote_module_path.split("/")[-1]
@ -369,7 +369,7 @@ class Runner(object):
# apply templating to source argument # apply templating to source argument
inject = self.setup_cache.get(conn.host,{}) 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 # transfer the file to a remote tmp location
tmp_src = tmp + source.split('/')[-1] 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 # apply templating to source argument so vars can be used in the path
inject = self.setup_cache.get(conn.host,{}) 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) (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() source_data = file(utils.path_dwim(self.basedir, source)).read()
resultant = '' resultant = ''
try: try:
resultant = utils.template(source_data, inject) resultant = utils.template(source_data, inject, self.setup_cache)
except Exception, e: except Exception, e:
return (host, False, dict(failed=True, msg=str(e)), '') return (host, False, dict(failed=True, msg=str(e)), '')
xfered = self._transfer_str(conn, tmp, 'source', resultant) xfered = self._transfer_str(conn, tmp, 'source', resultant)
@ -537,7 +537,7 @@ class Runner(object):
return [ host, False, "FAILED: %s" % str(e), None ] return [ host, False, "FAILED: %s" % str(e), None ]
cache = self.setup_cache.get(host, {}) 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) tmp = self._get_tmp_path(conn)
result = None result = None

View file

@ -33,7 +33,6 @@ except ImportError:
from ansible import errors from ansible import errors
import ansible.constants as C import ansible.constants as C
############################################################### ###############################################################
# UTILITY FUNCTIONS FOR COMMAND LINE TOOLS # UTILITY FUNCTIONS FOR COMMAND LINE TOOLS
############################################################### ###############################################################
@ -239,14 +238,16 @@ def varReplace(raw, vars):
return ''.join(done) return ''.join(done)
def template(text, vars): def template(text, vars, setup_cache):
''' run a text buffer through the templating engine ''' ''' run a text buffer through the templating engine '''
vars = vars.copy()
text = varReplace(str(text), vars) text = varReplace(str(text), vars)
vars['hostvars'] = setup_cache
template = jinja2.Template(text) template = jinja2.Template(text)
return template.render(vars) return template.render(vars)
def double_template(text, vars): def double_template(text, vars, setup_cache):
return template(template(text, vars), vars) return template(template(text, vars, setup_cache), vars, setup_cache)
def template_from_file(path, vars): def template_from_file(path, vars):
''' run a file through the templating engine ''' ''' run a file through the templating engine '''