http://blog.csdn.net/smallfish1983/article/details/38078019
According to the above man wrote, beginners, do not spit blood, basic skills can be achieved.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4B/06/wKioL1QnvwOSkcQiAADRwubmzMo149.jpg "style=" float: none; "title=" Index.png "alt=" Wkiol1qnvwoskcqiaadrwubmzmo149.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4B/06/wKioL1QnvwPwAo3UAADQorQ38N4284.jpg "style=" float: none; "title=" 2.png "alt=" Wkiol1qnvwpwao3uaadqorq38n4284.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4B/04/wKiom1QnvtqzJCZJAALh4ChCGTc159.jpg "style=" float: none; "title=" 3.png "alt=" Wkiom1qnvtqzjczjaalh4chcgtc159.jpg "/>
[Email protected]:~/ansible/tornado# Tree
.
├──templates
│├──index.html
│└──result.html
└──test.py
[Email protected]:~/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 (
#程序设置, dictionary form
Template_path=os.path.join (Os.path.dirname (__file__), "Templates"),
#设置模板文件路径
Static_path=os.path.join (Os.path.dirname (__file__), "Static"),
#设置静态文件路径, such as css\jpg\gif, etc.
# ui_modules={"book": Bookmodule},
#设置ui模块, you can use a dictionary to add multiple
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):
# define a function to determine the result
If Len (result[' Dark ']) >0:
# Dark Return is not empty indicates that the operation failed
Return result[' Dark '], ' failure! '
Else
Return result[' contacted ', ' success! '
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
<! DOCTYPE html>
<body>
<form method= "POST" action= "/result" >
<p>pattern<br><input type= "text" name= "pattern" ></p>
<p>module_name<br><input type= "text" name= "Module_name" ></p>
<p>module_args<br><input type= "text" name= "Module_args" ></p>
<input type= "Submit" >
</form>
</body>
# Cat Result.html
<! DOCTYPE html>
<body>
<p> message: {{message}} </p>
<p> Result: {{result}} </p>
</body>
Ansible + tornado + MongoDB