Ansible - Getting date and timestamp

Knowing date and timestamp comes handy in many tasks involving automation. We can use these for logging purposes, include in the names of output files, and directories. We can even decide if the task should execute at all based on the current timestamp.
 

Ansible "ansible_date_time" fact

There are a number of ways in which we can retrieve the values for the current date and timestamp. For example, we can take advantage of the facts that Ansible gathers at the beginning of each playbook. Ansible will record the current date and time in the variable "ansible_date_time".

Below is the playbook using that fact:

# get-timestamp-fact.yml
---
- name: Uses ansible_date_time fact to retrieve data and timestamp
  hosts: localhost

  tasks:

  - name: Ansible fact - ansible_date_time
    debug:
     var: ansible_date_time

  - name: Ansible fact - ansible_date_time.iso8601
    debug:
     msg: "Timestamp in the ISO8601 format: {{ ansible_date_time.iso8601 }}"

And below is the output of this playbook:

[przemek@quasar timestamp]$ ansible-playbook get-timestamp-fact.yml

PLAY [Uses ansible_date_time fact to retrieve data and timestamp] ****************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]

TASK [Ansible fact - ansible_date_time] ******************************************************************************************
ok: [localhost] => {
    "ansible_date_time": {
        "date": "2017-09-30",
        "day": "30",
        "epoch": "1506803206",
        "hour": "13",
        "iso8601": "2017-09-30T20:26:46Z",
        "iso8601_basic": "20170930T132646356294",
        "iso8601_basic_short": "20170930T132646",
        "iso8601_micro": "2017-09-30T20:26:46.356428Z",
        "minute": "26",
        "month": "09",
        "second": "46",
        "time": "13:26:46",
        "tz": "PDT",
        "tz_offset": "-0700",
        "weekday": "Saturday",
        "weekday_number": "6",
        "weeknumber": "39",
        "year": "2017"
    }
}

TASK [Ansible fact - ansible_date_time.iso8601] **********************************************************************************
ok: [localhost] => {
    "msg": "Timestamp in the ISO8601 format: 2017-09-30T20:26:46Z"
}

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

As we can see in the above output, "ansible_date_time" fact holds a number of keys, which we can use to retrieve current date, day, hour, timestamps using a few flavours of the iso8601 format, as well as the timezone and a few other more specific values. In other words Ansible gives us a number of useful formats that we can use in our tasks, saving us from having to manually manipulate timestamps.
 

System data and timestamp

There are times when we don't want to use Ansible's fact gathering abilities. Perhaps it would take too long to gather facts if we are running our playbook against hundreds of hosts. Whatever the reason, we can instead opt to use "shell" module, and retrieve the timestamp from the underlying OS.

The below playbook shows how we could retrieve date and time, including nanoseconds, from the OS, and assign it to variables:

# get-timestamp-os.yml
---
- name: Uses shell module to retrieve data and timestamp from the OS
  hosts: localhost

  tasks:

  - name: Get timestamp from the system
    shell: "date +%Y-%m-%d%H-%M-%S"
    register: tstamp

  - name: Get timestamp from the system, include the first 5 nanoseconds digits
    shell: "date +%Y-%m-%d%H-%M-%S.%5N"
    register: tstamp_ns

  - name: Set variables
    set_fact:
     cur_date: "{{ tstamp.stdout[0:10]}}"
     cur_time: "{{ tstamp.stdout[10:]}}"
     cur_time_ns: "{{ tstamp_ns.stdout[10:]}}"

  - name: System timestamp - date
    debug:
     msg:  "Current date: {{ cur_date }}"

  - name: System timestamp - time
    debug:
     msg:  "Current date: {{ cur_time }}"

  - name: System timestamp - time, including the first 5 nanoseconds digits
    debug:
     msg:  "Current date: {{ cur_time_ns }}"

Below is the playbook's output:

[przemek@quasar timestamp]$ ansible-playbook get-timestamp-os.yml

PLAY [Uses shell module to retrieve data and timestamp from the OS] ****************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]

TASK [Get timestamp from the system] *********************************************************************************************
changed: [localhost]

TASK [Get timestamp from the system, include the first 5 nanoseconds digits] *****************************************************
changed: [localhost]

TASK [Set variables] *************************************************************************************************************
ok: [localhost]

TASK [System timestamp - date] ***************************************************************************************************
ok: [localhost] => {
    "msg": "Current date: 2017-09-30"
}

TASK [System timestamp - time] ***************************************************************************************************
ok: [localhost] => {
    "msg": "Current date: 13-26-12"
}

TASK [System timestamp - time, including the first 5 nanoseconds digits] *********************************************************
ok: [localhost] => {
    "msg": "Current date: 13-26-13.04317"
}

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=7    changed=2    unreachable=0    failed=0

These are just two possible ways of using date and timestamp in your playbooks. I'm sure there are others, but this should be enough to cover most of your needs.

You can get full listings of the playbooks from my GitHub repository:
https://github.com/progala/ttl255.com/tree/master/ansible/getting-date-and-timestamp