Store the list of hosts to run on in runner object

This reduces the number of times inventory.list_hosts is called, which
can be costly. When coming from a playbook that data is already known.
This commit is contained in:
Jesse Keating 2014-01-06 20:13:39 -08:00
parent e8ad36c8d4
commit 6013f0738e
2 changed files with 11 additions and 3 deletions

View file

@ -316,7 +316,8 @@ class PlayBook(object):
check=self.check, diff=self.diff, environment=task.environment, complex_args=task.args,
accelerate=task.play.accelerate, accelerate_port=task.play.accelerate_port,
accelerate_ipv6=task.play.accelerate_ipv6,
error_on_undefined_vars=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR
error_on_undefined_vars=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR,
run_hosts=hosts
)
if task.async_seconds == 0:

View file

@ -141,6 +141,7 @@ class Runner(object):
accelerate=False, # use accelerated connection
accelerate_ipv6=False, # accelerated connection w/ IPv6
accelerate_port=None, # port to use with accelerated connection
run_hosts=None, # an optional list of pre-calculated hosts to run on
):
# used to lock multiprocess inputs and outputs at various levels
@ -205,6 +206,10 @@ class Runner(object):
# don't override subset when passed from playbook
self.inventory.subset(subset)
# If we get a pre-built list of hosts to run on, from say a playbook, use them.
# Also where we will store the hosts to run on once discovered
self.run_hosts = run_hosts
if self.transport == 'local':
self.remote_user = pwd.getpwuid(os.geteuid())[0]
@ -1002,7 +1007,7 @@ class Runner(object):
results2["dark"][host] = result.result
# hosts which were contacted but never got a chance to return
for host in self.inventory.list_hosts(self.pattern):
for host in self.run_hosts:
if not (host in results2['dark'] or host in results2['contacted']):
results2["dark"][host] = {}
return results2
@ -1013,7 +1018,9 @@ class Runner(object):
''' xfer & run module on all matched hosts '''
# find hosts that match the pattern
hosts = self.inventory.list_hosts(self.pattern)
if not self.run_hosts:
self.run_hosts = self.inventory.list_hosts(self.pattern)
hosts = self.run_hosts
if len(hosts) == 0:
self.callbacks.on_no_hosts()
return dict(contacted={}, dark={})