The variable ansible_date_time.date
gives the current date and time stamp, however I wish to increment this date by 'X' minutes/days. Is there any built in method or logic to do this?
The -
operator seems to work with date operands, however there doesn't seem to be any straightforward way to increment a date.
I want to accomplish this in a yml script itself and not by using additional Python scripting as described in Is it possible to manipulate the date in an Ansible Playbook
In Ansible 2.4 you can use strftime
to accomplish this by using epoch times and then converting back to a string using the strftime
filter.
For example, taking one day as 86400 seconds, to add 3 days would be:
- debug:
msg: "{{ '%Y-%m-%d' | strftime( ( ansible_date_time.epoch | int ) + ( 86400 * 3 ) ) }}"
For minutes, the multiplier would be 60 seconds, and the datetime format string at the start should include the appropriate time granularity (e.g. %H, %M, %S, ...).
There is some documentation on this filter here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With