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

相关内容

热门资讯

民进党明天拟正式提名沈伯洋参选... 针对外传民进党将于13日正式拍板征召不分区民代沈伯洋参选台北市长,对决中国国民党现任市长蒋万安一事,...
内塔尼亚胡称中国支持伊朗制造导... ‍‍5月12日,外交部发言人郭嘉昆主持例行记者会。有外媒记者提问,以色列总理内塔尼亚胡在接受采访时表...
AI会取代哪些职业?多个AI意... 当前,人们日益担忧人工智能(AI)将影响就业市场。美国近期一项研究显示,多个人工智能模型在预测哪些职...
大疆Air 4无人机曝光,预估... IT之家 5 月 12 日消息,消息源 Igor Bogdanov 昨日(5 月 11 日)在 X ...
中核国电漳州能源原党委书记、董... 中核集团中核国电漳州能源有限公司原党委书记、董事长何辉涉嫌严重违纪违法,目前正接受中央纪委国家监委驻...
总投资1.85亿元,郑州市金水... 【大河财立方消息】 5月11日,金水区人民政府对金水区城市更新项目入库信息进行公示,公示期为2026...
特朗普访华期间是否会讨论台湾和... 澎湃新闻记者 聂舒翼 谢瑞强5月12日,外交部发言人郭嘉昆主持例行记者会。有记者就特朗普访华期间是否...
AI+教育,郑州航空港区成果亮... 【大河财立方 记者 程帅星】5月11日,由教育部、浙江省人民政府共同主办的2026世界数字教育大会在...
外交部:中方反对美国向中国台湾... 新华社北京5月12日电(记者万倩仪、冯歆然)外交部发言人郭嘉昆12日在例行记者会上就中美关系和台湾问...
外交部:亚太各国应擦亮眼睛,共... 新华社北京5月12日电(记者董雪、孙楠)外交部发言人郭嘉昆5月12日在例行记者会上回答相关问题时表示...