-1

I have folders like this in my windows machine. using ansible want to sort this app-* pattern folder and get the last element. from this example I want to get d:\app-9.7.8 in ansible variable

d:\app-1.0.3
d:\app-1.0.7
d:\app-2.0.4
d:\app-7.0.4
d:\app-9.7.8

This code list the folder, but not sure how to sort and get the last element.

- name: Find dir
    win_find:
      paths: D:\
      recurse: no
      file_type: directory
    register: result

  - name: Find dir
    debug:
      msg: "output {{ result.files }}"
sfgroups
  • 18,151
  • 28
  • 132
  • 204
  • 1
    You may find this helpful : https://stackoverflow.com/questions/39423676/get-sorted-list-of-folders-with-ansible – aru_sha4 Jun 20 '20 at 16:28
  • With 12k reputation, one would expect you to know to include the code you have attempted to make it work, and the error message it is producing – mdaniel Jun 20 '20 at 16:39
  • @mdaniel, not getting the error, output is too long thats why didn't include – sfgroups Jun 20 '20 at 17:12

1 Answers1

0

I am getting the last element like this:

  - name: set dir
    set_fact:
      mule_dir_list: "{{ result.files | sort(attribute='path') | map(attribute='path') | list }}"
    when: "( result.files |length > 0 )"

  - name: print dir
    debug:
      msg: "dir {{ mule_dir_list[-1] }}"
sfgroups
  • 18,151
  • 28
  • 132
  • 204