ansiblejinja2

How to add time to the current timestamp in Ansible?


I am attempting to automate a few tasks in Ansible and while I've gotten everything else to work, I need to have a start_time and end_time variable that adds time to the current timestamp as the ServiceNow system does not accept a current timestamp when creating a change request.

For example, I have a variable start_time as "{{ lookup('pipe', date +\"%Y-%m-%d %r\"') }}" but I would need that to be the current time +5 minutes for example. Likewise on the end_date, but something like +15 minutes.


Solution

  • If you are going to use lookup_plugins and pipe, you may just add 5 mins by using date +"%Y-%m-%d %r" -d "5 mins".

    Thanks to

    Nevertheless it is recommend to use Ansible variables like ansible_date_time from facts.

    date_time:
      date: '2021-11-23'
      day: '23'
      epoch: '1637678908'
      hour: '15'
      iso8601: '2021-11-23T14:48:28Z'
      iso8601_basic: 20211123T154828773386
      iso8601_basic_short: 20211123T154828
      iso8601_micro: '2021-11-23T14:48:28.773386Z'
      minute: '48'
      month: '11'
      second: '28'
      time: '15:48:28'
      tz: CET
      tz_offset: '+0100'
      weekday: Tuesday
      weekday_number: '2'
      weeknumber: '47'
      year: '2021'
    

    Form there you could use epoch, minute or what would fit to your use case. See the other answer here.