Fix 'ansible_date_time' is undefined in Ansible
Codemia Knowledge Hub ·
'ansible_date_time' is undefined means Ansible facts were not collected before the task that uses the variable. ansible_date_time is a built-in fact holding the target host's date and time, and it only exists after fact gathering. The usual causes are gather_facts: false in the play, a task running on a host whose facts were never gathered, or misconfigured fact caching.
What ansible_date_time contains
It is a dictionary filled during fact gathering, with keys such as date, time, iso8601, epoch, year, month, day, hour, minute and second.
Cause 1: gather_facts: false
This is the most common cause. Disabling fact gathering removes every ansible_* variable, not just hardware facts.
Turn fact gathering back on:
Gathering only date/time facts with setup
If you keep gather_facts: false for speed, collect just what you need with the setup module:
filter limits collection to matching keys, which is faster than gathering everything.
Cause 2: Using the fact before it is gathered
Make sure gathering runs first, for example by putting a setup task at the top of pre_tasks:
Cause 3: Delegated tasks
A task delegated to another host sees that host's facts, not the original host's. If the delegated host's facts were never gathered, ansible_date_time is undefined.
Use delegate_facts, or gather facts on localhost in a separate play and read them through hostvars:
Cause 4: Fact caching
With fact caching (fact_caching = jsonfile or redis in ansible.cfg), a stale or empty cache can leave facts missing. A stale cache can also return the time from a previous run instead of the current one.
For time-sensitive tasks, disable caching or force a refresh.
Using now() instead of facts
Ansible 2.8+ has a now() function that does not need fact gathering:
The two are not interchangeable:
now()is evaluated at template render time on the controller and returns the controller's time.ansible_date_timeis collected on the target during fact gathering and reflects the target's clock and configured timezone. Hosts in different timezones return different values, so useansible_date_time.iso8601(UTC) for consistency.
If you need the target host's time, use ansible_date_time with fact gathering enabled.
Related reading
- Ansible idempotent MySQL installation Playbook
- Ansible playbook wait until all pods running
- Ansible showing task changed but the task has condition creates and does not actually execute
- ansible variables shared with multiple groups if inventory is only localhost
- ant warning 'includeantruntime' was not set
- Apache Beam pipeline running on Dataflow failed to read from KafkaIO SSL handshake failed
- Apache kafka production cluster setup problems
- Apache Storm java.nio.channels.ClosedChannelException null
System design interview problems on this topic
- Design a Metrics Monitoring and Alerting System: solution and walkthrough
- Design a Distributed Tracing System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.