Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the newest filename in a directory in Ansible

Tags:

ansible

I have an Ansible script, and I am trying to get the filename of the newest item in a directory. I am using this Ansible script:

- name: Finding newest file in a folder
  find:
    paths: "/var/www/html/wwwroot/somefolder/"
    age: "latest"
    age_stamp: mtime

However, I am getting the following error -

FAILED! => {"age": "latest", "changed": false, "failed": true, "msg": "failed to process age"}

How can I get Ansible to retrieve the filename of the newest file in a directory?

like image 640
J. Doe Avatar asked Feb 01 '17 02:02

J. Doe


1 Answers

Pure Ansible solution:

- name: Get files in a folder
  find:
    paths: "/var/www/html/wwwroot/somefolder/"
  register: found_files

- name: Get latest file
  set_fact:
    latest_file: "{{ found_files.files | sort(attribute='mtime',reverse=true) | first }}"
like image 57
Konstantin Suvorov Avatar answered Oct 21 '22 16:10

Konstantin Suvorov