Convert Ansible stat's mtime output to YYMMDD-HHMMSS

1.2k Views Asked by At

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) }}"
0

There are 0 best solutions below