From c1fd1348b16973572444fa3920a6f2b042e5b559 Mon Sep 17 00:00:00 2001
From: cocoy <imcocoy@gmail.com>
Date: Thu, 10 May 2012 17:45:30 +0800
Subject: [PATCH] Fix for bug #264

---
 lib/ansible/playbook.py | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/lib/ansible/playbook.py b/lib/ansible/playbook.py
index 68c8ea5cad1..b2c1ec9f626 100644
--- a/lib/ansible/playbook.py
+++ b/lib/ansible/playbook.py
@@ -399,19 +399,23 @@ class PlayBook(object):
 
     # *****************************************************
 
-    def _do_conditional_imports(self, vars_files):
+    def _do_conditional_imports(self, vars_files, pattern=None):
         ''' handle the vars_files section, which can contain variables '''
-        
+
         # FIXME: save parsed variable results in memory to avoid excessive re-reading/parsing
         # FIXME: currently parses imports for hosts not in the pattern, that is not wrong, but it's 
         #        not super optimized yet either, because we wouldn't have hit them, ergo
         #        it will raise false errors if there is no defaults variable file without any $vars
         #        in it, which could happen on uncontacted hosts.
- 
+
         if type(vars_files) != list:
             raise errors.AnsibleError("vars_files must be a list")
-        for host in self.inventory.list_hosts():
-            cache_vars = SETUP_CACHE.get(host,{}) 
+
+        host_list = [ h for h in self.inventory.list_hosts(pattern)
+                        if not (h in self.stats.failures or h in self.stats.dark) ]
+
+        for host in host_list:
+            cache_vars = SETUP_CACHE.get(host,{})
             SETUP_CACHE[host] = cache_vars
             for filename in vars_files:
                 if type(filename) == list:
@@ -453,12 +457,13 @@ class PlayBook(object):
 
         if vars_files is not None:
             self.callbacks.on_setup_secondary()
-            self._do_conditional_imports(vars_files)
+            self._do_conditional_imports(vars_files, pattern)
         else:
             self.callbacks.on_setup_primary()
 
         host_list = [ h for h in self.inventory.list_hosts(pattern) 
                         if not (h in self.stats.failures or h in self.stats.dark) ]
+
         self.inventory.restrict_to(host_list)
 
         # push any variables down to the system