ansible执行playbook时间显示的python脚本
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[self.current] = time.time() - self.stats[self.current] # Record the start time of the current task
self.current = name
self.stats[self.current] = 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[self.current] = time.time() - self.stats[self.current] # Sort the tasks by their running time
results = sorted(
self.stats.items(),
key=lambda value: value[1],
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([x[1] 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)))
)
)
下面是把这个插件集成到ansible的方法:
cd /etc/ansible
mkdir callback_plugins
cd callback_plugins
wget https://raw.githubusercontent.com/jlafon/ansible-profile/master/callback_plugins/profile_tasks.py
###友提
ansible2.0以上的版本需要在ansible.cfg中加入
callback_whitelist = profile_tasks
运行结果如下:

ansible执行playbook时间显示的python脚本的更多相关文章
- ansible批量分发免密钥登陆python脚本
最近看了看强大的号称自动化运维的三大利器之一的--ansible,ok,亲测之后,确实感觉,对于我们这种DBA工作者来说,确实很受益. 值得注意的是ansible要求被管理服务器python版本不低于 ...
- 开启SQL Server执行占用时间显示和逻辑读取次数显示
两条命令 1:set statistics time on 这条命令会显示你编译这条语句和执行这条语句花多长时间 2.set statistics io on 这条命令会显示你逻辑读取了多少次数据库和 ...
- ansible执行带有环境变量的脚本不生效
1背景 jenkins发布时,使用ansible执行远程主机上的启动tomcat脚本发现不生效,启动tomcat的脚本中有环境变量. ansible主机为:172.16.35.8 tomcat服务器为 ...
- Android上执行python脚本-QPython
看书,发现android可以跑python. 尝试了一下. 首先需要在手机上安装python环境,通过安装apk实现,这个apk叫QPython,还有同类的比如SL4A. QPython的官网:htt ...
- Python与Hack之window下运行带参数的Python脚本,实现一个简单的端口扫描器
1.前提是:windows已经配置好Python的环境变量: 2.进入cmd命令行模式: **输入python命令,检测是否环境配置好:显示这样说明配置环境变量没问题 **用cd命令进入Python脚 ...
- C#调用python脚本
因项目需要,需要使用C#控制台程序执行python脚本,查询各种资料后可以成功调用了,记录一下,以备后面遗忘. 只尝试了两种调用方式,第一种只适用于python脚本中不包含第三方模块的情况,第二种针对 ...
- Linux使用crontab定时执行Python脚本清理日志
Linux中,周期执行的任务一般由crond这个守护进程来处理.cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.crond的配置文件称为"crontab", ...
- Jenkins自动执行python脚本输出测试报告
前言 在用python做自动化测试时,我们写好代码,然后需要执行才能得到测试报告,这时我们可以通过 Jenkins 来进一步完成自动化工作. 借助Jenkins,我们可以结合 Git/SVN 自动拉取 ...
- 【原创】控制perl和python脚本执行过程中脚本文件是否关闭的方法
引子 跟踪perl和python脚本对文件的访问,实际过程中,perl和python解析器在解析完脚本后,直接关闭了 脚本文件,在进程中查询不到是访问文件的脚本文件名称. shell.perl和pyt ...
随机推荐
- ELK 的好文章连接
http://www.wklken.me/posts/2016/05/24/elk-mysql-slolog.html 处理mysql慢查询日志 http://www.wklken.me/post ...
- 【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...
- eclipse-插件安装的三种方式
(前两种安装方式以多国语言包的安装为例) 1. 普通安装:用直接解压的安装方式来实现 解压插件到某个文件夹 将下载的插件文件解压到 Eclipse 的安装目录下 如插件文件为多国语言包: NLpac ...
- C# 定时器运用
在晚上12点执行任务 using System;using System.Collections.Generic;using System.ComponentModel;using System.Da ...
- finally关键字
final:禁止多态开关~修饰变量:变量不能被改变修饰类:类不能被继承修饰方法:方法不能被重写 finally:用在异常处理的最后一个语句块无论是否产生异常都要被执行~~~ Java代码 public ...
- jsp动态include和静态Include
动态 INCLUDE 用 jsp:include 动作实现 <jsp:include page="included.jsp" flush="true" / ...
- Oracle 11g新特性
文章转自网络 Oracle 11g于2007年7月11日美国东部时间11时(北京时间11日22时)正式发布,11g是甲骨文公司30年来发布的最重要的数据库版本,根据用户的需求实现了信息生命周期管理(I ...
- BZOJ2005 莫比乌斯反演
题意:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 实际上把这些被挡住的点的坐标和能量值列举出来可以发现有个公式: “对于坐标系第一象限任意 ...
- CodeForces 51F Caterpillar
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- MySQL备份方式简介
MySQL备份的方式主要分为两种: 文本格式备份: 命令:mysqldump 转储文件:dump file 主要内容:数据库结构及数据(create talbe /insert) 二进制备份:这类备份 ...