新聞中心
http://blog.csdn.net/smallfish2983/article/details/38078019
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、相城網(wǎng)絡(luò)推廣、小程序設(shè)計(jì)、相城網(wǎng)絡(luò)營(yíng)銷、相城企業(yè)策劃、相城品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供相城建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
照著上面那哥們寫的,初學(xué),不要吐血,基本功能實(shí)現(xiàn)了。
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(
#程序設(shè)置,字典形式
template_path=os.path.join(os.path.dirname(__file__), "templates"),
#設(shè)置模板文件路徑
static_path=os.path.join(os.path.dirname(__file__), "static"),
#設(shè)置靜態(tài)文件路徑,如css\jpg\gif等
# ui_modules={"Book": BookModule},
#設(shè)置ui模塊,可以用字典添加多個(gè)
debug=True,
)
tornado.web.Application.__init__(self, handlers, **settings)
#傳入設(shè)置配置
class MainHandler(tornado.web.RequestHandler):
#主頁(yè)函數(shù)方法
def get(self):
#設(shè)置get方法函數(shù)
self.render(
"index.html",
)
class Module_actionHandler(tornado.web.RequestHandler):
#定義模塊操作函數(shù)方法
def post(self, *args, **kwargs):
pattern = self.get_arguments('pattern')[0]
#獲取主機(jī)名
module_name = self.get_arguments('module_name')[0]
#獲取模塊名
module_args = self.get_arguments('module_args')[0]
#獲取參數(shù)
import ansible.runner
runner = ansible.runner.Runner(
#根據(jù)ansible的api來運(yùn)行腳本
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):
# 定義一個(gè)判斷結(jié)果的函數(shù)
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
Enter arg bellow:
pattern
module_name
module_args
# cat result.html
ansible result
message: `message`
result: `result`
網(wǎng)頁(yè)名稱:ansible+tornado+MongoDB
瀏覽路徑:http://fisionsoft.com.cn/article/ijhdgj.html