ansible + tornado + MongoDB
admin
2023-04-12 15:41:43
0

http://blog.csdn.net/smallfish2983/article/details/38078019


照着上面那哥们写的,初学,不要吐血,基本功能实现了。


ansible + tornado + MongoDB

ansible + tornado + MongoDB

ansible + tornado + MongoDB



root@ubuntu12:~/ansible/tornado# tree
.
├── templates
│   ├── index.html
│   └── result.html
└── test.py


root@ubuntu12:~/ansible/tornado# cat test.py

#coding:utf-8  

import os.path
import tornado.locale
import tornado.httpserver
import tornado.ioloop  
import tornado.options  
import tornado.web  
from tornado.options import define, options  
import pymongo  


define("port", default=8000, help="run on the given port", type=int)  

class Application(tornado.web.Application):  
    def __init__(self):  
        #初始化一些东西  
        handlers = [  
            #url匹配  
            (r"/", MainHandler),  
            (r"/index", MainHandler),   
            (r"/result", Module_actionHandler),  
 
        ]  
        settings = dict(  
            #程序设置,字典形式  
            template_path=os.path.join(os.path.dirname(__file__), "templates"),  
            #设置模板文件路径  
            static_path=os.path.join(os.path.dirname(__file__), "static"),  
            #设置静态文件路径,如css\jpg\gif等  
            # ui_modules={"Book": BookModule},  
            #设置ui模块,可以用字典添加多个  
            debug=True,  
        )  

        tornado.web.Application.__init__(self, handlers, **settings)  
        #传入设置配置  

class MainHandler(tornado.web.RequestHandler):  
    #主页函数方法  
    def get(self):  
        #设置get方法函数  
        self.render(  
            "index.html",  
        )  

class Module_actionHandler(tornado.web.RequestHandler):  
    #定义模块操作函数方法  
       
 
    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(  
            #根据ansible的api来运行脚本  
            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:  
                # dark返回不为空则表示操作失败了  
                return result['dark'],'失败!'  
            else:  
                return result['contacted'],'成功!'  
        result = pars_result(result)  

        self.render(  
            "result.html", 
            message = result[0],  
            result = result[1]  
        )  

if __name__ == "__main__":  
    tornado.options.parse_command_line()  
    http_server = tornado.httpserver.HTTPServer(Application())  
    http_server.listen(options.port)  
    tornado.ioloop.IOLoop.instance().start() 


# cat index.html



    ansible
    
        

Enter arg bellow:


        

        

pattern


        

module_name


        

module_args


        
        

    


# cat result.html



    ansible result
    
        

ansible result


               

message: `message`


               

result: `result`


    


附件:http://down.51cto.com/data/2364907

相关内容

热门资讯

今日重大消息“么么都莱游戏究竟... 网上科普关于“么么都莱游戏有没有挂”话题很是火热,小编也是针对么么都莱游戏作*弊开挂的方法以及开挂对...
玩家分享攻略“顺欣茶楼开挂器?... 您好:顺欣茶楼这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游戏...
玩家分享攻略“微乐云南麻将有挂... 玩家分享攻略“微乐云南麻将有挂吗?”(其实是有挂)您好,微乐云南麻将这个游戏其实有挂的,确实是有挂的...
我来教教您“创乐缘真的有挂吗?... 我来教教您“创乐缘真的有挂吗?”(必胜开挂神器)您好,创乐缘这个游戏其实有挂的,确实是有挂的,需要了...
【第一消息】“乐禧乾安麻将真的... 有 亲,根据资深记者爆料乐禧乾安麻将是可以开挂的,确实有挂(咨询软件无需...
今日重大通报“成就联盟到底有挂... 有 亲,根据资深记者爆料成就联盟是可以开挂的,确实有挂(咨询软件无需打开...
凤凰晚报|被“斩杀”的美国童星... 今日人物【美国“斩杀线”斩落知名童星?】2025年9月的加州里弗赛德市,一名36岁的流浪男子引起一名...
玩家攻略科普“旺旺冲击麻将真的... 有 亲,根据资深记者爆料旺旺冲击麻将是可以开挂的,确实有挂(咨询软件无需...
网红罗大美遇害两年后下葬,妹妹... 据九派新闻,12月28日,罗大美妹妹发文:近日在亲属的陪伴下,哥哥的后事已经妥善完成。在一个小小的仪...
最新引进“数独大赛开挂神器?”... 家人们!今天小编来为大家解答数独大赛透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里买很...