I have OS X "El capitan" 10.11.6 and I am using Ansible 2.1.1.0 to run some maintenance tasks on a remote Linux server Ubuntu 16.04 Xenial. I am trying to get the following list of folders sorted, so I can remove the old ones when needed:
/releases/0.0.0
/releases/0.0.1
/releases/0.0.10
/releases/1.0.0
/releases/1.0.5
/releases/2.0.0
I have been trying with the module find in Ansible, but it returns a not sorted list. Is there an easy way to achieve this 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:The response is:
If you want to find files older than a period, maybe
age
andage_stamp
parameters offind
module can help you. For example:It sounds like what you want to do is real simple but the standard
ansible
modules doesn't quite have what you needed.As an alternative you can write your own script using your favorite programming language then use the
copy
module to pass that script to the host and usecommand
to execute it. When done, usefile
to remove that script.The downside of it is that the target host will need to have the required executable to run your script. For instance if you are doing a python script then the target host will need
python
Example:
You can sort items with
sort
filter: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
.