#*******一、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 '<h2>Access error</h2>' @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 '<h2>Access error</h2>' @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 '<h2>Access error</h2>' 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調(diào)用http接口 #_*_coding:utf-8_*_ import urllib2 import json #調(diào)用執(zhí)行命令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 #調(diào)用傳送文件到目標(biāo)主機的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 #調(diào)用從目標(biāo)主機中拉取文件到本地的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
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、成都做網(wǎng)站、鞏義網(wǎng)絡(luò)推廣、成都小程序開發(fā)、鞏義網(wǎng)絡(luò)營銷、鞏義企業(yè)策劃、鞏義品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供鞏義建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:muchs.cn
網(wǎng)頁題目:通過flask框架寫成http接口調(diào)用Ansible-創(chuàng)新互聯(lián)
URL鏈接:http://muchs.cn/article6/degsig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站策劃、網(wǎng)站設(shè)計公司、企業(yè)建站、ChatGPT、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容