def run(self, command, module_name="command", timeout=10, forks=10, pattern=''): """ run command from andible ad-hoc. command : 必须是一个需要执行的命令字符串, 比如 'uname -a' """ data = {} if module_name not in ["raw", "command", "shell"]: raise CommandValueError( "module_name", "module_name must be of the 'raw, command, shell'") hoc = Runner( module_name=module_name, module_args=command, timeout=timeout, inventory=self.inventory, pattern=pattern, forks=forks, ) self.results_raw = hoc.run()
def run(self, module_name='shell', module_args='', timeout=10, forks=10, pattern='*', become=False, become_method='sudo', become_user='******', become_pass='', transport='paramiko'): """ run module from andible ad-hoc. module_name: ansible module_name module_args: ansible module args """ hoc = Runner( module_name=module_name, module_args=module_args, timeout=timeout, inventory=self.inventory, private_key_file=private_key_file, pattern=pattern, forks=forks, become=become, become_method=become_method, become_user=become_user, become_pass=become_pass, transport=transport) self.results_raw = hoc.run() logger.debug(self.results_raw) return self.results_raw
def run( self, module_name="shell", module_args="", timeout=10, forks=10, pattern="*", become=False, become_method="sudo", become_user="******", become_pass="", ): """ run module from andible ad-hoc. module_name: ansible module_name module_args: ansible module args """ hoc = Runner( module_name=module_name, module_args=module_args, timeout=timeout, inventory=self.inventory, pattern=pattern, forks=forks, become=become, become_method=become_method, become_user=become_user, become_pass=become_pass, ) self.results_raw = hoc.run() logger.debug(self.results_raw) return self.results_raw
def exec_command(request): ret = '' if request.method == 'POST': action = request.get_full_path().split('=')[1] if action == 'exec': target = request.POST.get('target') command = request.POST.get('command') tgtcheck = Host.objects.filter(name=target) argcheck = command not in DANGER_COMMAND if tgtcheck and argcheck: results = Runner(pattern=target, forks=10, module_name='command', module_args=command).run() if results.get('dark'): ret = 'Failed. Please run again.' else: ret = results.get('contacted') elif not tgtcheck: ret = 'No specific host.' elif not argcheck: ret = 'Please contact admistrator.' return render(request, 'exec_command.html', locals())
def execute(self, *args, **kwargs): """ Puts args and kwargs in a way ansible can understand. Calls ansible and interprets the result. """ assert self.is_hooked_up, "the module should be hooked up to the api" self.module_args = module_args = self.get_module_args(args, kwargs) runner_args = { 'module_name': self.module_name, 'module_args': module_args, 'pattern': 'all', 'host_list': self.api.servers } runner_args.update(self.api.runner_args) runner = Runner(**runner_args) log.info(u'running {}'.format(u'- {module_name}: {module_args}'.format( module_name=self.module_name, module_args=module_args ))) start = datetime.utcnow() results = runner.run() log.info(u'took {} to complete'.format(datetime.utcnow() - start)) return RunnerResults(self.parse_results(results))
def run(self, module_arg, module_name="shell", complex_args=None, timeout=10, forks=10, pattern='*'): """ run command from andible ad-hoc. Args: module_arg: ansible module argument complex_args: complex structure argument module_name: which module want to use, default use shell timeout: set runner api forks: see runner api pattern: set runner api """ hoc = Runner( module_name=module_name, module_args=module_arg, complex_args=complex_args, timeout=timeout, inventory=self.inventory, pattern=pattern, forks=forks, module_path=C.DEFAULT_MODULE_PATH, ) return_data = hoc.run() logger.info(return_data) return AnsibleResult(return_data)
def run(self, module_name='shell', module_args='', timeout=10, forks=10, pattern='*', become=False, become_method='sudo', become_user='******', become_pass='', transport='paramiko'): """ run module from andible ad-hoc. module_name: ansible module_name module_args: ansible module args """ hoc = Runner(module_name=module_name, module_args=module_args, timeout=timeout, inventory=self.inventory, pattern=pattern, forks=forks, become=become, become_method=become_method, become_user=become_user, become_pass=become_pass, transport=transport) self.results_raw = hoc.run() logging.debug(self.results_raw) return self.results_raw
def run(self, task, module_args, module_name="shell", timeout=10, forks=10, pattern='*', su_user=None): runner = Runner( module_name=module_name, module_args=module_args, inventory=self.inventory, pattern=pattern, forks=forks, timeout=timeout, su=su_user and True or False, su_user=su_user, ) self.result_raw['celery_task_id'] = task tmp = runner.run() for (host, value) in tmp.get('contacted', {}).iteritems(): if value.get('invocation', {}).get('module_name', '') != 'setup': if not self.result_raw.get(host): self.result_raw[host] = {} self.result_raw[host]['result'] = value for (host, value) in tmp.get('dark', {}).iteritems(): if not self.result_raw.get(host): self.result_raw[host] = {} value['outcome'] = 'dark' self.result_raw[host]['result'] = value log_redis(self.result_raw) return self.result_raw
def get_alias_info(pattern, remote_user): runner = Runner(module_name='setup', module_args='', pattern=pattern, inventory=autils.get_inventory(g.mysql_db), remote_user=remote_user) info = runner.run() results = {} if info['contacted']: if not info['contacted'][pattern].has_key('failed'): facts = info['contacted'][pattern]['ansible_facts'] # Server info results['system_vendor'] = facts['ansible_system_vendor'] results['product_name'] = facts['ansible_product_name'] results['product_serial'] = facts['ansible_product_serial'] # System info results['distribution'] = facts['ansible_distribution'] results['distribution_version'] = facts[ 'ansible_distribution_version'] results['architecture'] = facts['ansible_architecture'] results['kernel'] = facts['ansible_kernel'] # Cpu info results['processor_count'] = facts['ansible_processor_count'] results['processor_cores'] = facts['ansible_processor_cores'] results['processor_vcpus'] = facts['ansible_processor_vcpus'] results['processor_threads_per_core'] = facts[ 'ansible_processor_threads_per_core'] results['processor'] = set(facts['ansible_processor']) # Swap info results['swaptotal_mb'] = facts['ansible_swaptotal_mb'] results['swapfree_mb'] = facts['ansible_swapfree_mb'] # Mem info results['memtotal_mb'] = facts['ansible_memtotal_mb'] results['memfree_mb'] = facts['ansible_memfree_mb'] # Disk info results['devices'] = facts['ansible_devices'] # Mount info results['mounts'] = facts['ansible_mounts'] # Ip info results['default_ipv4'] = facts['ansible_default_ipv4'] results['all_ipv4_addresses'] = facts['ansible_all_ipv4_addresses'] # Software results['selinux'] = facts['ansible_selinux'] results['python_version'] = facts['ansible_python_version'] else: facts = info['contacted'][pattern] results['failed'] = facts['failed'] results['msg'] = facts['msg'] elif info['dark']: facts = info['dark'][pattern] results['failed'] = facts['failed'] results['msg'] = facts['msg'] else: results['failed'] = False results['msg'] = "host not found" return results
def run_module(module_name): subset = request.args.get('subset') r = Runner(host_list=app.config['ANSIBLE_HOSTS'], module_name=module_name, subset=subset) res = r.run() response = make_response(json.dumps(res), 200) response.headers['Content-Type'] = 'application/json' return response
def _run(self): self.start = datetime.now() self.save() # initial jobs for host in Inventory(ANSIBLE_INVENTORY).list_hosts(self.inventory): self.job_set.add(Job(host=host, cmd=self.cmd, start=datetime.now())) self.save() runner = Runner(module_name='shell', module_args=self.cmd, pattern=self.inventory, sudo=self.sudo, forks=ANSIBLE_FORKS, host_list=ANSIBLE_INVENTORY) _, poller = runner.run_async(time_limit=WORKER_TIMEOUT) now = time.time() while True: if poller.completed or time.time( ) - now > WORKER_TIMEOUT: # TIMEOUT break results = poller.poll() results = results.get('contacted') if results: for host, result in results.items(): job = self.job_set.get(host=host) job.end = result.get('end') job.rc = result.get('rc') job.stdout = result.get('stdout') job.stderr = result.get('stderr') job.save() time.sleep(1) jobs_timeout = filter(lambda job: job.rc is None, self.job_set.all()) # rc is None jobs_failed = filter(lambda job: job.rc, self.job_set.all()) # rc > 0 for job in jobs_timeout: job.rc = 1 job.stderr = 'JOB TIMEOUT' # marked as 'TIMEOUT' job.save() self.rc = (jobs_timeout or jobs_failed) and 1 or 0 self.end = datetime.now() self.use = datetime.now() self.save() self.done()
def _run(self, *module_args, **complex_args): """Execute an ansible adhoc command returning the results in a AdHocResult object.""" # Assemble module argument string if True: module_args = ' '.join(module_args) else: if module_args: complex_args.update(dict(_raw_params=' '.join(module_args))) # Assert hosts matching the provided pattern exist hosts = self.options['inventory_manager'].list_hosts() no_hosts = False if len(hosts) == 0: no_hosts = True warnings.warn( "provided hosts list is empty, only localhost is available") self.options['inventory_manager'].subset(self.options.get('subset')) hosts = self.options['inventory_manager'].list_hosts( self.options['host_pattern']) if len(hosts) == 0 and not no_hosts: raise ansible.errors.AnsibleError( "Specified hosts and/or --limit does not match any hosts") # Log the module and parameters log.debug("[%s] %s: %s" % (self.options['host_pattern'], self.options['module_name'], complex_args)) # Build module runner object kwargs = dict( inventory=self.options.get('inventory_manager'), pattern=self.options.get('host_pattern'), module_name=self.options.get('module_name'), module_args=module_args, complex_args=complex_args, transport=self.options.get('connection'), remote_user=self.options.get('user'), module_path=self.options.get('module_path'), become=self.options.get('become'), become_method=self.options.get('become_method'), become_user=self.options.get('become_user'), ) # Run the module runner = Runner(**kwargs) results = runner.run() # Log the results log.debug(results) if 'dark' in results and results['dark']: raise AnsibleConnectionFailure("Host unreachable", dark=results['dark'], contacted=results['contacted']) # Success! return AdHocResult(contacted=results['contacted'])
def run(self): runner = Runner( module_name=self.module_name, module_args=self.module_args, pattern=self.pattern, forks=10 ) self.results_raw = runner.run() data = json.dumps(self.results_raw, indent=4) print data
def execute(self, servers, arguments): runner_args = { 'module_name': self.module_name, 'module_args': arguments, 'pattern': 'all', 'host_list': ' '.join(servers) } runner = Runner(**runner_args) return runner.run()
def network_verify(): heat_config_runner = Runner(host_list='hosts', module_name='network_check', remote_user='******', become=True, module_args='') run_result = heat_config_runner.run() heat_config = {} for k, v in run_result['contacted'].items(): heat_config[k] = v['ansible_facts']
def get_local_facts(self): """ Loads the Ansible local facts in self.facts Calls the Ansible Python API v1 'setup' module """ inventory = Inventory(["localhost"]) runner = Runner(module_name='setup', module_args='', pattern='localhost', inventory=inventory, transport="local") result = runner.run() self.facts = result["contacted"]["localhost"]["ansible_facts"]
def on_ok(self, host, res): module = res['invocation']['module_name'] print "%s ok:[%s]" % (str(datetime.now()), host) if 'git' == module and host == self.inventory.get_hosts()[0].name: r = Runner(module_name='shell', module_args='find . -name "Test*java" -exec basename {} \; | sed -e "s/.java//g" | tr "\n" "," chdir=$target_dir/%s' % self.module, remote_user=self.remote_user, private_key_file=self.private_key_file, inventory=self.inventory, pattern=host) res = r.run() gen_test_lists(self.build_dir, self.inventory, res['contacted'][host]['stdout'].split(','))
def show_template(host, path, gather_facts=True, inventory_file=None, password_file=None): inventory = get_inventory(inventory_file, password_file) setup_cache = get_gathered_facts(host, inventory) if gather_facts else {} # Show the template runner = Runner( inventory=inventory, setup_cache=setup_cache, ) host_vars = runner.get_inject_vars(host) print template_from_file('.', path, host_vars)
def run_task_call_callback(module_name, module_path, inventory_path, subset, extra_vars, event_callback): callbacks_object = EmitterCallbacks(event_callback) runner = Runner( module_name = module_name, module_path = module_path, inventory = Inventory(inventory_path), module_args = extra_vars, callbacks = callbacks_object, subset = subset ) results = runner.run() callbacks_object.on_complete()
def __init__(self, *args, **kwargs): self.show_colors = kwargs.pop('show_colors', False) for k in ( 'sudo_user', 'sudo_pass', 'sudo', 'su', 'su_user', 'su_pass', ): kwargs.pop(k, None) Runner.__init__(self, *args, **kwargs)
def execute(self, servers, arguments): runner_args = { 'module_name': self.module_name, 'module_args': arguments, 'pattern': 'all', 'host_list': ' '.join( servers ) } runner = Runner(**runner_args) return runner.run()
def check_health(opts, vm_name, vm_ip): """ Test connectivity to the VM :param vm_ip: ip address to the newly created VM :raises: :py:class:`~backend.exceptions.CoprWorkerSpawnFailError`: validation fails """ # setproctitle("check VM: {}".format(vm_ip)) log = get_redis_logger(opts, "vmm.check_health.detached", "vmm") runner_options = dict( remote_user=opts.build_user or "root", host_list="{},".format(vm_ip), pattern=vm_ip, forks=1, transport=opts.ssh.transport, timeout=opts.vm_ssh_check_timeout ) connection = Runner(**runner_options) connection.module_name = "shell" connection.module_args = "echo hello" result = { "vm_ip": vm_ip, "vm_name": vm_name, "msg": "", "result": "OK", "topic": EventTopics.HEALTH_CHECK } err_msg = None try: res = connection.run() if vm_ip not in res.get("contacted", {}): err_msg = ( "VM is not responding to the testing playbook." "Runner options: {}".format(runner_options) + "Ansible raw response:\n{}".format(res)) except Exception as error: err_msg = "Failed to check VM ({})due to ansible error: {}".format(vm_ip, error) log.exception(err_msg) try: if err_msg: result["result"] = "failed" result["msg"] = err_msg rc = get_redis_connection(opts) rc.publish(PUBSUB_MB, json.dumps(result)) except Exception as err: log.exception("Failed to publish msg health check result: {} with error: {}" .format(result, err))
def show_template(host, path, gather_facts=True, inventory_file=None, password_file=None, user=None): inventory = get_inventory(inventory_file, password_file) setup_cache = get_gathered_facts( host, inventory, user) if gather_facts else {} # Show the template runner = Runner( inventory=inventory, setup_cache=setup_cache, ) host_vars = runner.get_inject_vars(host) print template_from_file('.', path, host_vars)
def _run(self, *module_args, **complex_args): """Execute an ansible adhoc command returning the results in a AdHocResult object.""" # Assemble module argument string if True: module_args = ' '.join(module_args) else: if module_args: complex_args.update(dict(_raw_params=' '.join(module_args))) # Assert hosts matching the provided pattern exist hosts = self.options['inventory_manager'].list_hosts() no_hosts = False if len(hosts) == 0: no_hosts = True warnings.warn("provided hosts list is empty, only localhost is available") self.options['inventory_manager'].subset(self.options.get('subset')) hosts = self.options['inventory_manager'].list_hosts(self.options['host_pattern']) if len(hosts) == 0 and not no_hosts: raise ansible.errors.AnsibleError("Specified hosts and/or --limit does not match any hosts") # Log the module and parameters log.debug("[%s] %s: %s" % (self.options['host_pattern'], self.options['module_name'], complex_args)) # Build module runner object kwargs = dict( inventory=self.options.get('inventory_manager'), pattern=self.options.get('host_pattern'), module_name=self.options.get('module_name'), module_args=module_args, complex_args=complex_args, transport=self.options.get('connection'), remote_user=self.options.get('user'), module_path=self.options.get('module_path'), become=self.options.get('become'), become_method=self.options.get('become_method'), become_user=self.options.get('become_user'), ) # Run the module runner = Runner(**kwargs) results = runner.run() # Log the results log.debug(results) if 'dark' in results and results['dark']: raise AnsibleConnectionFailure("Host unreachable", dark=results['dark'], contacted=results['contacted']) # Success! return AdHocResult(contacted=results['contacted'])
def check_health(opts, vm_name, vm_ip): """ Test connectivity to the VM :param vm_ip: ip address to the newly created VM :raises: :py:class:`~backend.exceptions.CoprWorkerSpawnFailError`: validation fails """ # setproctitle("check VM: {}".format(vm_ip)) log = get_redis_logger(opts, "vmm.check_health.detached", "vmm") runner_options = dict(remote_user=opts.build_user or "root", host_list="{},".format(vm_ip), pattern=vm_ip, forks=1, transport=opts.ssh.transport, timeout=opts.vm_ssh_check_timeout) connection = Runner(**runner_options) connection.module_name = "shell" connection.module_args = "echo hello" result = { "vm_ip": vm_ip, "vm_name": vm_name, "msg": "", "result": "OK", "topic": EventTopics.HEALTH_CHECK } err_msg = None try: res = connection.run() if vm_ip not in res.get("contacted", {}): err_msg = ("VM is not responding to the testing playbook." "Runner options: {}".format(runner_options) + "Ansible raw response:\n{}".format(res)) except Exception as error: err_msg = "Failed to check VM ({})due to ansible error: {}".format( vm_ip, error) log.exception(err_msg) try: if err_msg: result["result"] = "failed" result["msg"] = err_msg rc = get_redis_connection(opts) rc.publish(PUBSUB_MB, json.dumps(result)) except Exception as err: log.exception( "Failed to publish msg health check result: {} with error: {}". format(result, err))
def _run(self): self.start = datetime.now() self.save() # initial jobs for host in Inventory(ANSIBLE_INVENTORY).list_hosts(self.inventory): self.job_set.add(Job(host = host, cmd = self.cmd, start = datetime.now())) self.save() runner = Runner(module_name = 'shell', module_args = self.cmd, pattern = self.inventory, sudo = self.sudo, forks = ANSIBLE_FORKS, host_list = ANSIBLE_INVENTORY) _, poller = runner.run_async(time_limit = WORKER_TIMEOUT) now = time.time() while True: if poller.completed or time.time() - now > WORKER_TIMEOUT: # TIMEOUT break results = poller.poll() results = results.get('contacted') if results: for host, result in results.items(): job = self.job_set.get(host = host) job.end = result.get('end') job.rc = result.get('rc') job.stdout = result.get('stdout') job.stderr = result.get('stderr') job.save() time.sleep(1) jobs_timeout = filter(lambda job: job.rc is None, self.job_set.all()) # rc is None jobs_failed = filter(lambda job: job.rc, self.job_set.all()) # rc > 0 for job in jobs_timeout: job.rc = 1 job.stderr = 'JOB TIMEOUT' # marked as 'TIMEOUT' job.save() self.rc = (jobs_timeout or jobs_failed) and 1 or 0 self.end = datetime.now() self.save() self.done()
def run(self): runner = Runner( host_list = self.host_list, module_name = self.module_name, module_args = self.module_args, pattern = self.pattern, forks = self.forks ) datastructure = runner.run() if len(datastructure['dark']) == 0 and len(datastructure['contacted']) == 0: results = {"error": "No hosts found"} else: results = datastructure return results
def __init__(self, name, host_list, module_name, module_args, pattern='*', inject=None): super(AnsibleTask, self).__init__(name, inject=inject, provides=name) self.name = name self.host_list = host_list self.logger = logging.getLogger("AnsibleTask") self.runner = Runner(host_list=host_list, pattern=pattern, module_name=module_name, module_args=module_args)
def run(self, module_name='shell', module_args='', timeout=10, forks=10, pattern='*', become=False, become_method='sudo', become_user='******', become_pass=''): hoc = Runner(module_name=module_name, module_args=module_args, timeout=timeout, inventory=self.inventory, pattern=pattern, forks=forks, become=become, become_user=become_user, become_method=become_method, become_pass=become_pass ) self.result_raw = hoc.run() loginfo.info(self.result_raw) return self.result_raw
def run(self, module_name='shell', module_args='', timeout=10, forks=10, pattern='*', become=False, become_method='sudo', become_user='******', become_pass=''): """ run module from andible ad-hoc. module_name: ansible module_name module_args: ansible module args """ if module_name == 'user_check': hoc = Runner(module_name='command', module_args='cat /etc/group', timeout=timeout, inventory=self.inventory, pattern=pattern, forks=forks, become=become, become_method=become_method, become_user=become_user, become_pass=become_pass ) temp_result= hoc.run() output = temp_result['contacted'] temp_list = list() for node in output.keys(): lines = output[node]['stdout'].split('\n') groups = [line.split(':')[0] for line in lines] if module_name == 'user_check': for g in groups: temp_name = 'name='+g if module_args.split(' ')[0] == temp_name: temp_list.append(node) break if temp_list: return {'flag': False, 'nodes': temp_list} hoc = Runner(module_name=module_name, module_args=module_args, timeout=timeout, inventory=self.inventory, pattern=pattern, forks=forks, become=become, become_method=become_method, become_user=become_user, become_pass=become_pass ) self.results_raw = hoc.run() logger.debug(self.results_raw) return self.results_raw
def connect_host(hosts,user,port,passwd,sudopasswd): #安装python-simplejson g_inv = ansible.inventory.Inventory(hosts) results = Runner( inventory=g_inv, module_name='raw', module_args="yum -y install python-simplejson", pattern="all", sudo=True, remote_user=user, remote_port=port, remote_pass=passwd, sudo_pass=sudopasswd ).run() #由于,上个模式肯定运行失败,所以再次运行setup任务 results = Runner( inventory=g_inv, module_name='setup', module_args=" ", pattern="all", sudo=True, remote_user=user, remote_port=port, remote_pass=passwd, sudo_pass=sudopasswd ).run() ret={} if results is None: for host in hosts: ret[host]=(False,"No hosts found",{}) return ret for host in hosts: ret[host]=(False,"unknow error",{}) for (host, result) in results['contacted'].items(): if not 'failed' in result: ret[host]=(True,"",get_info_from_result(result['ansible_facts']) ) else: ret[host]=(False,result['msg'],{}) print result for (host, result) in results['dark'].items(): ret[host]=(False,result['msg'],{}) return ret
def deploy(self, deploy_script=None): if deploy_script is None: if not os.path.isfile(self.git_dir + '/conf/deploy.sh'): logging.error("No deploy script") return False deploy_script = self.git_dir + '/conf/deploy.sh' arg = "{} {} {} {}".format( deploy_script, self.config['package_url'], self.deploy_dir, self.config['target_dir'], self.config['name'], ) for s in self.config['servers']: results = Runner( pattern=s, module_name='script', module_args=arg, ).run() print results for (h, r) in results['contacted'].items(): if r['rc'] != 0: logging.error("{} deploy failed.".format(s)) return False return True
class AnsibleTask(Task): def __init__(self, name, host_list, module_name, module_args, pattern='*', inject=None): super(AnsibleTask, self).__init__(name, inject=inject, provides=name) self.name = name self.host_list = host_list self.logger = logging.getLogger("AnsibleTask") self.runner = Runner(host_list=host_list, pattern=pattern, module_name=module_name, module_args=module_args) def execute(self): self.logger.info('Executing Task ' + self.name + ':') self.logger.debug('\tHosts: ' + ','.join(self.host_list)) self.logger.debug('\tModule_name: ' + self.runner.module_name) self.logger.debug('\tModule_args: ' + self.runner.module_args) self.logger.debug('\tPattern: ' + self.runner.pattern) result = self.runner.run() self.logger.debug('Result of Task ' + self.name + ':') self.logger.debug(json.dumps(result, indent=4, sort_keys=True)) return result
def configure_db(self): """ Add the necessary tables to the default PostgreSQL server running on the host and prepare the necessary roles and databases. """ # Update psql settings pg_conf = paths.P_PG_CONF lif = ["listen_addresses = '*'", "shared_buffers = 256MB", "wal_buffers = 8MB", "checkpoint_segments = 16", "checkpoint_completion_target = 0.9"] for l in lif: log.debug("Updating PostgreSQL conf file {0} setting: {1}".format(pg_conf, l)) regexp = ' '.join(l.split(' ')[:2]) try: Runner(inventory=Inventory(['localhost']), transport='local', become=True, become_user='******', module_name="lineinfile", module_args=('dest={0} backup=yes line="{1}" owner=postgres regexp="{2}"' .format(pg_conf, l, regexp)) ).run() except Exception, e: log.error("Exception updating psql conf {0}: {1}".format(l, e))
def ansible_cmd(pattern,module,args,forks): result = Runner( module_name = module, module_args = args, pattern = pattern, forks = forks).run() return result
def run(self, module_name='shell', module_args='', timeout=10, forks=2, pattern='*', become=False, become_method='sudo', become_user='******', become_pass='', transport='paramiko'): hoc = Runner(module_name=module_name, module_args=module_args, timeout=timeout, inventory=self.inventory, pattern=pattern, forks=forks, become=become, become_method=become_method, become_user=become_user, become_pass=become_pass, transport=transport ) self.results_raw = hoc.run() return self.results_raw
def test_class_override(self): override_hosts = ["thor", "odin"] hosts, groups = Runner.parse_hosts(self.inventory_file, override_hosts) assert hosts == override_hosts assert groups == {"ungrouped": override_hosts}
def ansible(**kwargs): results = Runner(**kwargs).run() rjson = json.dumps(results, indent=2) flash(rjson, 'ansible_results') # import pdb; pdb.set_trace() user = User.query.get(current_user.id) db.session.add(Log(user=user, log_info=rjson)) return results
def __init__(self, name, host_list, module_name, module_args, pattern='*', inject=None): super(AnsibleTask, self).__init__( name, inject=inject, provides=name) #provides send out the results to engine.storage self.name = name self.host_list = host_list self.logger = logging.getLogger("RecoveryHandler:Base") self.runner = Runner(host_list=host_list, pattern=pattern, module_name=module_name, module_args=module_args)
def run(self): """Run ansible command and process results Run ansible command, returning output processed with self.process_results. """ results = Runner.run(self) return self.process_results(results, show_colors=self.show_colors)
def _perform_task(self, task, logfile=None): task.fix_arguments() if isinstance(task, (NullTask, StructuralTask)): task.perform() else: cmapper = get_mapper(_agent_domain) processor = cmapper[task.__class__]() # task.get_task_role().fix_arguments() # task_host = task.get_task_host() task_host = self._get_run_host(task) if task_host is not None: msg = "Task {} being run on {}".format(task.name, task_host) if logfile: logfile.write("{}\n".format(msg)) hlist = [task_host] else: raise ExecutionException("We need a default execution host") kwargs = processor.make_args(task, hlist) kwargs["forks"] = 1 kwargs["timeout"] = 20 if logfile: logfile.write(">>>Params:\n{}\n".format(json.dumps(kwargs))) # msg = json.dumps(kwargs) # runner_file = find_file(json_runner.__file__) # args = [sys.executable, # runner_file] # proc = subprocess32.Popen(args, stdin=subprocess32.PIPE, # stdout=subprocess32.PIPE, # stderr=subprocess32.PIPE) # proc.stdin.write(msg) # proc.stdin.flush() # proc.stdin.close() # reply = proc.stdout.read() # proc.wait() # if logfile: # logfile.write(">>>Result:\n{}\n".format(reply)) # result = json.loads(reply) runner = Runner(**kwargs) result = runner.run() if logfile: logfile.write(">>>Result:\n{}\n".format(json.dumps(result))) processor.result_check(task, result, logfile=logfile) return
def _create_ans_conn(self, username=None): ans_conn = Runner(remote_user=username or self.opts.build_user, host_list=self.hostname + ",", pattern=self.hostname, forks=1, transport=self.opts.ssh.transport, timeout=self.timeout) return ans_conn
def run_module(self, inventory, source, sshpass, sudopass): runner_cb = BattleschoolRunnerCallbacks() runner_cb.options = self.options runner_cb.options.module_name = self.module_name() module_args = self.module_args(source) #TODO: get workstation from options runner = Runner( pattern='workstation', module_name=self.module_name(), module_path=self.options.module_path, module_args=module_args, inventory=inventory, callbacks=runner_cb, timeout=self.options.timeout, transport=self.options.connection, #sudo=self.options.sudo, sudo=False, sudo_user=self.options.sudo_user, sudo_pass=sudopass, check=self.options.check, diff=self.options.diff, private_key_file=self.options.private_key_file, remote_user=self.options.remote_user, remote_pass=sshpass, forks=self.options.forks) try: results = runner.run() for result in results['contacted'].values(): if 'failed' in result or result.get('rc', 0) != 0: display("ERROR: failed source type (%s) '%s': %s" % (self.type(), module_args, result['msg']), stderr=True, color='red') sys.exit(2) if results['dark']: display("ERROR: failed source type (%s) '%s': DARK" % (self.type(), module_args), stderr=True, color='red') sys.exit(2) except errors.AnsibleError, e: # Generic handler for ansible specific errors display("ERROR: %s" % str(e), stderr=True, color='red') sys.exit(1)
def run_module(self, inventory, source, sshpass, sudopass): runner_cb = BattleschoolRunnerCallbacks() runner_cb.options = self.options runner_cb.options.module_name = self.module_name() module_args = self.module_args(source) #TODO: get workstation from options runner = Runner( pattern='workstation', module_name=self.module_name(), module_path=self.options.module_path, module_args=module_args, inventory=inventory, callbacks=runner_cb, timeout=self.options.timeout, transport=self.options.connection, #sudo=self.options.sudo, become=False, become_method="sudo", become_user=self.options.sudo_user, become_pass=sudopass, check=self.options.check, diff=self.options.diff, private_key_file=self.options.private_key_file, remote_user=self.options.remote_user, remote_pass=sshpass, forks=self.options.forks ) try: results = runner.run() for result in results['contacted'].values(): if 'failed' in result or result.get('rc', 0) != 0: display("ERROR: failed source type (%s) '%s': %s" % (self.type(), module_args, result['msg']), stderr=True, color='red') sys.exit(2) if results['dark']: display("ERROR: failed source type (%s) '%s': DARK" % (self.type(), module_args), stderr=True, color='red') sys.exit(2) except errors.AnsibleError, e: # Generic handler for ansible specific errors display("ERROR: %s" % str(e), stderr=True, color='red') sys.exit(1)
def add_sshkey(alias): form = AddSSHKeyForm() if form.validate_on_submit(): remote_user = form.remote_user.data remote_pass = form.remote_pass.data runner = Runner(module_name='authorized_key', module_args={ 'user': remote_user, 'key': open(app.config['ANSIBLE_KEY']).read() }, pattern=alias, inventory=autils.get_inventory(g.mysql_db), remote_user=remote_user, remote_pass=remote_pass) info = runner.run() return redirect(url_for('.index')) host = g.mysql_db.get('SELECT * FROM hosts WHERE alias=%s', alias) return render_template('add_sshkey.html', host=host, form=form)
def run(self, module_arg, module_name="shell", timeout=10, forks=10, pattern='*'): """ run command from andible ad-hoc. Args: module_arg: ansible module argument module_name: which module want to use, default use shell timeout: set runner api forks: see runner api pattern: set runner api """ hoc = Runner(module_name=module_name, module_args=module_arg, timeout=timeout, inventory=self.inventory, pattern=pattern, forks=forks, ) self.results_raw = hoc.run() return AnsibleResult(self.results_raw)
def test_class_method(self): hosts, groups = Runner.parse_hosts(self.inventory_file) expected_hosts = ["jupiter", "saturn", "zeus", "hera", "poseidon", "thor", "odin", "loki"] assert hosts == expected_hosts expected_groups = { "ungrouped": ["jupiter", "saturn"], "greek": ["zeus", "hera", "poseidon"], "norse": ["thor", "odin", "loki"], } assert groups == expected_groups
def execute(self, ansible_mod, ansible_patt, ansible_args=None): ret = None if ansible_args: runner_obj = Runner( module_name=ansible_mod, module_args=ansible_args, host_list=self.ansible_host_list, pattern=ansible_patt, become=True, become_user='******', ) else: runner_obj = Runner( module_name=ansible_mod, host_list=self.ansible_host_list, pattern=ansible_patt, become=True, become_user='******', ) ret = runner_obj.run()
def heat_config_check(): heat_config_runner = Runner(host_list='hosts', module_name='heat_config', remote_user='******', become=True, module_args='') run_result = heat_config_runner.run() heat_config = {} for k, v in run_result['contacted'].items(): heat_config[k] = v['ansible_facts'] discoverd_data = get_discoverd_data() print '\n\nIronic discoverd data:' for hwid, data in discoverd_data.items(): print hwid, data # NOTE(shadower): the entire heat_config is HUGE print '\n\nos-net-config input:' for ip, config in heat_config.items(): print ip pprint.pprint(config['complete'].get('os_net_config', {})) pprint.pprint(heat_config)
def __init__(self, *args, **kwargs): self.show_colors = kwargs.pop('show_colors', False) Runner.__init__(self, *args, **kwargs)
def run_from_json(json_msg): kwargs = json.loads(json_msg) runner = Runner(**kwargs) result = runner.run() return json.dumps(result)
def playbook_on_stats(self, stats): if self.disable: return history = {} history['stats'] = {} hosts = sorted(stats.processed.keys()) for h in hosts: history['stats'][h] = stats.summarize(h) os.close(self.pipeout) pipein = os.fdopen(self.pipein) failed_at = pipein.readlines() self._fill_failed_at(failed_at) history['failed_at'] = self.failed_at history['roles'] = list(set([role[:role.find('|')].strip() for role in self.executed_task if role.find('|') != -1])) history['date'] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) history['ansible_version'] = ansible.utils.version("ansible") history['aeriscloud_version'] = ac_version ac_repo_path = os.path.join(aeriscloud_path, '.git') history['aeriscloud_commit'] = _git_repo_info(ac_repo_path)['commit'] if os.path.isfile('.aeriscloud.yml'): import yaml with open('.aeriscloud.yml') as fd: history['services'] = yaml.load(fd).get('services') # Re-create a parser and extract all the parameters we need # to run ansible parser = ansible.utils.base_parser( constants=ansible.constants, runas_opts=True, subset_opts=True, async_opts=True, output_opts=True, connect_opts=True, check_opts=True, diff_opts=False, usage='%prog <host-pattern> [options]' ) filtered_arguments = [] for arg in sys.argv: for opt in [ 'limit', 'inventory-file', 'private-key', 'user', 'connection' ]: if arg.startswith('--' + opt + '='): filtered_arguments.append(arg) if '-i' in sys.argv: inventory_index = sys.argv.index('-i') if inventory_index > -1: filtered_arguments.append(sys.argv[inventory_index]) filtered_arguments.append(sys.argv[inventory_index + 1]) (options, args) = parser.parse_args(filtered_arguments) inventory_manager = ansible.inventory.Inventory(options.inventory) if options.subset: inventory_manager.subset(options.subset) # Create the command to append the history data to the file command = "echo '" + json.dumps(history) + "' >> ~/.provision" # Disable the callback plugins to have no output ansible.callbacks.callback_plugins = [] runner = Runner( inventory=inventory_manager, subset=options.subset, module_name='raw', module_args=command, private_key_file=options.private_key_file, remote_user=options.remote_user, transport=options.connection, callbacks=None ) runner.run()