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.


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
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
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
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
Donate
Want to keep this project going? Please donate