# "invalid value" for a runtime function in Ansible playbook

**URL:** <https://forum.ansible.com/t/invalid-value-for-a-runtime-function-in-ansible-playbook/45781>\
**Category:** Get Help\
**Created:** [May 7, 2026, 12:31pm UTC](https://forum.ansible.com/t/invalid-value-for-a-runtime-function-in-ansible-playbook/45781 "2026-05-07T12:31:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![clerambeau370](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/clerambeau370/32/10260_2.png) [@clerambeau370](https://forum.ansible.com/u/clerambeau370)\
**Post date:** [May 7, 2026, 12:31pm UTC](https://forum.ansible.com/t/invalid-value-for-a-runtime-function-in-ansible-playbook/45781/1 "2026-05-07T12:31:24Z")

</div>

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:

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

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 7, 2026, 8:07pm UTC](https://forum.ansible.com/t/invalid-value-for-a-runtime-function-in-ansible-playbook/45781/2 "2026-05-07T20:07:26Z")

</div>

> [@clerambeau370](#):
>
> `time data '2026-05-07T12:14:03' does not match format '%Y-%m-%d %H:%M:%S'`

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 `T`s 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:

```yaml
---
# 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

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

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 7, 2026, 8:25pm UTC](https://forum.ansible.com/t/invalid-value-for-a-runtime-function-in-ansible-playbook/45781/3 "2026-05-07T20:25:00Z")

</div>

See the note about `.seconds` vs. `.total_seconds()` at [datetime.timedelta.seconds](https://docs.python.org/3/library/datetime.html#datetime.timedelta.seconds).

---

<div class="post-metadata">

**Author:** ![clerambeau370](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/clerambeau370/32/10260_2.png) [@clerambeau370](https://forum.ansible.com/u/clerambeau370)\
**Post date:** [May 7, 2026, 8:57pm UTC](https://forum.ansible.com/t/invalid-value-for-a-runtime-function-in-ansible-playbook/45781/4 "2026-05-07T20:57:39Z")

</div>

> [@utoddl](#):
>
> .total\_seconds()

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

---

<div class="post-metadata">

**Author:** ![clerambeau370](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/clerambeau370/32/10260_2.png) [@clerambeau370](https://forum.ansible.com/u/clerambeau370)\
**Post date:** [May 7, 2026, 9:14pm UTC](https://forum.ansible.com/t/invalid-value-for-a-runtime-function-in-ansible-playbook/45781/5 "2026-05-07T21:14:36Z")

</div>

> [@utoddl](#):
>
> ```auto
> - 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() }}"
> 
> ```

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

 ![image](https://us1.discourse-cdn.com/ansible/original/2X/4/454a0680cc992c0904f6a1c28c39573b30060d50.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 6, 2026, 9:15pm UTC](https://forum.ansible.com/t/invalid-value-for-a-runtime-function-in-ansible-playbook/45781/6 "2026-06-06T21:15:03Z")

</div>

Closed on June 6, 2026, 9:15pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
