From 5a8dff5bce7287c37386ac256ef6d12a287007c9 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sat, 20 Apr 2013 09:59:40 -0400 Subject: [PATCH] Instantiate inventory plugins only once for efficiency reasons. --- lib/ansible/inventory/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index f29d61a10e..d5dd3527b9 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -37,7 +37,8 @@ class Inventory(object): """ __slots__ = [ 'host_list', 'groups', '_restriction', '_also_restriction', '_subset', - 'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache', '_groups_list'] + 'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache', '_groups_list', + '_vars_plugins'] def __init__(self, host_list=C.DEFAULT_HOST_LIST): @@ -97,6 +98,9 @@ class Inventory(object): else: raise errors.AnsibleError("Unable to find an inventory file, specify one with -i ?") + self._vars_plugins = [ x for x in utils.plugins.vars_loader.all(self) ] + + def _match(self, str, pattern_str): if pattern_str.startswith('~'): return re.search(pattern_str[1:], str) @@ -277,7 +281,8 @@ class Inventory(object): raise errors.AnsibleError("host not found: %s" % hostname) vars = {} - for updated in map(lambda x: x.run(host), utils.plugins.vars_loader.all(self)): + vars_results = [ plugin.run(host) for plugin in self._vars_plugins ] + for updated in vars_results: if updated is not None: vars.update(updated)