设为首页 收藏本站
查看: 736|回复: 0

[经验分享] Ansible 多机文件分发、执行脚本并单机合并实验结果(Check point, 多线程异步执行,主机状态检测等)

[复制链接]

尚未签到

发表于 2018-1-3 11:46:03 | 显示全部楼层 |阅读模式
#!/usr/bin/env python  
# -*- coding:utf-8 -*-
  

  
import sys, os
  
import ConfigParser
  
import ansible.runner
  
import time
  

  
inventory_file = "../host.ini"
  
database_path = "../database"
  
config_file = "../config.ini"
  


  
>  
     def __init__(self):
  
         cf = ConfigParser.ConfigParser()
  
     cf.read(config_file)
  
     self.master_load = int(cf.get("ansible", "master_load"))
  
         self.slave_load = int(cf.get("ansible", "slave_load"))
  

  
     self.code_home_path = cf.get("ansible", "code_home")
  
     self.ansibleResult = self.code_home_path + "/ansibleResult"
  
     self.database_path = self.code_home_path + "/database"
  

  

  
     self.avail_master = []
  
     self.avail_slave = []
  
     self.loadBalancing = {}
  

  
     def extractHost(self,config_file_path):
  
         cf = ConfigParser.ConfigParser(allow_no_value = True)
  
         cf.read(config_file_path)
  
         s = cf.sections()
  
         #print 'section:', s
  
         ms = cf.options("master")
  
         #print 'options:', ms
  
     for x in ms:
  
         runner = ansible.runner.Runner(
  
         host_list=inventory_file,
  
             module_name='ping',
  
         module_args='',
  
         pattern=x
  
         )
  
         res = runner.run()
  
         #print res
  
         if res['dark'] and res['dark'][x] and res['dark'][x]['failed']:
  
         print "Host Error:", x, "Unreachable"
  
             continue
  
         self.avail_master.append(x)
  
     self.avail_master = list(set(self.avail_master))
  
     print self.avail_master
  
     ss = cf.options("slave")
  
     for x in ss:
  
         runner = ansible.runner.Runner(
  
             host_list=inventory_file,
  
             module_name='ping',
  
         module_args='',
  
         pattern=x
  
         )
  
         res = runner.run()
  
         #print res
  
         if res['dark'] and res['dark'][x] and res['dark'][x]['failed']:
  
             continue
  
         self.avail_slave.append(x)
  
     self.avail_slave = list(set(self.avail_slave))
  

  
     # make true the host is unique
  
     for host in self.avail_master:
  
         if host in self.avail_slave:
  
             self.avail_slave.remove(host)
  
     print self.avail_slave
  

  
     def calculateLoadBalancing(self):
  
         load_count = int(os.popen("ls %s | grep ^mygraph | wc -l" % database_path).read())
  
     self.load_count = load_count
  
     current_load_count = load_count
  
     print load_count
  
     print self.master_load, self.slave_load
  
     master_all_count = len(self.avail_master)
  
     slave_all_count = len(self.avail_slave)
  

  
     for m in self.avail_master:
  
         temp_count = min(current_load_count, int(load_count * (self.master_load / float(self.master_load * master_all_count + self.slave_load * slave_all_count))))
  
         print temp_count
  
         if temp_count > 0:
  
                 self.loadBalancing[m] = temp_count
  
         current_load_count -= temp_count
  
         for s in self.avail_slave:
  
         temp_count = min(current_load_count, int(load_count * (self.slave_load / float(self.master_load * master_all_count + self.slave_load * slave_all_count))))
  
         if temp_count > 0:
  
             self.loadBalancing = temp_count
  
         current_load_count -= temp_count
  

  
     if current_load_count > 0 and master_all_count > 0:
  
         self.loadBalancing[self.avail_master[0]] += current_load_count
  
         current_load_count = 0
  
     if current_load_count > 0 and slave_all_count > 0:
  
         self.loadBalancing[self.avail_slave[0]] += current_load_count
  
         current_load_count = 0
  
     print self.loadBalancing
  

  
     def fileTransfer(self):
  
         print self.code_home_path
  
     print self.ansibleResult
  
     if self.ansibleResult == "":
  
         print "ERROR: the home of the result about ansible is null!"
  
         sys.exit(1)
  
     current_database_count = 0
  
     #clean the directory of the result
  
     for host in self.loadBalancing:
  
         print host
  
         runner = ansible.runner.Runner(
  
             host_list=inventory_file,
  
         module_name='file',
  
         module_args='path=%s state=absent' % self.ansibleResult,
  
         pattern=host
  
         )
  
         res = runner.run()
  
         #print res
  
         runner = ansible.runner.Runner(
  
             host_list=inventory_file,
  
         module_name='file',
  
         module_args='path=%s state=directory mode=0750' % self.ansibleResult,
  
         pattern=host
  
         )
  
         res = runner.run()
  
         #print res
  

  
         # transfer the database
  
             this_host_load_count = self.loadBalancing[host]
  
         for i in range(this_host_load_count) :
  
             runner = ansible.runner.Runner(
  
             host_list=inventory_file,
  
             module_name='copy',
  
             module_args='src=%s/mygraph%s dest=%s mode=0750' % (self.database_path, current_database_count + i, self.ansibleResult),
  
             pattern=host
  
         )
  
         res = runner.run()
  
         print res
  
         #print current_database_count
  
         #print this_host_load_count
  
         current_database_count += this_host_load_count
  

  
         # transfer the code
  
         file_code_list = ["community.jar", "config.ini", "runAll.sh","lib"]
  
         for f in file_code_list:
  
             runner = ansible.runner.Runner(
  
             host_list=inventory_file,
  
             module_name='copy',
  
             module_args='src=%s/%s dest=%s mode=770' % (self.code_home_path, f, self.code_home_path),
  
             pattern=host
  
         )
  
         res = runner.run()
  
         print res
  

  
         # run the code distributed
  
         runner = ansible.runner.Runner(
  
             host_list=inventory_file,
  
         module_name="shell",
  
         module_args='chdir=%s nohup bash runAll.sh %s > bash.log &' % (self.code_home_path, self.ansibleResult),
  
         pattern=host
  
         )
  

  
         res = runner.run()
  
         print res
  
         print "The Host named", host, "is running!"
  

  
     def informationTransfer(self, transfer_module_name, transfer_module_args, host):
  
         print transfer_module_args
  
     print transfer_module_name
  
     runner = ansible.runner.Runner(
  
         host_list=inventory_file,
  
         module_name=transfer_module_name,
  
         module_args=transfer_module_args,
  
         pattern=host
  
     )
  
     res = runner.run()
  
     print res
  

  
     def checkPoint(self):
  
         os.system("rm -rf %s/output" % self.database_path)
  
         os.system("mkdir -p %s/output" % self.database_path)
  
         print "Begining CheckPoint"
  
     host_keys = self.loadBalancing.keys()
  
     #print host_keys
  
     while len(host_keys) > 0:
  
         for host in host_keys:
  
         runner = ansible.runner.Runner(
  
             host_list=inventory_file,
  
             module_name='command',
  
             module_args='removes=%s/success_log pwd' % self.ansibleResult,
  
             pattern=host
  
         )
  
         res = runner.run()
  
         print res
  
         if res['contacted'] and res['contacted'][host] and "skipped" in res['contacted'][host]['stdout']:
  
             continue
  
         else:
  
             print "Host", host, "finished his tasks!"
  
             host_keys.remove(host)
  
             self.informationTransfer("synchronize", "src=%s/output dest=%s mode=pull" % (self.ansibleResult, self.database_path), host)
  
             print host_keys
  
             print "to sleep"
  
             time.sleep(5)
  

  

  
if __name__ == "__main__":
  
     ob = DistributedByAnsible()
  
     ob.extractHost(inventory_file)
  
     #ob.calculateLoadBalancing();
  
     #ob.fileTransfer();
  
     #ob.checkPoint();
  
     print "Done"
  

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-431124-1-1.html 上篇帖子: Ansible系列(四):playbook应用和roles自动化批量安装示例 下篇帖子: ansible批量修改linux服务器密码的playbook
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表