Ansible
ansible_date_time
troubleshooting
automation
error_handling

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.

yaml
1# View the full ansible_date_time dictionary
2- hosts: all
3  tasks:
4    - name: Display date time facts
5      debug:
6        var: ansible_date_time
7
8# Output:
9# ansible_date_time:
10#   date: "2025-03-02"
11#   day: "02"
12#   epoch: "1740902400"
13#   hour: "14"
14#   iso8601: "2025-03-02T14:00:00Z"
15#   minute: "00"
16#   month: "03"
17#   second: "00"
18#   time: "14:00:00"
19#   weekday: "Sunday"
20#   year: "2025"

Cause 1: gather_facts: false

This is the most common cause. Disabling fact gathering removes every ansible_* variable, not just hardware facts.

yaml
1# BROKEN — facts are not collected
2- hosts: webservers
3  gather_facts: false
4  tasks:
5    - name: Create log with timestamp
6      copy:
7        content: "Deployed at {{ ansible_date_time.iso8601 }}"
8        dest: /var/log/deploy.log
9# ERROR: 'ansible_date_time' is undefined

Turn fact gathering back on:

yaml
1# Option 1: Remove gather_facts (defaults to true)
2- hosts: webservers
3  tasks:
4    - name: Create log with timestamp
5      copy:
6        content: "Deployed at {{ ansible_date_time.iso8601 }}"
7        dest: /var/log/deploy.log
8
9# Option 2: Explicitly set gather_facts: true
10- hosts: webservers
11  gather_facts: true
12  tasks:
13    - name: Create log with timestamp
14      copy:
15        content: "Deployed at {{ ansible_date_time.iso8601 }}"
16        dest: /var/log/deploy.log

Gathering only date/time facts with setup

If you keep gather_facts: false for speed, collect just what you need with the setup module:

yaml
1- hosts: webservers
2  gather_facts: false
3  tasks:
4    - name: Gather only date/time facts
5      setup:
6        filter: ansible_date_time
7
8    - name: Use the date
9      debug:
10        msg: "Today is {{ ansible_date_time.date }}"

filter limits collection to matching keys, which is faster than gathering everything.

Cause 2: Using the fact before it is gathered

yaml
1# BROKEN — role runs before facts are ready
2- hosts: all
3  gather_facts: false
4  pre_tasks:
5    - name: This runs before fact gathering
6      debug:
7        msg: "{{ ansible_date_time.date }}"
8  # ERROR: undefined

Make sure gathering runs first, for example by putting a setup task at the top of pre_tasks:

yaml
1- hosts: all
2  gather_facts: false
3  pre_tasks:
4    - name: Gather facts first
5      setup:
6
7    - name: Now safe to use
8      debug:
9        msg: "{{ ansible_date_time.date }}"

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.

yaml
1- hosts: webservers
2  tasks:
3    - name: Run on localhost
4      debug:
5        msg: "{{ ansible_date_time.date }}"
6      delegate_to: localhost
7      # May fail if localhost facts were not gathered

Use delegate_facts, or gather facts on localhost in a separate play and read them through hostvars:

yaml
1- hosts: localhost
2  gather_facts: true
3  tasks: []
4
5- hosts: webservers
6  tasks:
7    - name: Use localhost facts via hostvars
8      debug:
9        msg: "{{ hostvars['localhost'].ansible_date_time.date }}"

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.

ini
1# ansible.cfg
2[defaults]
3fact_caching = jsonfile
4fact_caching_connection = /tmp/ansible_facts
5fact_caching_timeout = 86400
yaml
1# Force fresh facts even with caching enabled
2- hosts: all
3  gather_facts: true
4  tasks:
5    - name: Refresh facts
6      setup:
7      tags: [always]

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:

yaml
1- hosts: all
2  gather_facts: false
3  tasks:
4    - name: Get current time without facts
5      debug:
6        msg: "{{ now(utc=true).isoformat() }}"
7    # Output: 2025-03-02T14:00:00.123456 (naive UTC, no offset)
8
9    - name: Formatted date
10      debug:
11        msg: "{{ now().strftime('%Y-%m-%d %H:%M') }}"
12    # Output: 2025-03-02 14:00

The two are not interchangeable:

  • now() is evaluated at template render time on the controller and returns the controller's time.
  • ansible_date_time is collected on the target during fact gathering and reflects the target's clock and configured timezone. Hosts in different timezones return different values, so use ansible_date_time.iso8601 (UTC) for consistency.

If you need the target host's time, use ansible_date_time with fact gathering enabled.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design