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脚本的更多相关文章

  1. ansible批量分发免密钥登陆python脚本

    最近看了看强大的号称自动化运维的三大利器之一的--ansible,ok,亲测之后,确实感觉,对于我们这种DBA工作者来说,确实很受益. 值得注意的是ansible要求被管理服务器python版本不低于 ...

  2. 开启SQL Server执行占用时间显示和逻辑读取次数显示

    两条命令 1:set statistics time on 这条命令会显示你编译这条语句和执行这条语句花多长时间 2.set statistics io on 这条命令会显示你逻辑读取了多少次数据库和 ...

  3. ansible执行带有环境变量的脚本不生效

    1背景 jenkins发布时,使用ansible执行远程主机上的启动tomcat脚本发现不生效,启动tomcat的脚本中有环境变量. ansible主机为:172.16.35.8 tomcat服务器为 ...

  4. Android上执行python脚本-QPython

    看书,发现android可以跑python. 尝试了一下. 首先需要在手机上安装python环境,通过安装apk实现,这个apk叫QPython,还有同类的比如SL4A. QPython的官网:htt ...

  5. Python与Hack之window下运行带参数的Python脚本,实现一个简单的端口扫描器

    1.前提是:windows已经配置好Python的环境变量: 2.进入cmd命令行模式: **输入python命令,检测是否环境配置好:显示这样说明配置环境变量没问题 **用cd命令进入Python脚 ...

  6. C#调用python脚本

    因项目需要,需要使用C#控制台程序执行python脚本,查询各种资料后可以成功调用了,记录一下,以备后面遗忘. 只尝试了两种调用方式,第一种只适用于python脚本中不包含第三方模块的情况,第二种针对 ...

  7. Linux使用crontab定时执行Python脚本清理日志

    Linux中,周期执行的任务一般由crond这个守护进程来处理.cron读取一个或多个配置文件,这些配置文件中包含了命令行及其调用时间.crond的配置文件称为"crontab", ...

  8. Jenkins自动执行python脚本输出测试报告

    前言 在用python做自动化测试时,我们写好代码,然后需要执行才能得到测试报告,这时我们可以通过 Jenkins 来进一步完成自动化工作. 借助Jenkins,我们可以结合 Git/SVN 自动拉取 ...

  9. 【原创】控制perl和python脚本执行过程中脚本文件是否关闭的方法

    引子 跟踪perl和python脚本对文件的访问,实际过程中,perl和python解析器在解析完脚本后,直接关闭了 脚本文件,在进程中查询不到是访问文件的脚本文件名称. shell.perl和pyt ...

随机推荐

  1. MySQL的热备percona-xtrabackup、innobackupex的安装方法

    http://blog.csdn.net/dbanote/article/details/13295727 http://blog.csdn.net/yangzhawen/article/detail ...

  2. 检测当前网段哪些IP是在线的

    [root@storage ~]# cat ping.ip #!/bin/bashfor ip in `seq 1 255`    do    {      ping -c 2 192.168.220 ...

  3. SpringMVC 部署项目静态资源文件访问问题

    问题:采用SpringMVC 部署项目后程序加载或用浏览器访问时出现类似的警告,2011-01-19 10:52:51,646 WARN [org.springframework.web.servle ...

  4. 【CodeForces 672B】Different is Good

    题 字符串所有子串要不同.求修改最少多少个字符. 因为只能是26个字母,显然大于26的不可能有答案,其它情况ans+=u[i]-1:u[i]是字母出现的次数. #include<cstdio&g ...

  5. 使用redis避免客户端频繁提交数据

    避免客户端频繁向服务器提交表单的解决方案 使用redis 在order的model中增加函数 ) { try{ $key = "lock_" . $key; $num = $thi ...

  6. bzoj2938: [Poi2000]病毒

    建AC自动机,把所有病毒的节点都删掉,dfs判有没有环,有环就找得到. #include <iostream> #include <cstdio> #include <c ...

  7. exit(0)、exit(1)、exit(-1)的区别

    exit(0) - 正常退出 exit(1) - 异常退出(除0外,其他值均为异常退出)

  8. 【BZOJ-4523】路由表 Trie树 + 乱搞

    4523: [Cqoi2016]路由表 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 155  Solved: 98[Submit][Status][ ...

  9. 【uoj128】 NOI2015—软件包管理器

    http://uoj.ac/problem/128 (题目链接) 题意 给出一棵树,每个节点代表一个软件包,维护卸载和安装操作.若要卸载节点x,那么必须卸载它的子树上的所有软件包:若要安装节点x必须安 ...

  10. bzoj2631: tree

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...