Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - Increment date by 'X' days/minutes

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

like image 442
Aishwaryameenakshi Avatar asked Jun 30 '17 07:06

Aishwaryameenakshi


1 Answers

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.

like image 140
Willem van Ketwich Avatar answered Oct 14 '22 20:10

Willem van Ketwich