# Does the find module stores the files by sorting based on modification time?

**URL:** <https://forum.ansible.com/t/does-the-find-module-stores-the-files-by-sorting-based-on-modification-time/35441>\
**Category:** Ansible Project\
**Created:** [June 30, 2021, 1:00pm UTC](https://forum.ansible.com/t/does-the-find-module-stores-the-files-by-sorting-based-on-modification-time/35441 "2021-06-30T13:00:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arun\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Arun\_Kumar](https://forum.ansible.com/u/Arun_Kumar)\
**Post date:** [June 30, 2021, 1:00pm UTC](https://forum.ansible.com/t/does-the-find-module-stores-the-files-by-sorting-based-on-modification-time/35441/1 "2021-06-30T13:00:04Z")

</div>

Does the find module stores the files by sorting based on modification time?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 30, 2021, 1:42pm UTC](https://forum.ansible.com/t/does-the-find-module-stores-the-files-by-sorting-based-on-modification-time/35441/2 "2021-06-30T13:42:25Z")

</div>

I have no idea, and the docs don't mention any sorting.  
Even if the output was sorted I wouldn't rely on any sorting being  
there in the future.  
Instead, sort it yourself if you need that.  
It will be more explicit and stable.  
For example:

sorted\_files: "{{ files|sort(attribute='mtime')

---

<div class="post-metadata">

**Author:** ![user60](https://avatars.discourse-cdn.com/v4/letter/u/90ced4/32.png) [@user60](https://forum.ansible.com/u/user60)\
**Post date:** [June 30, 2021, 1:49pm UTC](https://forum.ansible.com/t/does-the-find-module-stores-the-files-by-sorting-based-on-modification-time/35441/3 "2021-06-30T13:49:00Z")

</div>

please help me

I installed ansible on mac os and installed vagrant server. After installation, when I run ansible -i hosts testserver -m ping -vvvv, it does not respond with the following message. I have not been able to solve the cause for a month.

**ansible\_host=127.0.0.1 | UNREACHABLE! =\> {**

**“changed”: false,**

**“msg”: “Failed to connect to the host via ssh: OpenSSH\_8.1p1, LibreSSL 2.7.3\r\ndebug1: Reading configuration data /etc/ssh/ssh\_config\r\ndebug1: /etc/ssh/ssh\_config line 47: Applying options for \*\r\ndebug1: auto-mux: Trying existing master\r\ndebug1: Control socket "/Users/gimjeonghyeon/.ansible/cp/edc683342f" does not exist\r\ndebug2: resolving "ansible\_host=127.0.0.1" port 2222\r\nssh: Could not resolve hostname ansible\_host=127.0.0.1: nodename nor servname provided, or not known”,**

**“unreachable”: true**

2021년 6월 30일 수요일 오후 10시 42분 53초 UTC+9에 dick…@geant.org님이 작성:

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 30, 2021, 2:36pm UTC](https://forum.ansible.com/t/does-the-find-module-stores-the-files-by-sorting-based-on-modification-time/35441/4 "2021-06-30T14:36:59Z")

</div>

pls don't hijack existing/unrelated threads just because they're active...

---

<div class="post-metadata">

**Author:** ![Arun\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Arun\_Kumar](https://forum.ansible.com/u/Arun_Kumar)\
**Post date:** [June 30, 2021, 5:05pm UTC](https://forum.ansible.com/t/does-the-find-module-stores-the-files-by-sorting-based-on-modification-time/35441/5 "2021-06-30T17:05:25Z")

</div>

Thanks, i’m able to sort it now by modification time.  
Getting below error, though its working fine when it is hardcoded. Could you advise if something wrong with this approach?

ansible 2.9.21.

- hosts: localhost  
gather\_facts: 0  
tasks:

- find:  
paths: /tmp  
register: op

- debug: msg=“{{(op.files|sort(attribute=“mtime”))[0].path}}”

- debug: msg=“{{(op.files|sort(attribute=“mtime”))[item].path}}”  
with\_sequence: start=1 end=“{{op.files|length-1}}”

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “/tmp/comp2.yml”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘0’\n\nThe error appears to be in ‘/opt/ansible/find’: line 11, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - debug: msg="{{(op.files|sort(attribute="mtime"))[0].path}}"\n - debug: msg="{{(op.files|sort(attribute="mtime"))[item].path}}"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with\_items:\n - {{ foo }}\n\nShould be written as:\n\n with\_items:\n - "{{ foo }}"\n”}

---

<div class="post-metadata">

**Author:** ![Arun\_Kumar](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@Arun\_Kumar](https://forum.ansible.com/u/Arun_Kumar)\
**Post date:** [July 1, 2021, 4:27am UTC](https://forum.ansible.com/t/does-the-find-module-stores-the-files-by-sorting-based-on-modification-time/35441/6 "2021-07-01T04:27:27Z")

</div>

I fixed it. Need to parse the items as int. so modified it to

- debug: msg=“{{(op.files|sort(attribute=“mtime”))[item|int].path}}”  
with\_sequence: start=0 end=“{{op.files|length-1}}”

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 1, 2021, 7:09am UTC](https://forum.ansible.com/t/does-the-find-module-stores-the-files-by-sorting-based-on-modification-time/35441/7 "2021-07-01T07:09:18Z")

</div>

Be that as it may, I don't see the need to fiddle with indexes, type  
casting and length logic.  
Just iterating over the list will give the same result:

&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="{{ item.path }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ op.files|sort(attribute='mtime') }}"
