def yum_install_package(name, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting yum install package %s ... " % name, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='shell', module_args='rpm -q %s ' % name, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) status = result['contacted'][host]['rc'] if status == 0: details = "SKIP: The package %s exist in system" % name handle_ansible_info(details, host_post_info, "INFO") return True else: details = "Installing package %s ..." % name handle_ansible_info(details, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='yum', module_args='name=' + name + ' disable_gpg_check=no state=latest', pattern=host ) result = runner.run() logger.debug(result) if 'failed' in result['contacted'][host]: description = "ERROR: YUM install package %s failed" % name handle_ansible_failed(description, result, host_post_info) sys.exit(1) else: details = "SUCC: yum install package %s successful!" % name handle_ansible_info(details, host_post_info, "INFO") return True
def yum_remove_package(name, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting yum remove package %s ... " % name, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='shell', module_args='yum list installed ' + name, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) status = result['contacted'][host]['rc'] if status == 0: details = "Removing %s ... " % name handle_ansible_info(details, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='yum', module_args='name=' + name + ' state=absent', pattern=host ) result = runner.run() logger.debug(result) if 'failed' in result['contacted'][host]: description = "ERROR: Yum remove package %s failed!" % name handle_ansible_failed(description, result, host_post_info) sys.exit(1) else: details = "SUCC: Remove package %s " % name handle_ansible_info(details, host_post_info, "INFO") return True else: details = "SKIP: The package %s is not exist in system" % name handle_ansible_info(details, host_post_info, "INFO") return True
def authorized_key(user, key_path, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time if not os.path.exists(key_path): logger.info("key_path %s is not exist!" % key_path) sys.exit(1) private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Updating key %s to host %s" % (key_path, host), host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='shell', module_args="cat %s" % key_path, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: key = result['contacted'][host]['stdout'] key = '\'' + key + '\'' args = "user=%s key=%s" % (user, key) runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='authorized_key', module_args="user=%s key=%s" % (user, key), pattern=host ) result = runner.run() logger.debug(result) if 'failed' in result['contacted'][host]: description = "ERROR: Authorized on remote host %s failed!" % host handle_ansible_failed(description, result, host_post_info) sys.exit(1) else: details = "SUCC: update public key to host %s" % host handle_ansible_info(details, host_post_info, "INFO") return True
def raw_runner(command, pattern, inventory): runner = ansible.runner.Runner(pattern=pattern, timeout=5, module_name='raw', module_args=command, inventory=inventory) results = runner.run() output = '' if results is None: return '没有发现配置好的主机' for (hostname, result) in results['contacted'].items(): if not 'failed' in result: output += ('[{0}] 成功的执行结果 >>> \n{1}\n'.format( hostname, result['stdout'])) for (hostname, result) in results['contacted'].items(): if 'failed' in result: output += ('[{0}] 失败的执行结果 >>> \n{1}\n'.format( hostname, result['msg'])) for (hostname, result) in results['dark'].items(): output += ('[{0}] 执行结果 >>> \n{1}\n'.format(hostname, result['msg'])) return output
def _run_module(self, pattern, module, args, vars, remote_user, async_seconds, async_poll_interval, only_if, sudo, transport, port): ''' 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) if port is None: port=self.remote_port runner = ansible.runner.Runner( pattern=pattern, inventory=self.inventory, module_name=module, module_args=args, forks=self.forks, remote_pass=self.remote_pass, module_path=self.module_path, timeout=self.timeout, remote_user=remote_user, remote_port=port, module_vars=vars, setup_cache=SETUP_CACHE, basedir=self.basedir, conditional=only_if, callbacks=self.runner_callbacks, debug=self.debug, sudo=sudo, transport=transport, sudo_pass=self.sudo_pass, is_playbook=True ) if async_seconds == 0: results = runner.run() else: results = self._async_poll(runner, hosts, async_seconds, async_poll_interval, only_if) self.inventory.lift_restriction() 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'] host = data['host'] sign = data['sign'] cmdinfo = cmd.split(" ",1) print type,host,options.key hotkey = type+host+options.key print hotkey result = getmd5(hotkey) print result 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=host, forks=10 ) datastructure = runner.run() self.write(datastructure)
def start(self): runner = ansible.runner.Runner( module_name=self.module_name, module_args=self.cmd, pattern=self.host, timeout=self.timeout, ) log.debug('ansible %s RunCommand: %s' % (self.host, self.cmd)) # import pdb # pdb.set_trace() datastructure = runner.run() # print datastructure log.debug('ansible sttout %s' % datastructure) # print datastructure if datastructure['dark']: pass else: if not datastructure['contacted'][self.host]['rc']: data = datastructure['contacted'][self.host]['stdout'] return data else: return None
def AnsibleApi(AnsibleModuleName, AnsibleInventory, AnsibleCommand): ReturnMsg = {} runner = ansible.runner.Runner( module_name=AnsibleModuleName, inventory=AnsibleInventory, module_args=AnsibleCommand, environment={ 'LANG': 'zh_CN.UTF-8', 'LC_CTYPE': 'zh_CN.UTF-8' }, ) ReturnValue = runner.run() if ReturnValue is None: ReturnMsg['code'] = "-1" ReturnMsg['msg'] = "hosts not found" for (hostname, result) in ReturnValue['contacted'].items(): if result['rc'] == "0": ReturnMsg['code'] = "0" ReturnMsg['msg'] = result['stdout'] else: ReturnMsg['code'] = str(result['rc']) ReturnMsg['msg'] = result['stdout'] for (hostname, result) in ReturnValue['dark'].items(): ReturnMsg['code'] = "-3" ResturnMsg['msg'] = result return json.dumps(ReturnMsg)
def file_operation(file, args, host_post_info): ''''This function will change file attribute''' start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting change file %s ... " % file, host_post_info, "INFO") args = "path=%s " % file + args runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='file', module_args=args, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: if 'failed' in result['contacted'][host]: details = "INFO: %s not be changed" % file handle_ansible_info(details, host_post_info, "INFO") return False else: details = "INFO: %s changed successfully" % file handle_ansible_info(details, host_post_info, "INFO") return True
def check_command_status(command, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting run command [ %s ] ..." % command, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='shell', module_args=command, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: status = result['contacted'][host]['rc'] if status == 0: details = "SUCC shell command: '%s' return 0 " % command handle_ansible_info(details, host_post_info, "INFO") return True else: details = "INFO: shell command %s failed " % command handle_ansible_info(details, host_post_info, "WARNING") return False
def run_module(self, host_list, module, args, forks=1, ans_remote_user=AnsibleConstants.DEFAULT_REMOTE_USER, ans_remote_pass=AnsibleConstants.DEFAULT_REMOTE_PASS, use_sudo=True, local=False): """Runs an ansible module and returns its results :rtype : The result of the ansible execution """ use_transport = AnsibleConstants.DEFAULT_TRANSPORT if local or use_transport == 'local' or host_list == ['127.0.0.1']: use_transport = "local" host_list = [] host_list.append("127.0.0.1") # From: http://www.ansibleworks.com/docs/playbooks2.html#id20 # To run an entire playbook locally, just set the "hosts:" line to "hosts:127.0.0.1" and then run the playbook like so: # ansible-playbook playbook.yml --connection=local runner = ansible.runner.Runner( host_list=host_list if host_list != [] else self.__host_list, module_name=module, module_args=args, transport=use_transport, remote_user=ans_remote_user, remote_pass=ans_remote_pass, sudo=use_sudo) data = runner.run() return data
def batch(self,request): if request.method == 'POST': token = request.META['HTTP_TOKEN'] user_name = self.token_cache.get(token) #get user_name from cache record_key = token+'batch' record = self.record_cache.get(record_key) if user_name is not None and record is None: #someone had called auth function but never call batch in one minute ip_list = request.data["ip_list"] cmd = request.data['command'] ip_list = ','.join(ip_list) + ',' if cmd.find(".sh") != -1 or cmd.find(".py") != -1: runner = ansible.runner.Runner(module_name='script',remote_user='******', host_list=ip_list, timeout=10,pattern='all', module_args=os.path.join(settings.UPLOAD_ROOT+ '/' + cmd), forks=100) else: runner = ansible.runner.Runner(module_name='shell', remote_user='******', host_list=ip_list,pattern='all', module_args=cmd,forks=100) result = runner.run() logger.info(result) log = Log() log.write_log(result, user_name) self.record_cache.set(record_key, 'batch') content = {'msg': result, 'retcode': 200} return Response(content,status=status.HTTP_200_OK) elif user_name is None: logger.info('your token had expired') content = {'msg': 'your token had expired', 'retcode': 500} return Response(content,status.HTTP_500_INTERNAL_SERVER_ERROR) elif record is not None: logger.info('Request asked only once per minute') content = {'msg': 'Request asked only once per minute,please try again after one minute.', 'retcode': 500} return Response(content,status.HTTP_500_INTERNAL_SERVER_ERROR) elif request.method == 'GET': logger.info('Method GET not allowed.') content = {'msg': 'Method GET not allowed.', 'retcode': 500} return Response(content,status.HTTP_500_INTERNAL_SERVER_ERROR)
def remote_execution(request): """ remote command execution """ page_template = 'remote_execution.html' ret = '' tgtcheck = '' danger = ('rm', 'reboot', 'init ', 'shutdown', 'll') user = request.user if request.method == 'POST': action = request.get_full_path().split('=')[1] if action == 'exec': tgt = request.POST.get('tgt') arg = request.POST.get('arg') tgtcheck = Device.objects.filter(device_caption=tgt) argcheck = arg not in danger if tgtcheck and argcheck: #sapi = SaltAPI(url=settings.SALT_API['url'], username=settings.SALT_API['user'], # password=settings.SALT_API['password']) #ret = sapi.remote_execution(tgt, 'cmd.run', arg) runner = ansible.runner.Runner(module_name='command', module_args=arg, pattern=tgt) datastructure = runner.run() jdata = json.dumps(datastructure, indent=4) kdata = json.loads(jdata) ret = kdata.get('contacted').get(tgt).get('stdout') #ret = 'test!' elif not tgtcheck: ret = '目标主机不正确,请重新输入!' elif not argcheck: ret = '命令很危险, 请确认!' context = {'sub_title': '远程管理', 'page_title': '运维工具', 'ret': ret} return render(request, page_template, context)
def raw_runner(command, pattern, inventory): runner = ansible.runner.Runner( pattern=pattern, timeout=5, module_name='raw', module_args=command, inventory=inventory ) results = runner.run() output = '' if results is None: return '没有发现配置好的主机' for (hostname, result) in results['contacted'].items(): if not 'failed' in result: output += ('[{0}] 成功的执行结果 >>> \n{1}\n'.format(hostname, result['stdout'])) for (hostname, result) in results['contacted'].items(): if 'failed' in result: output += ('[{0}] 失败的执行结果 >>> \n{1}\n'.format(hostname, result['msg'])) for (hostname, result) in results['dark'].items(): output += ('[{0}] 执行结果 >>> \n{1}\n'.format(hostname, result['msg'])) return output
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 fetch_struct(pattern, retries=0): """ Create a basic structure using ansible's Runner :param pattern: Host pattern to run on :type pattern: str :param retries: Number of retries to use when host is unreachable or times out :type retries: int :return: A dictionary containting the output of the setup module :rtype: dict """ runner = gen_runner(pattern) struct = runner.run() for r in range(int(retries)): if not len(struct['dark']) == 0: newpattern = ':'.join(struct['dark'].keys()) print "Retrying %s" % newpattern newrunner = gen_runner(newpattern, forks=10, timeout=2) newstruct = newrunner.run() for host in newstruct['contacted'].keys(): try: struct['dark'].pop(host) except KeyError: pass for host in newstruct['contacted'].keys(): struct['contacted'][host] = newstruct['contacted'][host] return struct
def get_real_data(host, custom_commands=None): """ Runs the setup module on a host. When passed a custom command, it is appended to the returned struct :param host: The host to run on :type host: str :param custom_commands: The custom command that needs to be included :type custom_commands: list :return: A dictionary containing the output of the setup module as generated by ansible.runner.Runner :rtype: dict """ runner = ansible.runner.Runner( module_name="setup", module_args="", forks=1, pattern=host, ) data = runner.run() try: struct = flatten_ansible_struct(data) if custom_commands: struct[host]['custom_commands'] = custom_commands return struct except KeyError: pass
def run_module(self, host_list, module, args, timeout=AnsibleConstants.DEFAULT_TIMEOUT, forks=1, ans_remote_user=AnsibleConstants.DEFAULT_REMOTE_USER, ans_remote_pass=AnsibleConstants.DEFAULT_REMOTE_PASS, use_sudo=True, local=False): """Runs an ansible module and returns its results :rtype : The result of the ansible execution """ use_transport = AnsibleConstants.DEFAULT_TRANSPORT if local or use_transport == 'local' or host_list == ['127.0.0.1']: use_transport = "local" host_list = ["127.0.0.1"] # From: http://www.ansibleworks.com/docs/playbooks2.html#id20 # To run an entire playbook locally, just set the "hosts:" line to "hosts:127.0.0.1" and then run the playbook like so: # ansible-playbook playbook.yml --connection=local runner = ansible.runner.Runner(host_list=host_list if host_list != [] else self.__host_list, module_name=module, module_args=args, transport=use_transport, remote_user=ans_remote_user, remote_pass=ans_remote_pass, sudo=use_sudo, timeout=timeout ) data = runner.run() return data
def setup_getupgrades(host, web2py_path, remote_user=None, private_key=None): import ansible.runner module_path = os.path.join(current.request.folder, "private", "playbook", "library") if private_key: private_key = os.path.join(current.request.folder, "uploads", private_key) inventory = ansible.inventory.Inventory([host]) if private_key and remote_user: runner = ansible.runner.Runner(module_name = "upgrade", module_path = module_path, module_args = "web2py_path=/home/%s" % web2py_path, remote_user = remote_user, private_key_file = private_key, pattern = host, inventory = inventory, sudo = True, ) else: runner = ansible.runner.Runner(module_name = "upgrade", module_path = module_path, module_args = "web2py_path=/home/%s" % web2py_path, pattern = host, inventory = inventory, sudo = True, ) response = runner.run() return response
def getHostinfoByName(hostname): import ansible.runner files_ini = '/home/qa/miles/script/ansible/vm.ini' runner = ansible.runner.Runner(host_list=files_ini, module_name='setup' \ , pattern=hostname) output = runner.run() hostinfo = dict() hostinfo['host_name']=hostname hostinfo['mem'] = output['contacted'][hostname]['ansible_facts']['ansible_memory_mb']['real']['total'] hostinfo['os'] = "%s %s" % (output['contacted'][hostname]['ansible_facts']['ansible_distribution'] \ , output['contacted'][hostname]['ansible_facts'][ 'ansible_distribution_version']) ips = output['contacted'][hostname]['ansible_facts']['ansible_all_ipv4_addresses'] priv_ip_l = [] pub_ip_l = [] for ip in ips.split(','): if str(ip.split('.')[0]) in ['172', '192', '10']: priv_ip_l.append(ip) else: pub_ip_l.append(ip) hostinfo['private_ip'] = priv_ip_l hostinfo['public_ip'] = pub_ip_l return hostinfo
def apt_install_packages(name, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting apt install package %s ... " % name, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='apt', module_args='name=' + name + ' state=present', pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: if 'failed' in result['contacted'][host]: description = "ERROR: Apt install %s failed!" % name handle_ansible_failed(description, result, host_post_info) sys.exit(1) else: details = "SUCC: apt install package %s " % name handle_ansible_info(details, host_post_info, "INFO") return True
def ansible_run(args, check=False): """ run the mysql_query module with ansible against localhost and return only the results for localhost (assuming localhost was contacted successfully) :param args: :return: """ complex_args, module_args = [], [] for key, value in args.items(): (complex_args if isinstance(value, dict) else module_args).append((key, value)) runner = ansible.runner.Runner( module_name='mysql_query', module_args=dict(module_args), complex_args=dict(complex_args), inventory=Inventory(['localhost']), transport='local', check=check ) result = runner.run() if result['contacted'].has_key('localhost'): return result['contacted']['localhost'] else: raise Exception('could not contact localhost at all: %s' % str(result['dark']['localhost']))
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 apt_update_cache(cache_valid_time, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting apt update cache ", host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='apt', module_args='update_cache=yes cache_valid_time=%d' % cache_valid_time, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: if 'failed' in result['contacted'][host]: description = "ERROR: Apt update cache failed!" handle_ansible_failed(description, result, host_post_info) sys.exit(1) else: details = "SUCC: apt update cache successful! " handle_ansible_info(details, host_post_info, "INFO") return True
def post(self, *args, **kwargs): pattern = self.get_arguments('pattern')[0] module_name = self.get_arguments('module_name')[0] module_args = self.get_arguments('module_args')[0] import ansible.runner runner = ansible.runner.Runner( module_name=module_name, module_args=module_args, pattern=pattern, ) result = runner.run() #conn = pymongo.Connection("localhost", 27017) #db = conn["ansible"] #if type(result) == dict: # db.ansible.insert(result) def pars_result(result): if len(result['dark']) > 0: return result['dark'], 'ERRO!' else: return result['contacted'], 'OK!' result = pars_result(result) self.render("result.html", message=json.dumps(result[0], sort_keys=True, indent=4, separators=(',', ': ')), result=result[1])
def get_ulimit(ip): # 最大文件打开数 runner = ansible.runner.Runner(module_name='shell', module_args='ulimit -n', pattern='%s' % ip, forks=2) datastructure = runner.run() result = datastructure['contacted'][ip]['stdout'] return result
def set_ini_file(file, section, option, value, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting update file %s section %s ... " % (file, section), host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='ini_file', module_args='dest=' + file + ' section=' + section + ' option=' + option + " value=" + value, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: details = "SUCC: Update file: %s option: %s value %s" % (file, option, value) handle_ansible_info(details, host_post_info, "INFO") return True
def set_selinux(args, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Changing service status", host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='selinux', module_args=args, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: if 'failed' in result['contacted'][host]: description = "ERROR: set selinux to %s failed" % args handle_ansible_failed(description, result, host_post_info) sys.exit(1) else: details = "SUCC: Reset selinux to %s" % args handle_ansible_info(details, host_post_info, "INFO") return True
def file_dir_exist(name, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting check file or dir exist %s ... " % name, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='stat', module_args=name, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: status = result['contacted'][host]['stat']['exists'] if status is True: details = "INFO: %s exist" % name handle_ansible_info(details, host_post_info, "INFO") return True else: details = "INFO: %s not exist" % name handle_ansible_info(details, host_post_info, "INFO") return False
def _ansible_shells(host, mod_type, mod): runner = ansible.runner.Runner(host_list=host, module_name=mod_type, module_args=mod, remote_user="******", forks=10) return runner.run()
def get_data(): runner = ansible.runner.Runner(module_name='setup', module_args='', pattern='all', forks=2) datastructure = runner.run() return datastructure
def run_custom_command(pattern, command): runner = ansible.runner.Runner( module_name="shell", module_args=command, pattern=pattern, ) return runner.run()
def run_against_inventory(inv=ansible.inventory.Inventory([]), mod_name='ping', mod_args='', pattern='*', forks=10): ''' Run a single task against an Inventory. inv : Ansible Inventory object mod_name : module name mod_args : extra arguments for the module pattern : hosts or groups to match against forks : number of forks for the runner to create (default = 10) ''' try: # create a runnable task and execute runner = ansible.runner.Runner( module_name=mod_name, module_args=mod_args, pattern=pattern, forks=forks, inventory=inv, ) return runner.run() except Exception as e: raise e
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 get_tcpdump(self, host, command, header): """Perform TCPDUMP on Compute Node and Capture it's data""" runner = ansible.runner.Runner( module_name='shell', module_args=command, pattern=host, ) results = runner.run() # Print Error For Host for node in results['dark']: dark_table = PrettyTable(["Failed to Contact Host [%s] with error" % node]) dark_table.align["Failed to Contact Host [%s] with error" % node] = "l" dark_table.add_row([results['dark'][node]['msg']]) print dark_table # Print Results of STDOUT for node in results['contacted']: lines = results['contacted'][node]['stdout'].split('\n') results_table = PrettyTable([header]) # if 'Ping' in header: results_table.align[header] = "l" for i in lines: results_table.add_row([i]) print results_table
def save_info(ip): ip1 = json.dumps(ip) ip2 = str(ip) runner = ansible.runner.Runner( host_list='/var/www/cmdb/enndc_management/ops/inventory', module_name='setup', module_args='', pattern=ip, forks=10, remote_user="******", remote_pass="******", #remote_tmp='/tmp/.ansible/tmp', become=True, become_method="sudo", become_user="******", become_pass="******", #become_exe='sudo' ) data_structure = runner.run() # ansible_facts #data = json.dumps(data_structure, indent=4) #info = json.loads(data) facts = data_structure['contacted'][ip] #facts = info['contacted'][ip]['ansible_facts'] #hostname = facts['ansible_hostname'] #OS = facts['ansible_distribution'] #OsVersion = facts['ansible_distribution_version'] #CPU = facts['ansible_processor_count'] #mem = facts['ansible_memtotal_mb'] # k = facts['contacted'] print ip2, print '#########################' print json.dumps(facts, indent=4)
def index(): if request.method == "POST": cmd = request.form.get('cmd', type=str, default=None) ip = request.form.get('ip', type=str, default=None) host_cfg = os.path.join(BASE_DIR, 'host.cfg') print cmd, ip, host_cfg if cmd and ip: runner = ansible.runner.Runner( host_list=os.path.join(BASE_DIR, host_cfg), module_name='shell', module_args=cmd, pattern=ip, forks=10 ) datastructure = runner.run() for key, value in datastructure.items(): print(key,value) if 'contacted' in key: exec_result = value msg = (exec_result.get(ip)).get(u'stdout') return render_template('index.html', result=msg, ip=ip, cmd=cmd) else: return render_template('index.html') else: return render_template('index.html')
def get_remote_host_info(host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting get remote host %s info ... " % host, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='setup', module_args='filter=ansible_distribution*', pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: (distro, version) = [result['contacted'][host]['ansible_facts']['ansible_distribution'], int(result['contacted'][host]['ansible_facts']['ansible_distribution_major_version'])] handle_ansible_info("SUCC: Get remote host %s info successful" % host, host_post_info, "INFO") return (distro, version)
def yum_enable_repo(name, enablerepo, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Starting enable yum repo %s ... " % name, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='yum', module_args='name=' + name + ' enablerepo=' + enablerepo + " state=present", pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: if 'failed' in result['contacted'][host]: description = "ERROR: Enable yum repo failed" handle_ansible_failed(description, result, host_post_info) sys.exit(1) else: details = "SUCC: yum enable repo %s " % enablerepo handle_ansible_info(details, host_post_info, "INFO") return True
def update_file(dest, args, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Updating file %s" % dest, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='lineinfile', module_args="dest=%s %s" % (dest, args), pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: if 'failed' in result['contacted'][host]: description = "ERROR: Update file %s failed" % dest handle_ansible_failed(description, result, host_post_info) sys.exit(1) else: details = "SUCC: Update file %s" % dest handle_ansible_info(details, host_post_info, "INFO") return True
def yum_check_package(name, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Searching yum package %s ... " % name, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='shell', module_args='rpm -q %s ' % name, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) status = result['contacted'][host]['rc'] if status == 0: details = "SUCC: The package %s exist in system" % name handle_ansible_info(details, host_post_info, "INFO") return True else: details = "SUCC: The package %s not exist in system" % name handle_ansible_info(details, host_post_info, "INFO") return False
def get_info(ip): data = {} runner = ansible.runner.Runner(module_name='setup', module_args='', pattern='%s' % ip, forks=2) datastructure = runner.run() sn = datastructure['contacted'][ip]['ansible_facts']['ansible_product_serial'] host_name = datastructure['contacted'][ip]['ansible_facts']['ansible_hostname'] description = datastructure['contacted'][ip]['ansible_facts']['ansible_lsb']['description'] ansible_machine = datastructure['contacted'][ip]['ansible_facts']['ansible_machine'] sysinfo = '%s %s' % (description, ansible_machine) os_kernel = datastructure['contacted'][ip]['ansible_facts']['ansible_kernel'] cpu = datastructure['contacted'][ip]['ansible_facts']['ansible_processor'][1] cpu_count = datastructure['contacted'][ip]['ansible_facts']['ansible_processor_count'] cpu_cores = datastructure['contacted'][ip]['ansible_facts']['ansible_processor_cores'] mem = datastructure['contacted'][ip]['ansible_facts']['ansible_memtotal_mb'] ipadd_in = datastructure['contacted'][ip]['ansible_facts']['ansible_all_ipv4_addresses'][0] disk = datastructure['contacted'][ip]['ansible_facts']['ansible_devices']['sda']['size'] # print sysinfo data['sn'] = sn data['sysinfo'] = sysinfo data['cpu'] = cpu data['cpu_count'] = cpu_count data['cpu_cores'] = cpu_cores data['mem'] = mem data['disk'] = disk data['ipadd_in'] = ipadd_in data['os_kernel'] = os_kernel data['host_name'] = host_name return data
def get_dhcp_logs(network_nodes, mac, network_id): """Get DHCP Logs From Network Node""" dhcp_namespace = "qdhcp-%s" % network_id dhcp_agent_inventory = ansible.inventory.Inventory(network_nodes) dhcp_logs_command = "(echo '***** %s [ip a] ******';ip netns exec %s ip a); (echo '******************** DHCP LOGS *********************'; grep -w %s /var/log/messages | grep -v ansible-command | tail -5)" % ( dhcp_namespace, dhcp_namespace, mac) runner = ansible.runner.Runner( module_name='shell', module_args=dhcp_logs_command, inventory=dhcp_agent_inventory, ) result = runner.run() for agent in result['contacted']: dhcp_log = [] for line in result['contacted'][agent]['stdout'].split('\n'): if line != "": dhcp_log.append(line) dhcp_header = "DHCP Data on host [%s] for Mac Address: %s" % (agent, mac) dhcp_table = PrettyTable([dhcp_header]) dhcp_table.align[dhcp_header] = "l" for log in dhcp_log: dhcp_table.add_row([log]) print dhcp_table
def post(self, *args, **kwargs): pattern = self.get_arguments('pattern')[0] module_name = self.get_arguments('module_name')[0] module_args = self.get_arguments('module_args')[0] import ansible.runner runner = ansible.runner.Runner( module_name = module_name, module_args = module_args, pattern = pattern, ) result = runner.run() #conn = pymongo.Connection("localhost", 27017) #db = conn["ansible"] #if type(result) == dict: # db.ansible.insert(result) def pars_result(result): if len(result['dark'])>0: return result['dark'],'ERRO!' else: return result['contacted'],'OK!' result = pars_result(result) self.render( "result.html", message = json.dumps(result[0], sort_keys=True, indent=4, separators=(',', ': ')), result = result[1] )
def check_pip_version(version, host_post_info): start_time = datetime.now() host_post_info.start_time = start_time private_key = host_post_info.private_key host_inventory = host_post_info.host_inventory host = host_post_info.host post_url = host_post_info.post_url handle_ansible_info("INFO: Check pip version %s exist ..." % version, host_post_info, "INFO") runner = ansible.runner.Runner( host_list=host_inventory, private_key_file=private_key, module_name='shell', module_args="pip --version | grep %s" % version, pattern=host ) result = runner.run() logger.debug(result) if result['contacted'] == {}: ansible_start = AnsibleStartResult() ansible_start.host = host ansible_start.post_url = post_url ansible_start.result = result handle_ansible_start(ansible_start) sys.exit(1) else: status = result['contacted'][host]['rc'] if status == 0: details = "SUCC: Check pip-%s exist in system " % version handle_ansible_info(details, host_post_info, "INFO") return True else: details = "INFO: Check pip-%s is not exist in system" % version handle_ansible_info(details, host_post_info, "INFO") return False
def model(self): runner = ansible.runner.Runner(module_name=self.module_name, module_args=self.module_args, pattern=self.pattern, host_list=self.host_list, forks=100) data_list = [] data = runner.run() data = json.dumps(data, indent=4) for k, v in json.loads(data).items(): if k == "contacted": if self.module_name == "raw": for x, y in v.items(): data = {} data['ip'] = x data['msg'] = y.get('stdout').replace( '\t\t', '<br>').replace('\r\n', '<br>').replace('\t', '<br>') if y.get('rc') == 0: data['status'] = 'succeed' else: data['status'] = 'failed' data_list.append(data) elif self.module_name == "ping": for x, y in v.items(): data = {} data['ip'] = x if y.get('failed'): data['msg'] = y.get('msg') data['status'] = 'failed' else: data['msg'] = y.get('ping') data['status'] = 'succeed' data_list.append(data) else: for x, y in v.items(): data = {} data['ip'] = x if y.get('failed'): data['msg'] = y.get('msg') data['status'] = 'failed' else: data['msg'] = "%s %s succeed" % (self.module_name, self.module_args) data['status'] = 'succeed' data_list.append(data) elif k == "dark": for x, y in v.items(): data = {} data['status'] = 'failed' data['ip'] = x data['msg'] = y.get('msg') data_list.append(data) if data_list: return data_list else: return False
def get_iface_list (self,conffile,debugfile,inventory): module_args = "path=[sensor]interfaces conffile=%s debugfile=%s makebackup=False op=get" % (conffile,debugfile) i = ansible.inventory.Inventory (inventory) runner = ansible.runner.Runner( module_name='av_config', module_args = module_args, pattern='*',inventory = i) response = runner.run() nose.tools.ok_ (response['contacted'].get ('127.0.0.1'),msg = "System doesn't return data") ifaces = response['contacted']['127.0.0.1']['data'] return ifaces
def fetch(self, src, dest, flat='yes'): runner = ansible.runner.Runner( module_name='fetch', module_args='src=%s dest=%s flat=%s' % (src, dest, flat), remote_user=self.remote_user, inventory=self.inventory, ) out = runner.run() return out
def cmds(command,hosts,module="shell",n=5): runner = ansible.runner.Runner( module_name = module, module_args = command, pattern =hosts, forks = n ) result = runner.run() return result
def copy(self, src, dest): runner = ansible.runner.Runner( module_name='copy', module_args='src=%s dest=%s' % (src, dest), remote_user=self.remote_user, inventory=self.inventory, ) out = runner.run() return out
def run(self, host, remote_user, remote_pass, inventory, kwargs): runner = ansible.runner.Runner(module_name='setup', module_args='', inventory=inventory, pattern=host, remote_user=remote_user, remote_pass=remote_pass) result = runner.run() return result
def rsync_file(ip): # 最大文件打开数 args = "src=/opt/django/skyoms/ dest=/opt/django/skyoms/ delete=yes" runner = ansible.runner.Runner(module_name='synchronize', module_args="%s" % args, pattern='%s' % ip, forks=2) datastructure = runner.run() # print datastructure result = datastructure['contacted'][ip] return result
def test_ping(self): runner = ansible.runner.Runner(module_name='ping', module_args='', forks=10, inventory=Inventory(['localhost']), transport='local') datastructure = runner.run() self.assertEqual(len(datastructure['contacted']), 1) self.assertEqual(len(datastructure['dark']), 0)