Getting the latest file name in a directory in Ansible

I have an ansible script and am trying to get the file name of a new 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 get the following error:

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

How can I get Ansible to get the file name with the newest file in the directory?

+6
source share
1 answer

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 }}" 
+11
source

Source: https://habr.com/ru/post/1014610/


All Articles