From 2dc9a563efa776e599a856bb86a92e03b298e61a Mon Sep 17 00:00:00 2001 From: Jeroen Hoekx Date: Mon, 23 Apr 2012 20:06:14 +0200 Subject: [PATCH] Allow modules to return facts. If the module result contains "ansible_facts", that will be added to the setup cache. --- lib/ansible/playbook.py | 9 ++++++++- lib/ansible/runner.py | 14 ++++++++------ library/facter | 2 ++ library/ohai | 2 ++ library/setup | 10 ++++++---- test/TestRunner.py | 6 ++++-- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/ansible/playbook.py b/lib/ansible/playbook.py index ed9452ecd68..0a5d976a3dc 100755 --- a/lib/ansible/playbook.py +++ b/lib/ansible/playbook.py @@ -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'] # ***************************************************** diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index c7ef3663bfa..5929e421ece 100755 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -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) # ***************************************************** diff --git a/library/facter b/library/facter index a9cbf3006a2..7a60021a69d 100755 --- a/library/facter +++ b/library/facter @@ -22,4 +22,6 @@ # facter # ruby-json +echo '{ "ansible_facts":' /usr/bin/facter --json +echo '}' diff --git a/library/ohai b/library/ohai index 3b6f3ae144c..68c0b8acfee 100755 --- a/library/ohai +++ b/library/ohai @@ -18,4 +18,6 @@ # along with Ansible. If not, see . # +echo '{ "ansible_facts":' /usr/bin/ohai +echo '}' \ No newline at end of file diff --git a/library/setup b/library/setup index 134cd232e7f..20f3d33902d 100755 --- a/library/setup +++ b/library/setup @@ -362,9 +362,11 @@ md5sum2 = os.popen("md5sum %s" % ansible_file).read().split()[0] if md5sum != md5sum2: changed = True -setup_options['written'] = ansible_file -setup_options['changed'] = changed -setup_options['md5sum'] = md5sum2 +setup_result = {} +setup_result['written'] = ansible_file +setup_result['changed'] = changed +setup_result['md5sum'] = md5sum2 +setup_result['ansible_facts'] = setup_options -print json.dumps(setup_options) +print json.dumps(setup_result) diff --git a/test/TestRunner.py b/test/TestRunner.py index aac8c41b06b..96c88ee4b39 100644 --- a/test/TestRunner.py +++ b/test/TestRunner.py @@ -85,15 +85,17 @@ class TestRunner(unittest.TestCase): if not get_binary("facter"): raise SkipTest result = self._run('facter',[]) - assert "hostname" in result + assert "hostname" in result['ansible_facts'] # temporarily disbabled since it occasionally hangs # ohai's fault, setup module doesn't actually run this # to get ohai's "facts" anyway # #def test_ohai(self): + # if not get_binary("facter"): + # raise SkipTest # result = self._run('ohai',[]) - # assert "hostname" in result + # assert "hostname" in result['ansible_facts'] def test_copy(self): # test copy module, change trigger, etc