Date Time Conversion

22 views
Skip to first unread message

javed khan Siddque

unread,
Jul 26, 2023, 5:09:14 AM7/26/23
to Ansible Project
Hi All,


I want to convert the below time to human readable format, i tried some of the jinza formatter but it is failing .
Failed
{{ iso_date|iso8601_to_time|datetimeformat('%a, %B %d') }}
requirement 2023-07-20 16:36:44
So i can convert this to <file_name>_2023_07_20_16_36_44 .txt


"created": "2023-07-20T16:36:44.396234Z", "modified": "2023-07-20T16:37:26.400464Z",

Abhijeet Kasurde

unread,
Jul 26, 2023, 11:53:10 AM7/26/23
to ansible...@googlegroups.com
I would use `regex_replace` instead -

```
    - debug:
        msg: '{{ iso_date |regex_replace("(\d{4})\-(\d{2})\-(\d{2})\ (\d{2}):(\d{2}):(\d{2})", "\1_\2_\3_\4_\5_\6.txt") }}'
      vars:
        iso_date: "2023-07-20 16:36:44"
```

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/db9e47cf-a72f-415f-8474-16265c7eb0ccn%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Andrew Latham

unread,
Jul 26, 2023, 2:20:09 PM7/26/23
to ansible...@googlegroups.com
https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
https://github.com/ansible/ansible/blob/devel/lib/ansible/template/__init__.py#L829-L839

typed without testing, sorry, give it a go

- name: create my file
  ansible.builtin.file:
    path: /tmp/{{ now(fmt="%Y_%m_%d_%H_%M_%S.txt") }}
    state: touch

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/db9e47cf-a72f-415f-8474-16265c7eb0ccn%40googlegroups.com.


--
- Andrew "lathama" Latham -
Message has been deleted

Todd Lewis

unread,
Jul 26, 2023, 3:25:53 PM7/26/23
to ansible...@googlegroups.com, uto...@gmail.com
      path: /tmp/"{{ now(fmt="%Y_%m_%d_%H_%M_%S.txt") }}"
should be

      path: /tmp/{{ now(fmt="%Y_%m_%d_%H_%M_%S.txt") }}

On 7/26/23 2:37 PM, dbs34 wrote:
cool I see thank you!  but how do you avoid the special characters in the file name?  like so.
' "2023_07_26_14_34_22.txt" '

---

- name: date format filename
  hosts: localhost
  gather_facts: 0

  tasks:


    - name: create my file
      ansible.builtin.file:
        path: /tmp/"{{ now(fmt="%Y_%m_%d_%H_%M_%S.txt") }}"
        state: touch
...
Reply all
Reply to author
Forward
0 new messages