summaryrefslogtreecommitdiff
path: root/lib/ansible/__init__.py
diff options
context:
space:
mode:
authorMichael DeHaan <michael.dehaan@gmail.com>2012-02-23 23:00:37 -0500
committerMichael DeHaan <michael.dehaan@gmail.com>2012-02-23 23:00:37 -0500
commitbd37864242907f780162e7af87506a5f450579bc (patch)
tree7950ec935e339fbf60a8e2004f64d79bee448c2d /lib/ansible/__init__.py
parent11f79300385b40cc19106837a75dc412e9693857 (diff)
downloadansible-bd37864242907f780162e7af87506a5f450579bc.tar.gz
Comments and fixup on the dark/contacted code
Diffstat (limited to 'lib/ansible/__init__.py')
-rwxr-xr-xlib/ansible/__init__.py28
1 files changed, 23 insertions, 5 deletions
diff --git a/lib/ansible/__init__.py b/lib/ansible/__init__.py
index c740f88ae0..b57aee7aaa 100755
--- a/lib/ansible/__init__.py
+++ b/lib/ansible/__init__.py
@@ -38,6 +38,7 @@ DEFAULT_TIMEOUT = 60
DEFAULT_REMOTE_USER = 'root'
def _executor_hook(x):
+ ''' callback used by multiprocessing pool '''
(runner, host) = x
return runner._executor(host)
@@ -86,7 +87,11 @@ class Runner(object):
return False
def _connect(self, host):
- ''' obtains a paramiko connection to the host '''
+ '''
+ obtains a paramiko connection to the host.
+ on success, returns (True, connection)
+ on failure, returns (False, traceback str)
+ '''
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
@@ -97,8 +102,13 @@ class Runner(object):
return [ False, traceback.format_exc() ]
def _executor(self, host):
- ''' callback executed in parallel for each host '''
- # TODO: try/catch returning none
+ '''
+ callback executed in parallel for each host.
+ returns (hostname, connected_ok, extra)
+ where extra is the result of a successful connect
+ or a traceback string
+ '''
+ # TODO: try/catch around JSON handling
ok, conn = self._connect(host)
if not ok:
@@ -148,10 +158,17 @@ class Runner(object):
def run(self):
''' xfer & run module on all matched hosts '''
+
+ # find hosts that match the pattern
hosts = [ h for h in self.host_list if self._matches(h) ]
+
+ # attack pool of hosts in N forks
pool = multiprocessing.Pool(self.forks)
hosts = [ (self,x) for x in hosts ]
results = pool.map(_executor_hook, hosts)
+
+ # sort hosts by ones we successfully contacted
+ # and ones we did not
results2 = {
"contacted" : {},
"dark" : {}
@@ -159,9 +176,10 @@ class Runner(object):
for x in results:
(host, is_ok, result) = x
if not is_ok:
- results2["failed"][host] = result
+ results2["dark"][host] = result
else:
- results2["successful"][host] = result
+ results2["contacted"][host] = result
+
return results2