forked from strongit/flask-ansible-api-celery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask-api.py
53 lines (50 loc) · 1.88 KB
/
flask-api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
#coding:utf-8
from ansible.inventory import Inventory
from ansible.playbook import PlayBook
from ansible import callbacks
import ansible.runner
from flask import Flask,request,jsonify,render_template,abort
import commands,json
app = Flask(__name__)
hostfile='./hosts'
'''
http://127.0.0.1:5000/API/Ansible/playbook?ip=2.2.2.2&palybook=test
'''
@app.route('/API/Ansible/playbook')
def Playbook():
vars={}
inventory = Inventory(hostfile)
stats = callbacks.AggregateStats()
playbook_cb =callbacks.PlaybookCallbacks()
runner_cb =callbacks.PlaybookRunnerCallbacks(stats)
hosts=request.args.get('ip')
task=request.args.get('playbook')
vars['hosts'] = hosts
play=task + '.yml'
results = PlayBook(playbook=play,callbacks=playbook_cb,runner_callbacks=runner_cb,stats=stats,inventory=inventory,extra_vars=vars)
res = results.run()
return json.dumps(res,indent=4)
'''
curl -H "Content-Type: application/json" -X POST -d '{"ip":"1.1.1.1","module":"shell","args":"ls -l"}' http://127.0.0.1:5000/API/Ansible/runner
'''
@app.route('/API/Ansible/runner',methods=['POST'])
def Runner():
print request.json
if not request.json or not 'ip' in request.json or not 'module' in request.json or not 'args' in request.json:
abort(400)
hosts=request.json['ip']
module = request.json['module']
args=request.json['args']
runner = ansible.runner.Runner(module_name=module,module_args=args,pattern=hosts,forks=10,host_list=hostfile)
tasks=runner.run()
cpis={}
cpis1={}
for (hostname, result) in tasks['contacted'].items():
if not 'failed' in result:
cpis[hostname] = result['stdout']
for (hostname, result) in tasks['dark'].items():
cpis1[hostname] = result['msg']
return render_template('cpis.html',cpis=cpis,cpis1=cpis1)
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0')