Calculate Elapsed Time

Hello all, :waving_hand:

today I tried to calculate the elapsed time of a module execution in a playbook.

Here an example code:

- name: Get start time
  ansible.builtin.set_fact:
    startTime: "{{ now() }}"

- name: The module to be measured
  ...

- name: Get end time
  ansible.builtin.set_fact:
    endTime: "{{ now() }}"

- name: Try to calculate elapsed time, but it does not work
  ansible.builtin.debug:
    msg: "{{ (endTime - startTime) }}"

Unfortunately I have not found a way to calculate the elapsed time.

Now I have two questions:

  1. What would be the correct approach for calculating with times?
  2. Is there another or better approach to measure the elapsed time of module executions in a playbook?

Thanks for hints and tips.
Best regards
Stefan

Check out the profile tasks callback.

It provides information on how much time was spent on each task in line, and a summary at the end.

3 Likes

what @Lyle_McKarns is the best approach, yours could work, but you are not giving any indication no ‘how it does not work’ so it is very hard to give you a solution.

Normally you want to give the exact error and if possible extended verbose and/or debug information when asking for help.

2 Likes

Hello Brian [@bcoca],

thanks for your reply. Yep, my mistake, the error is:

Type 'timedelta' is unsupported for variable storage.

This brings me to the solution:

- name: Print start and end time
  ansible.builtin.debug:
    msg: "{{ (endTime.timestamp() - startTime.timestamp()) }}"

Best regards
Stefan

Hello Lyle, [ @Lyle_McKarns ],
thanks for your reply and your excellent tip, great to know that.
Best regards
Stefan