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?
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 }}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With