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
Welcome to another episode of Ansible Pilot! I’m Luca Berton, and today we’re diving into the fascinating world of handling date, time, and timestamps in Ansible Playbooks. We’ll explore the ansible_date_time
variable, conduct a live demo, and provide you with simple Ansible code to get started.
The ansible_date_time
Variable
Ansible simplifies working with date and time information through the ansible_date_time
variable. This built-in variable comes packed with a wealth of information, neatly organized into key-value pairs. Let’s take a closer look at some of the key values it provides:
"ansible_date_time": {
"date": "2022-05-18",
"day": "18",
"epoch": "1652887408",
"hour": "15",
"iso8601": "2022-05-18T15:23:28Z",
"minute": "23",
"month": "05",
"second": "28",
"time": "15:23:28",
"tz": "UTC",
"weekday": "Wednesday",
"weekday_number": "3",
"year": "2022"
}
These values cover everything from the current date, time, and timezone to more detailed information like the day of the week, month, and year. This data can be immensely useful in various scenarios within your Ansible Playbook.
One important note: to leverage the ansible_date_time
variable, ensure that Ansible Facts are enabled in your playbook by including gather_facts: true
.
Playbook
Are you ready for a hands-on experience? Let’s jump into a quick live demo where we’ll showcase how to display the full ansible_date_time
and the ISO8601 format.
Ansible Playbook 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
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- 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 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
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
Execution
Run the following command to execute the playbook:
$ ansible-playbook -i inventory datetime_fact.yml
The output will provide detailed information about the current date and time, as well as the ISO8601 format.
Conclusion
Congratulations! You’ve just learned how to harness the power of date, time, and timestamp variables in your Ansible Playbooks. The ansible_date_time
variable opens up a world of possibilities for managing time-related data effortlessly. Feel free to explore and integrate this knowledge into your automation workflows.
Happy automating!
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