1.提供方代码如下; # -*- coding: utf-8 -*- import json import subprocess import sys import requests reload(sys) sys.setdefaultencoding('utf-8') """ 查询cmdb 实例,应用,接口,读取应用,相关联的实例,和实例的ip 地址,开启多线程方式执行启动服务命令; """ class Power_reqair(object): def CHJ_ansible_operation(self, app_name, app_ip): self.app_name = app_name self.app_ap = app_ip cmd = """ansible -i {}, all -m shell -a 'su - work -c "/chj/app/{}/bin/console status"' -b -f 10""".format( app_ip.strip(), app_name.strip()) p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) result = p.communicate() with open('/tmp/Docker_test_new.txt', 'ab+') as f: f.write(result[0]) def get_ins(self, id, run_type): list_ip = [] url = 'http://xxx.xxx.xxx.xx/instance/select' data = { 'app_id': id } src_data = requests.post(url, json=data) json_data = json.loads(src_data.text) for i in json_data["data"]: if i["run_type"] == run_type: list_ip.append(i["ip"]) return list_ip def ops_env(self, env_type, result): url = 'http://xxx.xxx.xxx.xx/application/select' src_data = requests.post(url) json_data = json.loads(src_data.text) for i in json_data["data"]: if i['app_type'] == "java" or i['app_type'] == "business": id = i['id'] app_name = i['name'] try: res = self.get_ins(id, env_type) if res: for app_ip in res: result.setdefault(app_name,app_ip) t = threading.Thread(target=self.CHJ_ansible_operation(app_name,app_ip)) t.start() except Exception, e: print(e) print "none" 2.程序调用方代码; # -*- coding: utf-8 -*- import os import sys import time from flask import Flask, request, render_template, jsonify,Response from flask_cors import CORS from new_power_repaoir import Power_reqair @app.route('/service_pc', methods=['GET']) def service_pc(): power_reqair_instance = CHJ_Power_reqair() result = {} abc = power_reqair_instance.ops_env("test", result) return Response(json.dumps(result), mimetype='application/json') 3.页面效果如下;
{
"hmi-msg-service": "192.168.47.40",
"msc-hmi": "192.168.47.40",
"chj-service-tp-api": "192.168.58.52",
"xx-service-ssp-vcs-app": "192.168.58.33",
"aisp-swagger-group": "192.168.58.47",
"xx-service-ddes-app": "192.168.58.32",
"xx-service-dkms-app": "192.168.58.21",
"xx-key-admin-web": "192.168.58.72",
"-sso-service": "192.168.47.55",
"xx-service-ota-app": "192.168.47.177",
"xx-service-as-mock-web": "192.168.58.82",
"api-gateway": "192.168.58.34",
"ampmind-data-api-server": "192.168.58.85",
}