Allow modules to return facts.

If the module result contains "ansible_facts", that will be added to the setup
cache.
This commit is contained in:
Jeroen Hoekx 2012-04-23 20:06:14 +02:00
commit 2dc9a563ef
6 changed files with 30 additions and 13 deletions

View file

@ -397,6 +397,12 @@ class PlayBook(object):
module_args, module_vars, remote_user, async_seconds,
async_poll_interval, only_if, sudo, transport, port)
# add facts to the global setup cache
for host, result in results['contacted'].iteritems():
if "ansible_facts" in result:
for k,v in result['ansible_facts'].iteritems():
SETUP_CACHE[host][k]=v
self.stats.compute(results)
# if no hosts are matched, carry on, unlike /bin/ansible
@ -528,7 +534,8 @@ class PlayBook(object):
if vars_files is None:
# first pass only or we'll erase good work
for (host, result) in setup_ok.iteritems():
SETUP_CACHE[host] = result
if 'ansible_facts' in result:
SETUP_CACHE[host] = result['ansible_facts']
# *****************************************************

View file

@ -301,9 +301,9 @@ class Runner(object):
''' allows discovered variables to be used in templates and action statements '''
host = conn.host
try:
var_result = utils.parse_json(result)
except:
if 'ansible_facts' in result:
var_result = result['ansible_facts']
else:
var_result = {}
# note: do not allow variables from playbook to be stomped on
@ -328,10 +328,12 @@ class Runner(object):
module = self._transfer_module(conn, tmp, module_name)
(result, err, executed) = self._execute_module(conn, tmp, module, self.module_args)
if module_name == 'setup':
self._add_result_to_setup_cache(conn, result)
(host, ok, data, err) = self._return_from_module(conn, host, result, err, executed)
return self._return_from_module(conn, host, result, err, executed)
if ok:
self._add_result_to_setup_cache(conn, data)
return (host, ok, data, err)
# *****************************************************