bei 发表于 2018-7-29 13:11:53

ansible安装以及配置优化和实现动态inventory-Linux之旅

$ more callback_plugins/profile_tasks.py  
import datetime
  
import os
  
import time
  
from ansible.plugins.callback import CallbackBase
  

  

  
class CallbackModule(CallbackBase):
  
    """
  
    A plugin for timing tasks
  
    """
  
    def __init__(self):
  
      super(CallbackModule, self).__init__()
  
      self.stats = {}
  
      self.current = None
  

  
    def playbook_on_task_start(self, name, is_conditional):
  
      """
  
      Logs the start of each task
  
      """
  

  
      if os.getenv("ANSIBLE_PROFILE_DISABLE") is not None:
  
            return
  

  
      if self.current is not None:
  
            # Record the running time of the last executed task
  
            self.stats = time.time() - self.stats
  

  
      # Record the start time of the current task
  
      self.current = name
  
      self.stats = time.time()
  

  
    def playbook_on_stats(self, stats):
  
      """
  
      Prints the timings
  
      """
  

  
      if os.getenv("ANSIBLE_PROFILE_DISABLE") is not None:
  
            return
  

  
      # Record the timing of the very last task
  
      if self.current is not None:
  
            self.stats = time.time() - self.stats
  

  
      # Sort the tasks by their running time
  
      results = sorted(
  
            self.stats.items(),
  
            key=lambda value: value,
  
            reverse=True,
  
      )
  

  
      # Just keep the top 10
  
      results = results[:10]
  

  
      # Print the timings
  
      for name, elapsed in results:
  
            print(
  
                "{0:-<70}{1:->9}".format(
  
                  '{0} '.format(name),
  
                  ' {0:.02f}s'.format(elapsed),
  
                )
  
            )
  

  
      total_seconds = sum( for x in self.stats.items()])
  
      print("\nPlaybook finished: {0}, {1} total tasks.{2} elapsed. \n".format(
  
                time.asctime(),
  
                len(self.stats.items()),
  
                datetime.timedelta(seconds=(int(total_seconds)))
  
                )
  
          )
页: [1]
查看完整版本: ansible安装以及配置优化和实现动态inventory-Linux之旅