def tomcatdev_push(file, pclist, puthdir): ipdict = {} log = [] confname, path = process().tomcat_conf(id=file) for i in pclist: item = models.device_config.objects.get(id=i) ipdict[item.ipaddress] = item.password obj = models.task(task_name='tomcat推送', config_name=confname, task_Operated=','.join(ipdict.keys()), task_result=3) obj.save() obj_id = obj.id if len(puthdir.strip()) == 0: module_args = 'src=%s dest=/tmp/server.xml' % path else: module_args = 'src=%s dest=/%s/server.xml' % ( path, puthdir.strip().strip('/')) try: for k, y in ipdict.items(): date = ansible_api.MyRunner().cmdrun( pattern=k, module_name='copy', module_args=module_args)['contacted'] log.append(k + ':') log.append(str(date[k]['changed']) + '\n') models.task.objects.filter(id=obj_id).update(task_result=1, task_log='\n'.join(log)) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')
def nginxgroup_push(group, file): log = [] item = models.group_config.objects.get(id=group) confname, path = process().nginx_conf(id=file) obj = models.task(task_name='nginx组推送', config_name=confname, task_Operated=item.group_name, task_result=3) obj.save() obj_id = obj.id if len(item.nginx_puth.strip()) == 0: module_args = 'src=%s dest=/tmp/nginx.conf' % path else: module_args = 'src=%s dest=/%s/nginx.conf' % ( path, item.nginx_puth.strip().strip('/')) try: for i in models.device_config.objects.filter(group=group): date = ansible_api.MyRunner().cmdrun( pattern=i.ipaddress, module_name='copy', module_args=module_args)['contacted'] log.append(i.ipaddress + ':') log.append(str(date[i.ipaddress]['changed']) + '\n') models.task.objects.filter(id=obj_id).update(task_result=1, task_log='\n'.join(log)) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')
def nginxdev_push(file, pclist, puthdir): iplist = [] log = [] confname, path = process().nginx_conf(id=file) for i in pclist: item = models.device_config.objects.get(id=i) iplist.append(item.ipaddress) obj = models.task(task_name='nginx推送', config_name=confname, task_Operated=','.join(iplist), task_result=3) obj.save() obj_id = obj.id if len(puthdir.strip()) == 0: module_args = 'src=%s dest=/tmp/nginx.conf' % path else: module_args = 'src=%s dest=/%s/nginx.conf' % ( path, puthdir.strip().strip('/')) date = ansible_api.MyRunner().cmdrun(pattern=','.join(iplist), module_name='copy', module_args=module_args)['contacted'] task_result = 1 for i in iplist: try: log.append(i + ':') log.append(str(date[i]['changed']) + '\n') except Exception, e: log.append(str(date[i]['msg']) + '\n') task_result = 2 continue
def tinstallgroup_push(group_id, id): log = [] item = models.group_config.objects.get(id=group_id) confname, path = process().tomcatinstall_conf(id=id) obj = models.task(task_name='tomcat组安装', config_name=confname, task_Operated=item.group_name, task_result=3) obj.save() obj_id = obj.id try: for i in models.device_config.objects.filter(group=group_id): date = ansible_api.MyRunner().PlayBook_execute( play=path, params='{"host": "%s"}' % i.ipaddress) log.append(i.ipaddress + ':') log.append(str(date[i.ipaddress]['failures']) + '\n') models.task.objects.filter(id=obj_id).update(task_result=1, task_log='\n'.join(log)) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')
def playbook(p_id, p_name, inventory=None): basedir = models.Playbook.objects.get(id=p_id) path = os.path.join(basedir.basedir.split('.')[0], p_name) obj = models.task(task_name='playbook执行', config_name='不涉及', task_Operated='不涉及', task_result=3) obj.save() obj_id = obj.id try: if inventory: date = ansible_api.MyRunner().roles_execute( play=path, inventory=os.path.join( basedir.basedir.split('.')[0], 'inventory')) else: date = ansible_api.MyRunner().roles_execute(play=path) models.task.objects.filter(id=obj_id).update(task_result=1, task_log=date) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')
def tinstall_push(pclist, id): ipdict = {} log = [] confname, path = process().tomcatinstall_conf(id=id) for i in pclist: item = models.device_config.objects.get(id=i) ipdict[item.ipaddress] = item.password obj = models.task(task_name='tomcat安装', config_name=confname, task_Operated=','.join(ipdict.keys()), task_result=3) obj.save() obj_id = obj.id try: for k, y in ipdict.items(): date = ansible_api.MyRunner().PlayBook_execute( play=path, params='{"host": "%s"}' % k) log.append(k + ':') log.append(str(date[k]['failures']) + '\n') models.task.objects.filter(id=obj_id).update(task_result=1, task_log='\n'.join(log)) except Exception, e: models.task.objects.filter(id=obj_id).update( task_result=2, task_log='被控制机没有安装libselinux-python,或网络不可达!')