Get the App
SLTechnology News&Howtos  ›  Database  › 

Ansible + tornado + MongoDB

Shulou Source: shulou.com Published: 2022-06-01 11:39:02 09月26日 Update

Http://blog.csdn.net/smallfish2983/article/details/38078019

According to the above guy wrote, beginners, do not spit blood, the basic function has been realized.

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):

# initialize something

Handlers = [

# url matching

(r "/", MainHandler)

(r "/ index", MainHandler)

(r "/ result", Module_actionHandler)

]

Settings = dict (

# Program settings, dictionary form

Template_path=os.path.join (os.path.dirname (_ _ file__), "templates")

# set the template file path

Static_path=os.path.join (os.path.dirname (_ _ file__), "static")

# set static file path, such as css\ jpg\ gif, etc.

# ui_modules= {"Book": BookModule}

# set ui module, you can add multiple modules with a dictionary

Debug=True

)

Tornado.web.Application.__init__ (self, handlers, * * settings)

# incoming settings configuration

Class MainHandler (tornado.web.RequestHandler):

# Home page function method

Def get (self):

# set get method function

Self.render (

"index.html"

)

Class Module_actionHandler (tornado.web.RequestHandler):

# define the method of module operation function

Def post (self, * args, * * kwargs):

Pattern = self.get_arguments ('pattern') [0]

# get hostname

Module_name = self.get_arguments ('module_name') [0]

# get the module name

Module_args = self.get_arguments ('module_args') [0]

# get parameters

Import ansible.runner

Runner = ansible.runner.Runner (

# run the script according to ansible's 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 judge the result

If len (result ['dark']) > 0:

# if the dark return is not empty, the operation failed

Return result ['dark'],' failed!'

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

Ansible

Enter arg bellow:

Pattern

Module_name

Module_args

# cat result.html

Ansible result

Ansible result

Message: `message`

Result: `result`

Attachment: http://down.51cto.com/data/2364907

Tags: Functions methods modules dictionaries files paths success things hosts home pages functions parameters buddies multiple forms templates instructions programs results scripts Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi Docker Microsoft Shulou Technology