def run_playbook(books): results_callback = callback_loader.get('json') playbooks = [books] variable_manager.extra_vars = { "ansible_ssh_user": "******", "ansible_ssh_pass": "******" } callback = ResultsCollector() pd = PlaybookExecutor( playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None, ) #pd._tqm._stdout_callback = results_callback pd._tqm._stdout_callback = callback try: result = pd.run() return callback except Exception as e: # ('run_playbook:%s'%e) print "error" print e
def run_playbook(books): inventory = set_inventory(hostname) results_callback = callback_loader.get('json') playbooks = [books] variable_manager.extra_vars = { 'ansible_ssh_user': '******', 'ansible_ssh_pass': '******' } callback = ResultsCollector() pd = PlaybookExecutor( playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=None, ) pd._tqm._stdout_callback = callback try: result = pd.run() return callback except Exception as e: print e
def load_callbacks(self): ''' Loads all available callbacks, with the exception of those which utilize the CALLBACK_TYPE option. When CALLBACK_TYPE is set to 'stdout', only one such callback plugin will be loaded. ''' if self._callbacks_loaded: return stdout_callback_loaded = False if self._stdout_callback is None: self._stdout_callback = C.DEFAULT_STDOUT_CALLBACK if isinstance(self._stdout_callback, CallbackBase): stdout_callback_loaded = True elif isinstance(self._stdout_callback, string_types): if self._stdout_callback not in callback_loader: raise AnsibleError( "Invalid callback for stdout specified: %s" % self._stdout_callback) else: self._stdout_callback = callback_loader.get( self._stdout_callback) stdout_callback_loaded = True else: raise AnsibleError( "callback must be an instance of CallbackBase or the name of a callback plugin" ) for callback_plugin in callback_loader.all(class_only=True): if hasattr(callback_plugin, 'CALLBACK_VERSION' ) and callback_plugin.CALLBACK_VERSION >= 2.0: # we only allow one callback of type 'stdout' to be loaded, so check # the name of the current plugin and type to see if we need to skip # loading this callback plugin callback_type = getattr(callback_plugin, 'CALLBACK_TYPE', None) callback_needs_whitelist = getattr(callback_plugin, 'CALLBACK_NEEDS_WHITELIST', False) (callback_name, _) = os.path.splitext( os.path.basename(callback_plugin._original_path)) if callback_type == 'stdout': if callback_name != self._stdout_callback or stdout_callback_loaded: continue stdout_callback_loaded = True elif callback_name == 'tree' and self._run_tree: pass elif not self._run_additional_callbacks or ( callback_needs_whitelist and (C.DEFAULT_CALLBACK_WHITELIST is None or callback_name not in C.DEFAULT_CALLBACK_WHITELIST)): continue self._callback_plugins.append(callback_plugin()) self._callbacks_loaded = True
def __init__(self, inventory, callback, variable_manager, loader, options): self._inventory = inventory self._variable_manager = variable_manager self._loader = loader self._options = options # a special flag to help us exit cleanly self._terminated = False # create and start the multiprocessing manager #self._manager = AnsibleManager() #self._manager.start() # this dictionary is used to keep track of notified handlers self._notified_handlers = dict() # dictionaries to keep track of failed/unreachable hosts self._failed_hosts = dict() self._unreachable_hosts = dict() self._final_q = multiprocessing.Queue() # FIXME: hard-coded the default callback plugin here, which # should be configurable. self._callback = callback_loader.get(callback) # create the pool of worker threads, based on the number of forks specified try: fileno = sys.stdin.fileno() except ValueError: fileno = None self._workers = [] for i in range(self._options.forks): # duplicate stdin, if possible new_stdin = None if fileno is not None: try: new_stdin = os.fdopen(os.dup(fileno)) except OSError, e: # couldn't dupe stdin, most likely because it's # not a valid file descriptor, so we just rely on # using the one that was passed in pass main_q = multiprocessing.Queue() rslt_q = multiprocessing.Queue() prc = WorkerProcess(self, main_q, rslt_q, loader, new_stdin) prc.start() self._workers.append((prc, main_q, rslt_q))
def __init__(self, inventory, callback, variable_manager, loader, options): self._inventory = inventory self._variable_manager = variable_manager self._loader = loader self._options = options # a special flag to help us exit cleanly self._terminated = False # this dictionary is used to keep track of notified handlers self._notified_handlers = dict() # dictionaries to keep track of failed/unreachable hosts self._failed_hosts = dict() self._unreachable_hosts = dict() self._final_q = multiprocessing.Queue() # FIXME: hard-coded the default callback plugin here, which # should be configurable. self._callback = callback_loader.get(callback) # create the pool of worker threads, based on the number of forks specified try: fileno = sys.stdin.fileno() except ValueError: fileno = None self._workers = [] for i in range(self._options.forks): # duplicate stdin, if possible new_stdin = None if fileno is not None: try: new_stdin = os.fdopen(os.dup(fileno)) except OSError, e: # couldn't dupe stdin, most likely because it's # not a valid file descriptor, so we just rely on # using the one that was passed in pass main_q = multiprocessing.Queue() rslt_q = multiprocessing.Queue() prc = WorkerProcess(self, main_q, rslt_q, loader, new_stdin) prc.start() self._workers.append((prc, main_q, rslt_q))
def load_callbacks(self): ''' Loads all available callbacks, with the exception of those which utilize the CALLBACK_TYPE option. When CALLBACK_TYPE is set to 'stdout', only one such callback plugin will be loaded. ''' if self._callbacks_loaded: return stdout_callback_loaded = False if self._stdout_callback is None: self._stdout_callback = C.DEFAULT_STDOUT_CALLBACK if isinstance(self._stdout_callback, CallbackBase): stdout_callback_loaded = True elif isinstance(self._stdout_callback, string_types): if self._stdout_callback not in callback_loader: raise AnsibleError("Invalid callback for stdout specified: %s" % self._stdout_callback) else: self._stdout_callback = callback_loader.get(self._stdout_callback) stdout_callback_loaded = True else: raise AnsibleError("callback must be an instance of CallbackBase or the name of a callback plugin") for callback_plugin in callback_loader.all(class_only=True): if hasattr(callback_plugin, 'CALLBACK_VERSION') and callback_plugin.CALLBACK_VERSION >= 2.0: # we only allow one callback of type 'stdout' to be loaded, so check # the name of the current plugin and type to see if we need to skip # loading this callback plugin callback_type = getattr(callback_plugin, 'CALLBACK_TYPE', None) callback_needs_whitelist = getattr(callback_plugin, 'CALLBACK_NEEDS_WHITELIST', False) (callback_name, _) = os.path.splitext(os.path.basename(callback_plugin._original_path)) if callback_type == 'stdout': if callback_name != self._stdout_callback or stdout_callback_loaded: continue stdout_callback_loaded = True elif callback_name == 'tree' and self._run_tree: pass elif not self._run_additional_callbacks or (callback_needs_whitelist and ( C.DEFAULT_CALLBACK_WHITELIST is None or callback_name not in C.DEFAULT_CALLBACK_WHITELIST)): continue self._callback_plugins.append(callback_plugin()) self._callbacks_loaded = True
write('{') for key2, obj2 in sorted(six.iteritems(obj)): if not (key2.startswith('_ansible_') or key2.endswith('_lines')): printi(tio, obj2, key=key2, indent=indent+1) key = None write('}') elif isinstance(obj, six.text_type): for line in obj.splitlines(): write('%s', line.rstrip('\r\n')) elif isinstance(obj, six.binary_type): obj = obj.decode('utf-8', 'replace') for line in obj.splitlines(): write('%s', line.rstrip('\r\n')) else: write('%r', obj) DefaultModule = callback_loader.get('default', class_only=True) class CallbackModule(DefaultModule): def _dump_results(self, result, *args, **kwargs): try: tio = io.StringIO() printi(tio, result) return tio.getvalue() #.encode('ascii', 'replace') except: import traceback traceback.print_exc() raise
def dump_ansible_results(results, stdout_callback='yaml'): cb = callback_loader.get(stdout_callback) return cb._dump_results(results) if cb else results
key2.endswith('_lines')): printi(tio, obj2, key=key2, indent=indent+1) key = None write('}') elif isinstance(obj, six.text_type): for line in obj.splitlines(): write('%s', line.rstrip('\r\n')) elif isinstance(obj, six.binary_type): obj = obj.decode('utf-8', 'replace') for line in obj.splitlines(): write('%s', line.rstrip('\r\n')) else: write('%r', obj) DefaultModule = callback_loader.get('default', class_only=True) class CallbackModule(DefaultModule): def _dump_results(self, result, *args, **kwargs): try: tio = io.StringIO() if pprint: pprint.pprint(result, stream=tio) else: printi(tio, result) return tio.getvalue() #.encode('ascii', 'replace') except: import traceback traceback.print_exc() raise