def apps_model(request): if request.method == "GET": serverList = Server_Assets.objects.all() return render_to_response('apps/apps_model.html',{"user":request.user, "serverList":serverList}, context_instance=RequestContext(request)) elif request.method == "POST" and request.user.has_perm('OpsManage.can_change_ansible_playbook'): resource = [] sList = [] serverList = request.POST.getlist('ansible_server') for server in serverList: server_assets = Server_Assets.objects.get(id=server) sList.append(server_assets.ip) if server_assets.keyfile == 1:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port)}) else:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username,"password": server_assets.passwd}) if len(request.POST.get('custom_model')) > 0:model_name = request.POST.get('custom_model') else:model_name = request.POST.get('ansible_model',None) redisKey = base.makeToken(strs=str(request.user)+"ansible_model") DsRedis.OpsAnsibleModel.delete(redisKey) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Start] Ansible Model: {model} ARGS:{args}".format(model=model_name,args=request.POST.get('ansible_agrs',"None"))) ANS = ANSRunner(resource,redisKey) ANS.run_model(host_list=sList,module_name=model_name,module_args=request.POST.get('ansible_agrs',"")) DsRedis.OpsAnsibleModel.lpush(redisKey, "[Done] Ansible Done.") #操作日志异步记录 recordAnsibleModel.delay(user=str(request.user),ans_model=model_name,ans_server=','.join(sList),ans_args=request.POST.get('ansible_agrs',None)) return JsonResponse({'msg':"操作成功","code":200,'data':[]})
def ansible_run(request): if request.method == "POST": if request.POST.get('model') == 'model': strs = str(request.user)+"ansible_model" redisKey = base.makeToken(strs) msg = DsRedis.OpsAnsibleModel.rpop(redisKey) elif request.POST.get('model') == 'playbook': redisKey = request.POST.get('playbook_uuid') msg = DsRedis.OpsAnsiblePlayBook.rpop(redisKey) if msg:return JsonResponse({'msg':msg,"code":200,'data':[]}) else:return JsonResponse({'msg':None,"code":200,'data':[]})