1

I'm trying to extract a file's last modified time in the format YYMMDD-HHMMSS (ex:210422-135018) using Ansible's stat module.

I'm getting the file's mtime in a playbook:

    - name: stat
      stat:
        path: /path/to/file
      register: file
    - debug:
        msg: "{{ file.stat.mtime }}"

Which returns the mtime in EPOCH format (ex: 1632916899.9357276)

I've tried following instructions in the Ansible docs, but could not get it to work.

Is there a simple way to do this via Ansible, or is it best to do it via the shell module?

Thanks!

UPDATE: Solved with help from comment below:

    - name: stat
      stat:
        path: /path/to/file
      register: file
    - debug:
        msg: "{{ '%Y%m%d-%H%M%S' | strftime(file.stat.mtime) }}"
dkd6
  • 65
  • 1
  • 6
  • 1
    Please don't use the language "it doesn't work" when asking others for help. Show what you _did_ try and include the error message or bad outcome you got from your attempt. I strongly suspect you will want to read about [`"..." | strftime(file.stat.mtime)`](https://docs.ansible.com/ansible/2.10/user_guide/playbooks_filters.html#handling-dates-and-times) – mdaniel Sep 29 '21 at 15:00
  • Thanks for your respone! I apologise if I came across as ungrateful - that was certainly not my intention. I've read the documentation at the link you sent, but it didn't quite 'click' until the example you've added. – dkd6 Sep 29 '21 at 15:36
  • @dkd6, you can also answer your own question and mark them later as accepted answer. – U880D Sep 29 '21 at 16:44

0 Answers0