Ansible - Increment date by 'X' days / minutes

The variable ansible_date_time.date

gives the current date and time stamp, however I want to increase this date by "X" minutes / days. Is there a built-in method or logic to do this?

The operator -

seems to work with date operands, however there doesn't seem to be an easy way to increment the date.

I want to accomplish this in the yml script itself, rather than using additional Python scripts as described in Is it possible to manipulate the date in the Ansible Playbook ?

+3


source to share


2 answers


The command module can be used as shown in the following snippet to increase the current date. This worked for me as expected.

- command: "date +'%d-%m-%Y' -d '+3 days'"
  register: result

- debug: msg="{{result.stdout}}"

      



This will return the date in dd-mm-yyyy format. For example, if today is the date "03-07-2017", it will increment the date by 3 days (as specified in the command shown in the above example snippet) and return "06-07-2017".

0


source


In Ansible 2.4 you can use strftime

to accomplish this using the epoch time and then convert back to a string using a filter strftime

.

For example, for one day equal to 86400 seconds, add 3 days:

- debug:
    msg: "{{ '%Y-%m-%d' | strftime( ( ansible_date_time.epoch | int ) + ( 86400 * 3 )  ) }}"

      



Within a few minutes, the multiplier will be 60 seconds and the appropriate temporal granularity must be enabled at the beginning of the date / time format string (e.g.% H,% M,% S, ...).

This filter is here .

+4


source







All Articles