Last active
August 29, 2015 14:10
-
-
Save odyssey4me/8fc5760c3fe1bdb8fb9b to your computer and use it in GitHub Desktop.
Place into /usr/local/lib/python2.7/dist-packages/ansible/callback_plugins/ in order to see the duration of time for each task and play.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on the examples available at https://github.com/ansible/ansible/tree/devel/plugins/callbacks | |
# and the similar https://github.com/ginsys/ansible-plugins/blob/devel/callback_plugins/timestamp.py | |
# imports | |
import time | |
from ansible.callbacks import display | |
# define start time | |
t0 = tn = time_play_start = time.time() | |
def secondsToStr(t): | |
# http://bytes.com/topic/python/answers/635958-handy-short-cut-formatting-elapsed-time-floating-point-seconds | |
rediv = lambda ll,b : list(divmod(ll[0],b)) + ll[1:] | |
return "%d:%02d:%02d.%03d" % tuple(reduce(rediv,[[t*1000,], 1000,60,60])) | |
def filled(msg, fchar="*"): | |
if len(msg) == 0: | |
width = 79 | |
else: | |
msg = "%s " % msg | |
width = 79 - len(msg) | |
if width < 3: | |
width = 3 | |
filler = fchar * width | |
return "%s%s " % (msg, filler) | |
def timestamp(type="task"): | |
global tn, time_play_start | |
time_elapsed = secondsToStr(time.time() - tn) | |
time_play_elapsed = secondsToStr(time.time() - time_play_start) | |
time_total_elapsed = secondsToStr(time.time() - t0) | |
display( filled( 'Previous Task Duration: %s Overall Duration: %s' % (time_elapsed, time_total_elapsed ))) | |
if type == "play": | |
display( filled( 'Previous Play Duration: %s' % (time_play_elapsed ))) | |
tn = time.time() | |
class CallbackModule(object): | |
def playbook_on_setup(self): | |
timestamp() | |
pass | |
def playbook_on_play_start(self, pattern): | |
global time_play_start | |
timestamp(type="play") | |
time_play_start = time.time() | |
pass | |
def playbook_on_stats(self, stats): | |
timestamp(type="play") | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment