How to add time to the current timestamp in Ansible?

0

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.

ansible jinja2
2021-11-23 13:22:47
3

2

Something like?

{{ ansible_date_time.date }}
{{ ansible_date_time.hour|int +1|int }}
{{ ansible_date_time.minute|int +15|int }}
2021-11-23 15:18:12

This is also very good, thank you!
oakenshield
1

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.

2021-11-23 14:50:21

I was successful in getting time added using date +"%Y-%m-%d %r" -d "+5 minutes" however I would like to achieve this using a variable file instead of having to execute that command in each playbook and storing the variable.
oakenshield

Turns out you can have it stored as a variable like "{{ lookup('pipe', 'date -d \"+5 minutes\" +\"%Y%m%d %r\"') }}"
oakenshield
0

You can use the strftime filter to format times, Ansible's built-in fact gathering to get the current time, and a little bit of arithmetic to add an offset.

- hosts: localhost
  tasks:
    - debug:
        msg:
          - "{{ '%Y-%m-%d %r' | strftime(ansible_facts.date_time.epoch | int + 300 ) }}"
          - "{{ '%Y-%m-%d %r' | strftime(ansible_facts.date_time.epoch | int + 900 ) }}"
TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": [
        "2021-11-23 01:10:45 PM",
        "2021-11-23 01:20:45 PM"
    ]
}
2021-11-23 14:41:37

This is very good, thank you!
oakenshield

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................