Browse code

Make returning results after loading vars optional

Reinstates some functionality removed by commit 0ba9a6a but
makes the expensive operation optional (and default to off)

(cherry picked from commit 7dc09adaf4d07444c09229e58bddac762f2e818e)

Alex Mirski-Fitton authored on 2016/07/21 18:26:57
Showing 1 changed files
... ...
@@ -718,13 +718,13 @@ class Inventory(object):
718 718
             self._vars_per_host = {}
719 719
             self._vars_per_group = {}
720 720
 
721
-    def get_host_vars(self, host, new_pb_basedir=False):
721
+    def get_host_vars(self, host, new_pb_basedir=False, return_results=False):
722 722
         """ Read host_vars/ files """
723
-        return self._get_hostgroup_vars(host=host, group=None, new_pb_basedir=new_pb_basedir)
723
+        return self._get_hostgroup_vars(host=host, group=None, new_pb_basedir=new_pb_basedir, return_results=return_results)
724 724
 
725
-    def get_group_vars(self, group, new_pb_basedir=False):
725
+    def get_group_vars(self, group, new_pb_basedir=False, return_results=False):
726 726
         """ Read group_vars/ files """
727
-        return self._get_hostgroup_vars(host=None, group=group, new_pb_basedir=new_pb_basedir)
727
+        return self._get_hostgroup_vars(host=None, group=group, new_pb_basedir=new_pb_basedir, return_results=return_results)
728 728
 
729 729
     def _find_group_vars_files(self, basedir):
730 730
         """ Find group_vars/ files """
... ...
@@ -746,7 +746,7 @@ class Inventory(object):
746 746
             found_vars = set(os.listdir(unicode(path)))
747 747
         return found_vars
748 748
 
749
-    def _get_hostgroup_vars(self, host=None, group=None, new_pb_basedir=False):
749
+    def _get_hostgroup_vars(self, host=None, group=None, new_pb_basedir=False, return_results=False):
750 750
         """
751 751
         Loads variables from group_vars/<groupname> and host_vars/<hostname> in directories parallel
752 752
         to the inventory base directory or in the same directory as the playbook.  Variables in the playbook
... ...
@@ -785,11 +785,15 @@ class Inventory(object):
785 785
             if host is None and any(map(lambda ext: group.name + ext in self._group_vars_files, C.YAML_FILENAME_EXTENSIONS)):
786 786
                 # load vars in dir/group_vars/name_of_group
787 787
                 base_path = to_unicode(os.path.abspath(os.path.join(to_bytes(basedir), b"group_vars/" + to_bytes(group.name))), errors='strict')
788
-                self._variable_manager.add_group_vars_file(base_path, self._loader)
788
+                host_results = self._variable_manager.add_group_vars_file(base_path, self._loader)
789
+                if return_results:
790
+                    results = combine_vars(results, host_results)
789 791
             elif group is None and any(map(lambda ext: host.name + ext in self._host_vars_files, C.YAML_FILENAME_EXTENSIONS)):
790 792
                 # same for hostvars in dir/host_vars/name_of_host
791 793
                 base_path = to_unicode(os.path.abspath(os.path.join(to_bytes(basedir), b"host_vars/" + to_bytes(host.name))), errors='strict')
792
-                self._variable_manager.add_host_vars_file(base_path, self._loader)
794
+                group_results = self._variable_manager.add_host_vars_file(base_path, self._loader)
795
+                if return_results:
796
+                    results = combine_vars(results, group_results)
793 797
 
794 798
         # all done, results is a dictionary of variables for this particular host.
795 799
         return results