# Format date/time variable in Jinja2 template

**URL:** <https://forum.ansible.com/t/format-date-time-variable-in-jinja2-template/10934>\
**Category:** Get Help\
**Created:** [November 15, 2024, 3:43pm UTC](https://forum.ansible.com/t/format-date-time-variable-in-jinja2-template/10934 "2024-11-15T15:43:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 15, 2024, 3:43pm UTC](https://forum.ansible.com/t/format-date-time-variable-in-jinja2-template/10934/1 "2024-11-15T15:43:23Z")

</div>

I have a playbook that sets a fact for user info:

- name: Get User Account Info  
set\_fact:  
user\_info: “{{ user\_show.results | json\_query(‘[\*].json.result.result.{uid: uid[0], locked: krbloginfailedcount[0], disabled: nsaccountlock, pwdexp: krbpasswordexpiration[0]. **datetime** }’) | list }}”

Later in the playbook, I use the template module to format up an HTML file that I email out. In that file, I’m trying to format the pwdexp variable above as a date/time string:

{% for user in user\_info %}

{{ user.uid }} {{ user.locked }} {{ user.disabled }} {{ user.pwdexp|to\_datetime('%m/%d/%Y %H:%M:%S').strftime('%s') }} {% endfor %}

The error I’m getting is: AnsibleError: template error while templating string: expected token ‘end of print statement’, got ‘.’

Any ideas on what I’m doing wrong?

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 15, 2024, 4:04pm UTC](https://forum.ansible.com/t/format-date-time-variable-in-jinja2-template/10934/2 "2024-11-15T16:04:37Z")

</div>

I’d try adding some debugging before the template task, for example:

```yaml
- name: Debug user variable
  ansible.builtin.debug:
    var: user
  loop: user_info
  loop_control:
    loop_var: user

- name: Debug user.pwdexp variable
  ansible.builtin.debug:
    var: user.pwdexp
  loop: user_info
  loop_control:
    loop_var: user

- name: Debug user.pwdexp | to_datetime variable
  ansible.builtin.debug:
    var: user.pwdexp | to_datetime('%m/%d/%Y %H:%M:%S')
  loop: user_info
  loop_control:
    loop_var: user

- name: Debug user.pwdexp | to_datetime.strftime variable
  ansible.builtin.debug:
    var: (user.pwdexp | to_datetime('%m/%d/%Y %H:%M:%S')).strftime('%s')
  loop: user_info
  loop_control:
    loop_var: user

```

However I think this might fix the template:

```jinja2
{% for user in user_info %}
{{ user.uid }} {{ user.locked }} {{ user.disabled }} {{ (user.pwdexp|to_datetime('%m/%d/%Y %H:%M:%S')).strftime('%s') }}
{% endfor %} 

```

Note the extra set of brackets.

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 15, 2024, 4:11pm UTC](https://forum.ansible.com/t/format-date-time-variable-in-jinja2-template/10934/3 "2024-11-15T16:11:28Z")

</div>

So I added the extra parentheses in my template file, but now the playbook is giving the following error:

ValueError: time data ‘20241230121558Z’ does not match format ‘%m/%d/%Y %H:%M:%S’

Does the to\_datetime and format have to match? I want to have 20241230121558 displayed as “12/30/2024 12:15:58Z”.

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 15, 2024, 4:21pm UTC](https://forum.ansible.com/t/format-date-time-variable-in-jinja2-template/10934/4 "2024-11-15T16:21:07Z")

</div>

> [@harry\_devine](#):
>
> Does the to\_datetime and format have to match?

Yes I, think so, see the documentation [here](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/to_datetime_filter.html) and [here](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/strftime_filter.html).

What is an example value of `user.pwdexp`?

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [November 15, 2024, 4:22pm UTC](https://forum.ansible.com/t/format-date-time-variable-in-jinja2-template/10934/5 "2024-11-15T16:22:27Z")

</div>

I figured it out. I changed the template to the following:

{{ (user.pwdexp|to\_datetime(‘%Y%m%d%H%M%SZ’)).strftime(‘%m/%d/%Y %H:%M:%S’) }}

So the to\_datetime matches the data format, and strftime prints that how I need it. All good now.

Thanks,  
Harry

---

<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:** [December 15, 2024, 4:23pm UTC](https://forum.ansible.com/t/format-date-time-variable-in-jinja2-template/10934/6 "2024-12-15T16:23:24Z")

</div>

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