Skip to content

Commit 1994b2c

Browse files
Merge pull request #626 from pablomh/datetime_format
plugins/callback/profile_tasks.py: Add option to provide a different date/time format SUMMARY The new datetime_format key will offer the possibility of providing a different date/time format than the default one ('%A %d %B %Y %H:%M:%S %z'). The iso8601 value can be used as an '%Y-%m-%dT%H:%M:%S.%f' alias (format of the ISO 8601 date/time standard). The code has changed from using the time API to the datetime one in order to support sub-second precision (needed by the ISO 8601 format, for example). Fixes: #279 ISSUE TYPE Feature Pull Request COMPONENT NAME plugins/callback/profile_tasks.py ADDITIONAL INFORMATION Output with no key which keeps current behavior: TASK [Import subscription manifest] ******************************************************************************************************************************************************************************************************************************************************************* Thursday 10 April 2025 00:52:11 +0200 (0:00:17.416) 0:00:17.453 ******** changed: [localhost] Output with datetime_format = 'iso8601': TASK [Import subscription manifest] ******************************************************************************************************************************************************************************************************************************************************************* 2025-04-10T00:55:19.967718 (0:00:15.664) 0:00:15.691 ******************** changed: [localhost] Output with datetime_format = '%Y-%m-%dT%H:%M:%S.%f%z' (ISO 8601 with UTC offset information): TASK [Import subscription manifest] ******************************************************************************************************************************************************************************************************************************************************************* 2025-04-10T00:57:49.290347+0200 (0:00:16.265) 0:00:16.293 *************** changed: [localhost] Reviewed-by: Hideki Saito <[email protected]>
2 parents 96ec209 + 2f224e6 commit 1994b2c

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
minor_changes:
3+
- profile_tasks - Add option to provide a different date/time format (https://github.com/ansible-collections/ansible.posix/issues/279).

plugins/callback/profile_tasks.py

+34-14
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@
5252
- section: callback_profile_tasks
5353
key: summary_only
5454
version_added: 1.5.0
55+
datetime_format:
56+
description:
57+
- Datetime format, as expected by the C(strftime) and C(strptime) methods.
58+
An C(iso8601) alias will be translated to C('%Y-%m-%dT%H:%M:%S.%f') if that datetime standard wants to be used.
59+
default: '%A %d %B %Y %H:%M:%S %z'
60+
env:
61+
- name: PROFILE_TASKS_DATETIME_FORMAT
62+
ini:
63+
- section: callback_profile_tasks
64+
key: datetime_format
65+
version_added: 3.0.0
5566
'''
5667

5768
EXAMPLES = '''
@@ -72,14 +83,15 @@
7283
'''
7384

7485
import collections
75-
import time
86+
87+
from datetime import datetime
7688

7789
from ansible.module_utils.six.moves import reduce
7890
from ansible.plugins.callback import CallbackBase
7991

8092

8193
# define start time
82-
t0 = tn = time.time()
94+
dt0 = dtn = datetime.now().astimezone()
8395

8496

8597
def secondsToStr(t):
@@ -104,17 +116,18 @@ def filled(msg, fchar="*"):
104116

105117
def timestamp(self):
106118
if self.current is not None:
107-
elapsed = time.time() - self.stats[self.current]['started']
119+
elapsed = (datetime.now().astimezone() - self.stats[self.current]['started']).total_seconds()
108120
self.stats[self.current]['elapsed'] += elapsed
109121

110122

111-
def tasktime():
112-
global tn
113-
time_current = time.strftime('%A %d %B %Y %H:%M:%S %z')
114-
time_elapsed = secondsToStr(time.time() - tn)
115-
time_total_elapsed = secondsToStr(time.time() - t0)
116-
tn = time.time()
117-
return filled('%s (%s)%s%s' % (time_current, time_elapsed, ' ' * 7, time_total_elapsed))
123+
def tasktime(self):
124+
global dtn
125+
cdtn = datetime.now().astimezone()
126+
datetime_current = cdtn.strftime(self.datetime_format)
127+
time_elapsed = secondsToStr((cdtn - dtn).total_seconds())
128+
time_total_elapsed = secondsToStr((cdtn - dt0).total_seconds())
129+
dtn = cdtn
130+
return filled('%s (%s)%s%s' % (datetime_current, time_elapsed, ' ' * 7, time_total_elapsed))
118131

119132

120133
class CallbackModule(CallbackBase):
@@ -134,6 +147,7 @@ def __init__(self):
134147
self.sort_order = None
135148
self.summary_only = None
136149
self.task_output_limit = None
150+
self.datetime_format = None
137151

138152
super(CallbackModule, self).__init__()
139153

@@ -159,9 +173,14 @@ def set_options(self, task_keys=None, var_options=None, direct=None):
159173
else:
160174
self.task_output_limit = int(self.task_output_limit)
161175

176+
self.datetime_format = self.get_option('datetime_format')
177+
if self.datetime_format is not None:
178+
if self.datetime_format == 'iso8601':
179+
self.datetime_format = '%Y-%m-%dT%H:%M:%S.%f'
180+
162181
def _display_tasktime(self):
163182
if not self.summary_only:
164-
self._display.display(tasktime())
183+
self._display.display(tasktime(self))
165184

166185
def _record_task(self, task):
167186
"""
@@ -176,10 +195,11 @@ def _record_task(self, task):
176195
# with the same UUID is executed when `serial` is specified in a playbook.
177196
# elapsed: Elapsed time since the first serialized task was started
178197
self.current = task._uuid
198+
dtn = datetime.now().astimezone()
179199
if self.current not in self.stats:
180-
self.stats[self.current] = {'started': time.time(), 'elapsed': 0.0, 'name': task.get_name()}
200+
self.stats[self.current] = {'started': dtn, 'elapsed': 0.0, 'name': task.get_name()}
181201
else:
182-
self.stats[self.current]['started'] = time.time()
202+
self.stats[self.current]['started'] = dtn
183203
if self._display.verbosity >= 2:
184204
self.stats[self.current]['path'] = task.get_path()
185205

@@ -196,7 +216,7 @@ def playbook_on_stats(self, stats):
196216
# Align summary report header with other callback plugin summary
197217
self._display.banner("TASKS RECAP")
198218

199-
self._display.display(tasktime())
219+
self._display.display(tasktime(self))
200220
self._display.display(filled("", fchar="="))
201221

202222
timestamp(self)

0 commit comments

Comments
 (0)