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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#*******一、flask_ansible.py文件 #!/usr/bin/env python #_*_ coding:utf-8 _*_ import json
from flask import Flask,request
from ansible_api_job import Ansibles
app = Flask(__name__)
@app .route( '/ansible/command' , methods = [ 'GET' , 'POST' ])
def command():
if request.method = = 'POST' :
jsondata = request.get_data()
dictdata = json.loads(jsondata)
ansible = Ansibles(dictdata[ 'host' ]) #实例化Ansible对象
res = ansible.shell(dictdata[ 'command' ])
return res
else :
return '<h1>Access error</h1>'
@app .route( '/ansible/copyfile' , methods = [ 'GET' , 'POST' ])
def copyfile():
if request.method = = 'POST' :
jsondata = request.get_data()
dictdata = json.loads(jsondata)
ansible = Ansibles(dictdata[ 'targethost' ]) #实例化Ansible对象
res = ansible.copyfile( str (dictdata[ 'srcfile' ]), str (dictdata[ 'dectdir' ]))
return res
else :
return '<h1>Access error</h1>'
@app .route( '/ansible/pullfile' ,methods = [ 'GET' , 'POST' ])
def pullfile():
if request.method = = 'POST' :
jsondata = request.get_data()
dictdata = json.loads(jsondata)
ansible = Ansibles(dictdata[ 'srchost' ])
res = ansible.pull(dictdata[ 'pullsrcfile' ], dictdata[ 'savelocaldir' ])
return res
else :
return '<h1>Access error</h1>'
if __name__ = = '__main__' :
app.run(debug = True ,host = '0.0.0.0' )
#************二、ansible_api_job.py文件 #!/user/bin/eng python # -*- coding=utf-8 -*- import ansible.runner
class Ansibles( object ):
def __init__( self ,hostname):
self .hostlist = "host.txt"
self .user = "root"
self .passwd = "1qaz#EDC"
self .pattern = "vmserver"
self .hostname = hostname
hostlist = self .hostname.split( ',' )
hostfile = open ( 'host.txt' , 'w+' )
hostfile.writelines( '[vmserver]' + '\n' )
for line in hostlist:
hostfile.writelines(line + '\n' )
hostfile.close()
def shell( self ,command):
results = ansible.runner.Runner(
host_list = self .hostlist,
remote_user = self .user,
remote_pass = self .passwd,
module_name = 'shell' ,
module_args = command,
pattern = self .pattern,
forks = 10
).run()
for (hostname,result) in results[ 'contacted' ].items():
if result[ 'stdout' ] = = "":
return "HOST:%s, ERROR>>(%s)" % (hostname, result[ 'stderr' ])
else :
return "HOST:%s, RESULTS>>(%s)" % (hostname, result[ 'stdout' ])
def copyfile( self ,srcfile,dectdir):
results = ansible.runner.Runner(
host_list = self .hostlist,
remote_user = self .user,
remote_pass = self .passwd,
module_name = 'copy' ,
module_args = 'src=%s dest=%s' % (srcfile,dectdir),
pattern = self .pattern,
forks = 10
).run()
for (hostname,result) in results[ 'contacted' ].items():
if 'failed' in result:
return "HOST:%s, ERROR>>(%s)" % (hostname,result[ 'msg' ])
else :
return "HOST:%s, copy ok" % (hostname)
# 文件拉取到本地
def pull( self ,pullsrcfile,savelocaldir):
pullfileres = ansible.runner.Runner(
host_list = self .hostlist,
remote_user = self .user,
remote_pass = self .passwd,
module_name = 'fetch' ,
module_args = 'src=%s dest=%s flat=yes' % (pullsrcfile, savelocaldir),
pattern = self .pattern,
forks = 10
).run()
for (hostname,result) in pullfileres[ 'contacted' ].items():
if 'msg' in result:
return "HOST:%s ERROR>>(%s)" % (hostname,result[ 'msg' ])
else :
return 'HOST:%s pull ok' % (hostname)
if __name__ = = "__main__" :
pass
#*********三、urlib2调用http接口 #_*_coding:utf-8_*_ import urllib2
import json
#调用执行命令HTTP接口 def ansible_http_post_command(host,command):
url = 'http://192.168.89.8:5000/ansible/command'
data = { 'host' : host, 'command' : command}
headers = { 'Content-Type' : 'application/json' }
req = urllib2.Request(url = url, headers = headers, data = json.dumps(data))
response = urllib2.urlopen(req)
return response.read()
#res = ansible_http_post_command('vm5',"uname -a") #print res #调用传送文件到目标主机的HTTP接口 def ansible_http_post_copyfile(srcfile,targethost,dectdir):
url = 'http://192.168.89.8:5000/ansible/copyfile'
data = { 'srcfile' : srcfile, 'targethost' : targethost, 'dectdir' :dectdir}
headers = { 'Content-Type' : 'application/json' }
req = urllib2.Request(url = url, headers = headers, data = json.dumps(data))
response = urllib2.urlopen(req)
return response.read()
res = ansible_http_post_copyfile( '/tmp/12345.txt' , 'vm4' , '/root/' )
print res
#调用从目标主机中拉取文件到本地的HTTP接口 def ansible_http_post_pullsend(srchost,pullsrcfile,savelocaldir):
url = 'http://192.168.89.8:5000/ansible/pullfile'
data = { 'srchost' :srchost, 'pullsrcfile' :pullsrcfile, 'savelocaldir' :savelocaldir}
headers = { 'Content-Type' : 'application/json' }
req = urllib2.Request(url = url, headers = headers, data = json.dumps(data))
response = urllib2.urlopen(req)
return response.read()
#res = ansible_http_post_pullsend('vm2','/tmp/12345.txt','/tmp/') #print res |
本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/1879968