Ansible集成到Flask网站中
来自CloudWiki
准备工作
下面代码中用的test_ansible2 是从这个文档中定义的
实训步骤
代码
main.py
from flask import Flask, jsonify, abort, request from test_ansible2 import do_ansible app = Flask(__name__) #远程关机功能 @app.route('/ansible/api/v1/machine_close/all/<string:host>', methods=['GET']) def shutdown_machine(host): host_list=[host] tasks_close=[ dict(action=dict(module='shell', args='shutdown -h now'), register='shell_out'), ] #do_ansible(host_list,tasks_close) return jsonify({'status': host+"shutdown order is sent"}) abort(404) #远程重启功能 @app.route('/ansible/api/v1/machine_restart/all/<string:host>', methods=['GET']) def restart_machine(host): host_list=[host] tasks=[ dict(action=dict(module='shell', args='reboot'), register='shell_out'), ] #do_ansible(host_list,tasks_close) return jsonify({'status': host+"restart order is sent"}) abort(404) if __name__ == '__main__': app.run(host='0.0.0.0',debug=True)
输出
python3 main.py