ansible执行playbook时间显示的python脚本
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
import datetimeimport osimport timefrom ansible.plugins.callback import CallbackBaseclass 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的方法:
|
1
2
3
4
5
6
7
|
cd /etc/ansiblemkdir callback_pluginscd callback_pluginswget https://raw.githubusercontent.com/jlafon/ansible-profile/master/callback_plugins/profile_tasks.py###友提ansible2.0以上的版本需要在ansible.cfg中加入callback_whitelist = profile_tasks |
运行结果如下:


浙公网安备 33010602011771号