def _run_task_internal(self, task): ''' run a particular module step in a playbook ''' hosts = [ h for h in self.inventory.list_hosts() if (h not in self.stats.failures) and (h not in self.stats.dark)] self.inventory.restrict_to(hosts) runner = ansible.runner.Runner( pattern=task.play.hosts, inventory=self.inventory, module_name=task.module_name, module_args=task.module_args, forks=self.forks, remote_pass=self.remote_pass, module_path=self.module_path, timeout=self.timeout, remote_user=task.play.remote_user, remote_port=task.play.remote_port, module_vars=task.module_vars, private_key_file=self.private_key_file, setup_cache=self.SETUP_CACHE, basedir=self.basedir, conditional=task.only_if, callbacks=self.runner_callbacks, verbose=self.verbose, sudo=task.play.sudo, sudo_user=task.play.sudo_user, transport=task.play.transport, sudo_pass=self.sudo_pass, is_playbook=True ) if task.async_seconds == 0: results = runner.run() else: results, poller = runner.run_async(task.async_seconds) self.stats.compute(results) if task.async_poll_interval > 0: # if not polling, playbook requested fire and forget, so don't poll results = self._async_poll(poller, task.async_seconds, task.async_poll_interval) self.inventory.lift_restriction() return results
def _run_task_internal(self, task): ''' run a particular module step in a playbook ''' hosts = self._list_available_hosts() self.inventory.restrict_to(hosts) runner = ansible.runner.Runner( pattern=task.play.hosts, inventory=self.inventory, module_name=task.module_name, module_args=task.module_args, forks=self.forks, remote_pass=self.remote_pass, module_path=self.module_path, timeout=self.timeout, remote_user=task.remote_user, remote_port=task.play.remote_port, module_vars=task.module_vars, default_vars=task.default_vars, private_key_file=self.private_key_file, setup_cache=self.SETUP_CACHE, basedir=task.play.basedir, conditional=task.when, callbacks=self.runner_callbacks, sudo=task.sudo, sudo_user=task.sudo_user, transport=task.transport, sudo_pass=task.sudo_pass, is_playbook=True, 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) if task.async_seconds == 0: results = runner.run() else: results, poller = runner.run_async(task.async_seconds) self.stats.compute(results) if task.async_poll_interval > 0: # if not polling, playbook requested fire and forget, so don't poll results = self._async_poll(poller, task.async_seconds, task.async_poll_interval) else: for (host, res) in results.get('contacted', {}).iteritems(): self.runner_callbacks.on_async_ok(host, res, poller.jid) contacted = results.get('contacted', {}) dark = results.get('dark', {}) self.inventory.lift_restriction() if len(contacted.keys()) == 0 and len(dark.keys()) == 0: return None return results
def _run_task_internal(self, task): ''' run a particular module step in a playbook ''' hosts = [ h for h in self.inventory.list_hosts() if (h not in self.stats.failures) and (h not in self.stats.dark)] self.inventory.restrict_to(hosts) runner = ansible.runner.Runner( pattern=task.play.hosts, inventory=self.inventory, module_name=task.module_name, module_args=task.module_args, forks=self.forks, remote_pass=self.remote_pass, module_path=self.module_path, timeout=self.timeout, remote_user=task.play.remote_user, remote_port=task.play.remote_port, module_vars=task.module_vars, private_key_file=self.private_key_file, setup_cache=self.SETUP_CACHE, basedir=task.play.basedir, conditional=task.only_if, callbacks=self.runner_callbacks, sudo=task.play.sudo, sudo_user=task.play.sudo_user, transport=task.transport, sudo_pass=self.sudo_pass, is_playbook=True ) if task.async_seconds == 0: results = runner.run() else: results, poller = runner.run_async(task.async_seconds) self.stats.compute(results) if task.async_poll_interval > 0: # if not polling, playbook requested fire and forget, so don't poll results = self._async_poll(poller, task.async_seconds, task.async_poll_interval) self.inventory.lift_restriction() return results
def __call__(self, nodes, *args, **kwargs): # Initialize ansible inventory manage inventory = self._ansible_inventory(nodes) runner_callbacks = AnsibleRunnerCallback(self.queue) # Assemble module argument string module_args = list() if args: module_args += list(args) module_args = ' '.join(module_args) # pop async parameters async = kwargs.pop('run_async', False) time_limit = kwargs.pop('time_limit', 60) forks = kwargs.pop('forks', C.DEFAULT_FORKS) # Build module runner object kwargs = dict( inventory=inventory, pattern='all', callbacks=runner_callbacks, module_name=self.module_name, module_args=module_args, complex_args=kwargs, forks=forks, transport=self.options.get('connection'), remote_user=self.options.get('user'), ) # Handle >= 1.9.0 options if has_ansible_become: kwargs.update(dict( become=self.options.get('become'), become_method=self.options.get('become_method'), become_user=self.options.get('become_user'),) ) else: kwargs.update(dict( sudo=self.options.get('sudo'), sudo_user=self.options.get('sudo_user'),) ) runner = ansible.runner.Runner(**kwargs) # Run the module if async: res, poll = runner.run_async(time_limit=time_limit) return _ExtendedPoller(res, poll) else: return _ExtendedPoller(runner.run(), None).poll()
def _run_task_internal(self, task): ''' run a particular module step in a playbook ''' hosts = self._list_available_hosts() self.inventory.restrict_to(hosts) runner = ansible.runner.Runner( pattern=task.play.hosts, inventory=self.inventory, module_name=task.module_name, module_args=task.module_args, forks=self.forks, remote_pass=self.remote_pass, module_path=self.module_path, timeout=self.timeout, remote_user=task.remote_user, remote_port=task.play.remote_port, module_vars=task.module_vars, default_vars=task.default_vars, private_key_file=self.private_key_file, setup_cache=self.SETUP_CACHE, basedir=task.play.basedir, conditional=task.only_if, callbacks=self.runner_callbacks, sudo=task.sudo, sudo_user=task.sudo_user, transport=task.transport, sudo_pass=task.sudo_pass, is_playbook=True, 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 ) if task.async_seconds == 0: results = runner.run() else: results, poller = runner.run_async(task.async_seconds) self.stats.compute(results) if task.async_poll_interval > 0: # if not polling, playbook requested fire and forget, so don't poll results = self._async_poll(poller, task.async_seconds, task.async_poll_interval) else: for (host, res) in results.get('contacted', {}).iteritems(): self.runner_callbacks.on_async_ok(host, res, poller.jid) contacted = results.get('contacted',{}) dark = results.get('dark', {}) self.inventory.lift_restriction() if len(contacted.keys()) == 0 and len(dark.keys()) == 0: return None return results
def _run_task_internal(self, task): ''' run a particular module step in a playbook ''' hosts = self._list_available_hosts() self.inventory.restrict_to(hosts) runner = ansible.runner.Runner( pattern=task.play.hosts, inventory=self.inventory, module_name=task.module_name, module_args=task.module_args, forks=self.forks, remote_pass=self.remote_pass, module_path=self.module_path, timeout=self.timeout, remote_user=task.play.remote_user, remote_port=task.play.remote_port, module_vars=task.module_vars, private_key_file=self.private_key_file, private_key=self.private_key, setup_cache=self.SETUP_CACHE, basedir=task.play.basedir, conditional=task.only_if, callbacks=self.runner_callbacks, sudo=task.sudo, sudo_user=task.sudo_user, transport=task.transport, sudo_pass=task.sudo_pass, is_playbook=True ) if task.async_seconds == 0: results = runner.run() else: results, poller = runner.run_async(task.async_seconds) self.stats.compute(results) if task.async_poll_interval > 0: # if not polling, playbook requested fire and forget, so don't poll results = self._async_poll(poller, task.async_seconds, task.async_poll_interval) contacted = results.get('contacted',{}) dark = results.get('dark', {}) self.inventory.lift_restriction() if len(contacted.keys()) == 0 and len(dark.keys()) == 0: return None return results
def _run_task_internal(self, task, include_failed=False): ''' run a particular module step in a playbook ''' hosts = self._trim_unavailable_hosts(self.inventory.list_hosts( task.play._play_hosts), keep_failed=include_failed) self.inventory.restrict_to(hosts) runner = ansible.runner.Runner( pattern=task.play.hosts, inventory=self.inventory, module_name=task.module_name, module_args=task.module_args, forks=self.forks, remote_pass=self.remote_pass, module_path=self.module_path, timeout=self.timeout, remote_user=task.remote_user, remote_port=task.play.remote_port, module_vars=task.module_vars, play_vars=task.play_vars, play_file_vars=task.play_file_vars, role_vars=task.role_vars, role_params=task.role_params, default_vars=task.default_vars, extra_vars=self.extra_vars, private_key_file=self.private_key_file, setup_cache=self.SETUP_CACHE, vars_cache=self.VARS_CACHE, basedir=task.play.basedir, conditional=task.when, callbacks=self.runner_callbacks, transport=task.transport, is_playbook=True, 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, vault_pass=self.vault_password, run_hosts=hosts, no_log=task.no_log, run_once=task.run_once, become=task.become, become_method=task.become_method, become_user=task.become_user, become_pass=task.become_pass, ) runner.module_vars.update({'play_hosts': hosts}) runner.module_vars.update({'ansible_version': self._ansible_version}) if task.async_seconds == 0: results = runner.run() else: results, poller = runner.run_async(task.async_seconds) self.stats.compute(results) if task.async_poll_interval > 0: # if not polling, playbook requested fire and forget, so don't poll results = self._async_poll(poller, task.async_seconds, task.async_poll_interval) else: for (host, res) in results.get('contacted', {}).iteritems(): self.runner_callbacks.on_async_ok( host, res, poller.runner.vars_cache[host]['ansible_job_id']) contacted = results.get('contacted', {}) dark = results.get('dark', {}) self.inventory.lift_restriction() if len(contacted.keys()) == 0 and len(dark.keys()) == 0: return None return results
def _run_task_internal(self, task, include_failed=False): ''' run a particular module step in a playbook ''' hosts = self._trim_unavailable_hosts(self.inventory.list_hosts(task.play._play_hosts), keep_failed=include_failed) self.inventory.restrict_to(hosts) runner = ansible.runner.Runner( pattern=task.play.hosts, inventory=self.inventory, module_name=task.module_name, module_args=task.module_args, forks=self.forks, remote_pass=self.remote_pass, module_path=self.module_path, timeout=self.timeout, remote_user=task.remote_user, remote_port=task.play.remote_port, module_vars=task.module_vars, play_vars=task.play_vars, play_file_vars=task.play_file_vars, role_vars=task.role_vars, role_params=task.role_params, default_vars=task.default_vars, extra_vars=self.extra_vars, private_key_file=self.private_key_file, setup_cache=self.SETUP_CACHE, vars_cache=self.VARS_CACHE, basedir=task.play.basedir, conditional=task.when, callbacks=self.runner_callbacks, transport=task.transport, is_playbook=True, 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, vault_pass = self.vault_password, run_hosts=hosts, no_log=task.no_log, run_once=task.run_once, become=task.become, become_method=task.become_method, become_user=task.become_user, become_pass=task.become_pass, ) runner.module_vars.update({'play_hosts': hosts}) runner.module_vars.update({'ansible_version': self._ansible_version}) if task.async_seconds == 0: results = runner.run() else: results, poller = runner.run_async(task.async_seconds) self.stats.compute(results) if task.async_poll_interval > 0: # if not polling, playbook requested fire and forget, so don't poll results = self._async_poll(poller, task.async_seconds, task.async_poll_interval) else: for (host, res) in results.get('contacted', {}).iteritems(): self.runner_callbacks.on_async_ok(host, res, poller.runner.vars_cache[host]['ansible_job_id']) contacted = results.get('contacted',{}) dark = results.get('dark', {}) self.inventory.lift_restriction() if len(contacted.keys()) == 0 and len(dark.keys()) == 0: return None return results
def post(self): data = simplejson.loads(self.request.body) badcmd = ['reboot', 'rm', 'kill', 'pkill', 'shutdown', 'half', 'mv', 'dd', 'mkfs', '>', 'wget'] type = data['type'] cmd = data['cmd'] inventory = data['host'] sign = data['sign'] isudo = data['sudo'] cmdinfo = cmd.split(" ", 1) print type, inventory, options.key hotkey = type + inventory + options.key print hotkey result = getmd5(hotkey) print result now = datetime.datetime.now() taskinfo = {} taskinfo['mode'] = type taskinfo['cmd'] = cmd taskinfo['inventory'] = inventory taskinfo['type'] = 'async ad-hoc' taskinfo['start'] = now.strftime(TIME_FORMAT) taskinfo['sudo'] = isudo uri = ConnMongoDB() client = MongoClient(uri, safe=False) db = client.ansible_log id=db.ansible_task.insert(taskinfo) mongoid={"_id":ObjectId(id)} print id if sign != result: self.write("Sign is Error") else: if cmdinfo[0] in badcmd: self.write("This is Danger Shell") else: runner = ansible.runner.Runner( module_name=type, module_args=cmd, pattern=inventory, sudo = isudo, forks=ANSIBLE_FORKS ) _, res = runner.run_async(time_limit = WORKER_TIMEOUT) now = time.time() while True: if res.completed or time.time() - now > WORKER_TIMEOUT: break results = res.poll() results = results.get('contacted') if results: for result in results.items(): jobinfo = {} data = result[1] print data inventory = result[0] jobinfo['inventory']=inventory jobinfo['job_id']=data['ansible_job_id'] jobinfo['cmd']=data['cmd'] jobinfo['task_id']=id uri = ConnMongoDB() client = MongoClient(uri, safe=False) db = client.ansible_log id2 = db.ansible_job.insert(jobinfo) mongoid2 = {"_id":ObjectId(id2)} if data['rc'] == 0 : thisinfo2 = db.ansible_job.find_one(mongoid2) thisinfo2['rc']=data['rc'] thisinfo2['stdout']=data['stdout'] thisinfo2['stderr']=data['stderr'] db.ansible_job.save(thisinfo2) thisinfo = db.ansible_task.find_one(mongoid) thisinfo['end'] = data['end'] thisinfo['rc'] = data['rc'] db.ansible_task.save(thisinfo) elif data['rc'] == 1 : thisinfo2 = db.ansible_job.find_one(mongoid2) thisinfo2['rc']=data['rc'] thisinfo2['stderr']=data['stderr'] db.ansible_job.save(thisinfo2) thisinfo = db.ansible_task.find_one(mongoid) thisinfo['rc'] = data['rc'] db.ansible_task.save(thisinfo) else: thisinfo2 = db.ansible_job.find_one(mongoid2) thisinfo2['rc']=data['rc'] thisinfo2['stderr']=data['msg'] db.ansible_job.save(thisinfo2) thisinfo = db.ansible_task.find_one(mongoid) thisinfo['rc'] = data['rc'] db.ansible_task.save(thisinfo) time.sleep(2)
def exec_runner(hosts): sshUser = '******' # Uncomment this to increase verbosity #ansible.utils.VERBOSITY = 4 # Hosts where execute commands example_host1 = ansible.inventory.host.Host(name=hosts[0]) example_host2 = ansible.inventory.host.Host(name=hosts[1]) # Setting hosts group task_group = ansible.inventory.group.Group(name="test") # Hosts added to group task_group.add_host(example_host1) task_group.add_host(example_host2) # Inventory task_inventory = ansible.inventory.Inventory() task_inventory.add_group(task_group) task_inventory.subset('test') # Runner runner = ansible.runner.Runner( # remote-cmd.py must be in ansible library path or in the library directory # inside the directory where this script is placed module_name='remote-cmd', # the command to be executed on remote hosts must be passed as an argument # to the module module_args='/usr/bin/python /root/test.py', remote_user=sshUser, pattern=':'.join(hosts), # Task hard limit, if task has not finished after background seconds task will be finished background=20, inventory=task_inventory, ) # This script uses kerberos authentication. To use that ssh native # client need to be supported. If ssh native client is not supported # then pub key authentication or password authentication must be used. krbCmd = "/usr/bin/kinit -kt /root/jadebustos.keytab jadebustos" execKrbCmd = subprocess.Popen(krbCmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) myStdout, myStderr = execKrbCmd.communicate() # Executing module on nodes in async mode response, poller = runner.run_async(20) # Wait until task is finished, hard time is set to 20 and polling is carried out every 8 seconds poller.wait(20, 8) items = response['contacted'].keys() # Results file for each task y copied to /tmp/hostname/username/.ansible_async/taskid for item in items: print "Procesando: " + item resFile = response['contacted'][item]['results_file'] # Runner to get remote results file to /tmp runnerFile = ansible.runner.Runner( module_name='fetch', module_args='src=' + resFile + ' dest=/tmp/ fail_on_missing=no flat=no', remote_user=sshUser, pattern=item, inventory=task_inventory, ) runnerFile.run() # Runner to remove remote results file runnerDelFile = ansible.runner.Runner( module_name='file', module_args='path=' + resFile + ' state=absent', remote_user=sshUser, pattern=item, inventory=task_inventory, ) runnerDelFile.run() # Modules output is a json return response
remote_port=task.play.remote_port, module_vars=task.module_vars, private_key_file=self.private_key_file, setup_cache=self.SETUP_CACHE, basedir=task.play.basedir, conditional=task.only_if, callbacks=self.runner_callbacks, sudo=task.sudo, sudo_user=task.sudo_user, transport=task.transport, sudo_pass=task.sudo_pass, is_playbook=True, check=self.check, diff=self.diff, environment=task.environment, complex_args=task.args, accelerate=task.play.accelerate, accelerate_port=task.play.accelerate_port, error_on_undefined_vars=C.DEFAULT_UNDEFINED_VAR_BEHAVIOR ) >>>>>>> LOCAL if task.async_seconds == 0: results = runner.run() else: results, poller = runner.run_async(task.async_seconds) self.stats.compute(results) if task.async_poll_interval > 0: # if not polling, playbook requested fire and forget, so don't poll results = self._async_poll(poller, task.async_seconds, task.async_poll_interval) else: for (host, res) in results.get('contacted', {}).iteritems(): self.runner_callbacks.on_async_ok(host, res, poller.jid) contacted = results.get('contacted',{}) dark = results.get('dark', {}) self.inventory.lift_restriction() if len(contacted.keys()) == 0 and len(dark.keys()) == 0: return None
def post(self): data = simplejson.loads(self.request.body) badcmd = [ 'reboot', 'rm', 'kill', 'pkill', 'shutdown', 'half', 'mv', 'dd', 'mkfs', '>', 'wget' ] type = data['type'] cmd = data['cmd'] inventory = data['host'] sign = data['sign'] isudo = data['sudo'] cmdinfo = cmd.split(" ", 1) print type, inventory, options.key hotkey = type + inventory + options.key print hotkey result = getmd5(hotkey) print result now = datetime.datetime.now() taskinfo = {} taskinfo['mode'] = type taskinfo['cmd'] = cmd taskinfo['inventory'] = inventory taskinfo['type'] = 'async ad-hoc' taskinfo['start'] = now.strftime(TIME_FORMAT) taskinfo['sudo'] = isudo uri = ConnMongoDB() client = MongoClient(uri, safe=False) db = client.ansible_log id = db.ansible_task.insert(taskinfo) mongoid = {"_id": ObjectId(id)} print id if sign != result: self.write("Sign is Error") else: if cmdinfo[0] in badcmd: self.write("This is Danger Shell") else: runner = ansible.runner.Runner(module_name=type, module_args=cmd, pattern=inventory, sudo=isudo, forks=ANSIBLE_FORKS) _, res = runner.run_async(time_limit=WORKER_TIMEOUT) now = time.time() while True: if res.completed or time.time() - now > WORKER_TIMEOUT: break results = res.poll() results = results.get('contacted') if results: for result in results.items(): jobinfo = {} data = result[1] print data inventory = result[0] jobinfo['inventory'] = inventory jobinfo['job_id'] = data['ansible_job_id'] jobinfo['cmd'] = data['cmd'] jobinfo['task_id'] = id uri = ConnMongoDB() client = MongoClient(uri, safe=False) db = client.ansible_log id2 = db.ansible_job.insert(jobinfo) mongoid2 = {"_id": ObjectId(id2)} if data['rc'] == 0: thisinfo2 = db.ansible_job.find_one(mongoid2) thisinfo2['rc'] = data['rc'] thisinfo2['stdout'] = data['stdout'] thisinfo2['stderr'] = data['stderr'] db.ansible_job.save(thisinfo2) thisinfo = db.ansible_task.find_one(mongoid) thisinfo['end'] = data['end'] thisinfo['rc'] = data['rc'] db.ansible_task.save(thisinfo) elif data['rc'] == 1: thisinfo2 = db.ansible_job.find_one(mongoid2) thisinfo2['rc'] = data['rc'] thisinfo2['stderr'] = data['stderr'] db.ansible_job.save(thisinfo2) thisinfo = db.ansible_task.find_one(mongoid) thisinfo['rc'] = data['rc'] db.ansible_task.save(thisinfo) else: thisinfo2 = db.ansible_job.find_one(mongoid2) thisinfo2['rc'] = data['rc'] thisinfo2['stderr'] = data['msg'] db.ansible_job.save(thisinfo2) thisinfo = db.ansible_task.find_one(mongoid) thisinfo['rc'] = data['rc'] db.ansible_task.save(thisinfo) time.sleep(2)