Get sorted list of folders with Ansible
Interesting solutions, thanks a lot guys. But I think I have found the easiest way in Ubuntu, just using ls -v /releases/
will apply natural sorting to all folders:
- name: List of releases in ascendent order
command: ls -v /releases/
register: releases
- debug: msg={{ releases.stdout_lines }}
The response is:
ok: [my.remote.com] => {
"msg": [
"0.0.0",
"0.0.1",
"0.0.10",
"1.0.0",
"1.0.5",
"2.0.0"
]
}
If you want to find files older than a period, maybe age
and age_stamp
parameters of find
module can help you. For example:
# Recursively find /tmp files older than 4 weeks and equal or greater than 1 megabyte
- find: paths="/tmp" age="4w" size="1m" recurse=yes
You can sort items with sort
filter:
- hosts: localhost
gather_facts: no
tasks:
- find: path="/tmp" patterns="test*"
register: files
- debug: msg="{{ files.files | sort(attribute='ctime') | map(attribute='path') | list }}"
Just change sort attribute to your need.
But beware that string sort is not numeric, so /releases/1.0.5
will go after /releases/1.0.10
.