AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

ansible_date_time: Use Date, Time & Timestamps in Ansible Playbooks — Video Tutorial

Learn how to use date, time, and timestamp in Ansible playbooks without facts. Follow our Playbook and simple Ansible code examples for quick solutions.

Watch Video

Watch "ansible_date_time: Use Date, Time & Timestamps in Ansible Playbooks" on YouTube

What You'll Learn

Full Tutorial Content

How to use Date, Time, and Timestamp without Facts in Ansible Playbook. A quick and dirty workaround using the `date` command-line utility. I'm going to show you a live Playbook and some simple Ansible code. I'm Luca Berton and welcome to today's episode of Ansible Pilot. Using Date, Time, and Timestamp without Facts in Ansible Playbook Date and time - `date +%Y-%m-%d@%H:%M:%S` ISO8601 - `date --iso-8601=seconds` - `date +%Y-%m-%dT%H:%M:%S%z` How to Use the Date, Time, and Timestamp in Ansible Playbook. The `ansible_data_time` fact is an amazing resource but sometimes you can't have it or you don't want to use the date and time of the remote host. These solutions enable you to print the Date and Time and ISO8601 format. I ended up after carefully reading the `date` man page. Unfortunately not all the platforms support the `--iso-8601` parameter so you need to build it manually the format by yourself (for example in macOS operating system). Links - [`ansible.builtin.pipe lookup plugin`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/pipe_lookup.html) - [`date man page`](https://man7.org/linux/man-pages/man1/date.1.html) - [ISO8601](https://www.iso.org/iso-8601-date-and-time-format.html) ## Playbook Let's jump into a quick live Playbook 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 ```yaml --- - name: date and time Playbook hosts: all gather_facts: false tasks: - name: date and time ansible.builtin.debug: msg: "{{ lookup('pipe', 'date +%Y-%m-%d@$H:%M:%S') }}" - name: iso8601 manual ansible.builtin.debug: msg: "{{ lookup('pipe', 'date +%Y-%m-%dT%H:%M:%S%z') }}" - name: iso8601 ansible.builtin.debug: msg: "{{ lookup('pipe', 'date --iso8601=seconds') }}" ignore_errors: true ``` execution ```bash $ ansible-playbook -i virtualmachines/demo/inventory variables/datetime_nofact.yml PLAY [date and time Playbook] ************************************************************************* TASK [date and time] ****************************************************************************** ok: [demo.example.com] => { "msg": "2022-05-22@:57:33" } TASK [iso8601 manual] ***************************************************************************** ok: [demo.example.com] => { "msg": "2022-05-22T14:57:33+0200" } TASK [iso8601] ************************************************************************************ date: illegal option -- - usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] fatal: [demo.example.com]: FAILED! => {"msg": "An unhandled exception occurred while running the lookup plugin 'pipe'. Error was a , original message: lookup_plugin.pipe(date --iso8601=seconds) returned 1. lookup_plugin.pipe(date -

About This Tutorial

Read the full written article: ansible_date_time: Use Date, Time & Timestamps in Ansible Playbooks

Topics Covered

Related Video Tutorials