ansible+tornado+MongoDB-创新互联

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

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:ansible+tornado+MongoDB-创新互联
本文URL:http://myzitong.com/article/cejoos.html