Ansible - Determine Time Difference
by
Jeremy Canfield |
Updated: August 07 2024
| Ansible articles
to_datetime can be used to determine the difference between two dates. In this example, the total number of seconds between two dates will be returned.
---
- hosts: all
tasks:
- ansible.builtin.debug:
msg: '{{ ("2016-08-14 20:00:12" | to_datetime - "2015-12-25 03:34:10" | to_datetime).total_seconds() }}'
...
By default, to_datetime expects the datetime to be in format %Y-%m-%d %H:%M:%S. If you have a string in some other format, you can include the format as a parameter.
---
- hosts: all
tasks:
- ansible.builtin.debug:
msg: '{{ ("2016-08-14 20:00:12" | to_datetime - "12/25/2015" | to_datetime("%m/%d/%Y")).total_seconds() }}'
...
Did you find this article helpful?
If so, consider buying me a coffee over at