標籤:ansible tornado mongodb
http://blog.csdn.net/smallfish1983/article/details/38078019
照著上面那哥們寫的,初學,不要吐血,準系統實現了。
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(
#程式設定,字典形式
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
<!DOCTYPE html>
<html>
<head><title>ansible </title></head>
<body>
<h1>Enter arg bellow:</h1>
<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>
</html>
# cat result.html
<!DOCTYPE html>
<html>
<head><title>ansible result</title></head>
<body>
<h1>ansible result</h1>
<p> message: {{message}} </p>
<p> result: {{result}} </p>
</body>
</html>
ansible + tornado + MongoDB