Ansible Pilot

Using Date, Time and Timestamp in Ansible Playbook - Ansible Tip and Tricks

How to use Date, Time, and Timestamp in ISO 8601 format in Ansible Playbook.

May 19, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to Use Date, Time, and Timestamp in Ansible Playbook.

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Using Date, Time, and Timestamp in Ansible Playbook

"ansible_date_time": {
        "date": "2022-05-18",
        "day": "18",
        "epoch": "1652887408",
        "epoch_int": "1652887408",
        "hour": "15",
        "iso8601": "2022-05-18T15:23:28Z",
        "iso8601_basic": "20220518T152328995863",
        "iso8601_basic_short": "20220518T152328",
        "iso8601_micro": "2022-05-18T15:23:28.995863Z",
        "minute": "23",
        "month": "05",
        "second": "28",
        "time": "15:23:28",
        "tz": "UTC",
        "tz_dst": "UTC",
        "tz_offset": "+0000",
        "weekday": "Wednesday",
        "weekday_number": "3",
        "weeknumber": "20",
        "year": "2022"

The good news is that Ansible provides the ansible_date_time variable that already has out-of-the-box information about date and time. This dictionary has a lot of keys and values that should be self-explicative and also enable a lot of use-cases. For example inside the Ansible Playbook, on debug messages, on output and input files. The only drawback is that the Ansible ansible_date_time variable requires Ansible Facts enabled, so the gather_facts: true statement on your Ansible Playbook.

The Best Resources For Ansible

Video Course

Printed Book

eBooks

demo

Are you ready to make your hands dirty? Let’s jump into a quick live demo of Using Date, Time, and Timestamp in the Ansible Playbook. I’m going to share with you how to display the full ansible_date_time and the ISO8601 format.

code

---
- name: date and time demo
  hosts: all
  gather_facts: true
  tasks:
    - name: date and time
      ansible.builtin.debug:
        var: ansible_date_time
    - name: ISO8601
      ansible.builtin.debug:
        var: ansible_date_time.iso8601

execution

$ ansible-playbook -i virtualmachines/demo/inventory variables/datetime_fact.yml 
PLAY [date and time demo] *************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [date and time] ******************************************************************************
ok: [demo.example.com] => {
    "ansible_date_time": {
        "date": "2022-05-19",
        "day": "19",
        "epoch": "1652970190",
        "epoch_int": "1652970190",
        "hour": "14",
        "iso8601": "2022-05-19T14:23:10Z",
        "iso8601_basic": "20220519T142310777257",
        "iso8601_basic_short": "20220519T142310",
        "iso8601_micro": "2022-05-19T14:23:10.777257Z",
        "minute": "23",
        "month": "05",
        "second": "10",
        "time": "14:23:10",
        "tz": "UTC",
        "tz_dst": "UTC",
        "tz_offset": "+0000",
        "weekday": "Thursday",
        "weekday_number": "4",
        "weeknumber": "20",
        "year": "2022"
    }
}
TASK [ISO8601] ************************************************************************************
ok: [demo.example.com] => {
    "ansible_date_time.iso8601": "2022-05-19T14:23:10Z"
}
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

code with ❤️ in GitHub

Recap

Now you know how to use the Date, Time and Timestamp Variable in Ansible Playbook. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases