"invalid value" for a runtime function in Ansible playbook

Hello. My task is calculate a playbook runtime. I get this error: “the field ‘args’ has an invalid value ({‘end_time’: ‘{{ ansible_date_time.iso8601[:19] }}’, ‘runtime’: ‘{{ ((ansible_date_time.iso8601[:19] | to_datetime) - (start_time | to_datetime)).seconds }}’}), and could not be converted to an dict.The error was: time data ‘2026-05-07T12:14:03’ does not match format ‘%Y-%m-%d %H:%M:%S’”

Here is the playbook with the actual tasks removed as they work fine (but may be the identation is the issue because there are a couple of conditional blocks by the time the comparison is made:

---
- name: Get all task metadata for the DCR project 
  hosts: localhost
  connection: local
  gather_facts: true
  vars_files:
    - vars/main.yml

  pre_tasks:
    - name: Set start time
      set_fact:
        start_time: "{{ ansible_date_time.iso8601[:19] }}"

  tasks:

    - name: Set fact for Ansible date_time | dcr_jira {{ ansible_date_time.iso8601 }}
      delegate_to: localhost
      run_once: true
      set_fact: 
        currentdate: "{{ ansible_date_time.date }}"
        tags: linux

Omitting some tasks, then:

            - name: Force update of current timestamp
              setup:
                filter: 'ansible_date_time'

            - name: Calculate runtime
              set_fact:
                end_time: "{{ ansible_date_time.iso8601[:19] }}"
                runtime: "{{ ((ansible_date_time.iso8601[:19] | to_datetime) - (start_time | to_datetime)).seconds }}"

The output:

TASK [Archive the scratchpad directory into a zip file, move to /dcr_archived | dcr_jira 2026-05-07T12:13:55Z] *****************************************************************************************************************************
changed: [localhost]

TASK [Attach the refresh worklog | dcr_jira 2026-05-07T12:13:55Z] **************************************************************************************************************************************************************************
changed: [localhost]

TASK [Force update of current timestamp] ***************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Calculate runtime] *******************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {}

MSG:

the field 'args' has an invalid value ({'end_time': '{{ ansible_date_time.iso8601[:19] }}', 'runtime': '{{ ((ansible_date_time.iso8601[:19] | to_datetime) - (start_time | to_datetime)).seconds }}'}), and could not be converted to an dict.The error was: time data '2026-05-07T12:14:03' does not match format '%Y-%m-%d %H:%M:%S'

The error appears to be in '/etc/ansible/dcr_jira.yml': line 388, column 15, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


            - name: Calculate runtime
              ^ here


PLAY RECAP *********************************************************************************************************************************************************************************************************************************
localhost                  : ok=30   changed=6    unreachable=0    failed=1    skipped=17   rescued=0    ignored=0   

[misoracle@ehsmg-mis-dba1 ansible]$ 

Why does the “runtime: “{{ ((ansible_date_time.iso8601[:19] | to_datetime) - (start_time | to_datetime)).seconds }}”” calculation fail? Both start and end formatted the same!

Thank you.

Nestor Kandinsky-Clerambeau.

That’s the problem: .iso8601 puts a “T” between the date and the time, and ansible.builtin.to_datetime uses a default format of '%Y-%m-%d %H:%M:%S'.

You can either remove the Ts with |regex_replace('T', ' '), or give ansible.builtin.to_datetime the format of your string

| ansible.builtin.to_datetime('%Y-%m-%dT%H:%M:%S')

Here’s your playbook with these changes:

---
# clerambeau370__45781_00.yml
- name: Calculate runtime
  hosts: localhost
  connection: local
  gather_facts: true

  pre_tasks:
    - name: Set start time
      ansible.builtin.set_fact:
        start_time: "{{ ansible_facts['date_time'].iso8601[:19] }}"

  tasks:

    - name: Set fact for Ansible date_time | dcr_jira {{ ansible_facts['date_time'].iso8601 }}
      delegate_to: localhost
      run_once: true
      ansible.builtin.set_fact:
        currentdate: "{{ ansible_facts['date_time'].date }}"

    - name: Force update of current timestamp
      ansible.builtin.setup:
        filter: 'ansible_date_time'

    - name: Show times
      ansible.builtin.debug:
        msg:
          - "start_time: {{ start_time }}"
          - "ansible_facts['date_time'].iso8601[:19]: {{ ansible_facts['date_time'].iso8601[:19] }}"

    - name: Calculate runtime
      ansible.builtin.set_fact:
        end_time: "{{   ansible_facts['date_time'].iso8601[:19] }}"
        runtime:  "{{ ((ansible_facts['date_time'].iso8601[:19] | ansible.builtin.to_datetime('%Y-%m-%dT%H:%M:%S'))
                      -
                      (start_time                               | ansible.builtin.to_datetime('%Y-%m-%dT%H:%M:%S'))
                     ).total_seconds() }}"

which produces

TASK [Calculate runtime] **************
ok: [localhost] => 
    ansible_facts:
        end_time: '2026-05-07T20:01:22'
        runtime: 2.0
    changed: false

See the note about .seconds vs. .total_seconds() at datetime.timedelta.seconds.

Outstanding, you keep saving me over and over and I am immensely appreciative. Testing.

@utoddl Ansible rocks, so do you. This 50 scripts run takes us 5 days. Here is what Ansible does: