Ansible Pilot

Using Date, Time and Timestamp without Facts in Ansible Playbook - Ansible date and lookup plugin

How to print the Date and Time and Timestamp in ISO 8601 format without using Ansible Facts using the date command-line utility in Ansible Playbook.

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

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 demo 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

ISO8601

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).

The Best Resources For Ansible

Certifications

Video Course

Printed Book

eBooks

demo

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: 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

$ ansible-playbook -i virtualmachines/demo/inventory variables/datetime_nofact.yml 

PLAY [date and time demo] *************************************************************************

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 <class 'ansible.errors.AnsibleError'>, original message: lookup_plugin.pipe(date --iso8601=seconds) returned 1. lookup_plugin.pipe(date --iso8601=seconds) returned 1"}
...ignoring

PLAY RECAP ****************************************************************************************
demo.example.com           : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1

code with ❤️ in GitHub

Recap

Now you know how to use the Date, Time and Timestamp without Ansible Facts in Ansible Playbook. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) 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