datetimeansible

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


Solution

  • The command module can be used as shown in the following snippet to increment 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's date is "03-07-2017", it will increment the date by 3 days (as mentioned in the command shown in above example snippet) and would return "06-07-2017".