Machine readable duration

18 views
Skip to first unread message

Matthew Kenigsberg

unread,
Aug 3, 2021, 11:29:19 AM8/3/21
to Ansible Development


I would like a machine readable duration for how long a command runs. I was told I could use existing filters. How would I accomplish that? strftime does not support parsing the output of str(timedelta), which is currently used for duration (see https://bugs.python.org/issue41254)

Brian Coca

unread,
Aug 3, 2021, 12:47:15 PM8/3/21
to Matthew Kenigsberg, Ansible Development
I was not aware of the datetime bug (was thinking to_datetime &
strftime), but it is still simple to do in jinja:

- debug: msg='{{seconds}}'
vars:
d: "{{res['delta'].split(':')}}"
seconds : "{{ d[0]|int * 3600 + d[1]|int * 60 + d[2]|float }}"


----------
Brian Coca

Matthew Kenigsberg

unread,
Aug 3, 2021, 1:05:02 PM8/3/21
to Ansible Development
That doesn't work for longer periods of time: "2 days, 2:02:02.000002"
It's definitely possible to parse (right now I'm using regex in Python), it's just annoying to have to manually write a regex for a non-standard format unique to Python timedeltas

Matthew Kenigsberg

unread,
Aug 3, 2021, 1:39:07 PM8/3/21
to Brian Coca, ansibl...@googlegroups.com
Makes sense. If it weren't for backwards compatibility I would definitely vote duration is machine readable, but maybe that's an issue with str(timedelta). Guess I'll just use regex 🤷

On Tue, Aug 3, 2021 at 1:31 PM Brian Coca <bc...@redhat.com> wrote:
Understood, we were not aware of the python bug when closing the
ticket, but a custom filter should work as well.

One of the performance bottlenecks we find is in module return data
serialization/unserialization and memory ballooning (since registered
vars are kept through the life of the run), so we are now very
conservative on adding more fields to module returns if they can be
calculated with existing information on the controller side.

Since now we have datetime filters, we would not add the current
'delta' field to command anymore since start/end information is
already there.

--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages