How to cast stat.mtime (float) to readable date-time

9 views
Skip to first unread message

Jim Griffiths

unread,
Apr 11, 2019, 10:36:25 AM4/11/19
to Ansible Project
I need to display the date-time that a file was last updated.   

The value can be obtained with stat on the file:

- name: Get file stats

     stat: path="{{ path_of_file }}"

  register: reg


- debug:

     msg:  “Timestamp of {{path}}: {{reg.stat.mtime}}”



However the output is meaningless because it is a float value.
The only solution I could find requires the creation of a custom filter:  https://stackoverflow.com/questions/41084097/ansible-cast-float-to-date

Is there a simpler way?

Thank you all.
Jim

Matt Martz

unread,
Apr 11, 2019, 10:42:59 AM4/11/19
to ansible...@googlegroups.com
You want something like this:

    - debug:
        msg: "Timestamp of {{ path }}: {{ '%c'|strftime(reg.stat.mtime) }}"

Documentation about the strftime filter can be found at https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html


--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/1812f74f-c435-4c81-a91e-edda66795dc8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Jim Griffiths

unread,
Apr 11, 2019, 3:30:54 PM4/11/19
to Ansible Project
Thanks for the advice, Matt!

Here's what I ended up using:

- debug:

    msg: “Timestamp of {{logpath}}: {{ '%Y-%m-%d %H:%M:%S' | strftime(reg.stat.mtime) }}”


Cheers

Jim



On Thursday, April 11, 2019 at 10:42:59 AM UTC-4, Matt Martz wrote:
You want something like this:

    - debug:
        msg: "Timestamp of {{ path }}: {{ '%c'|strftime(reg.stat.mtime) }}"

Documentation about the strftime filter can be found at https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html


On Thu, Apr 11, 2019 at 9:36 AM Jim Griffiths <jameswg...@gmail.com> wrote:
I need to display the date-time that a file was last updated.   

The value can be obtained with stat on the file:

- name: Get file stats

     stat: path="{{ path_of_file }}"

  register: reg


- debug:

     msg:  “Timestamp of {{path}}: {{reg.stat.mtime}}”



However the output is meaningless because it is a float value.
The only solution I could find requires the creation of a custom filter:  https://stackoverflow.com/questions/41084097/ansible-cast-float-to-date

Is there a simpler way?

Thank you all.
Jim

--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages