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

32 views
Skip to first unread message

Arun Kumar

unread,
Jun 30, 2021, 9:00:04 AM6/30/21
to Ansible Project
Does the find module stores the files by sorting based on modification time?

Dick Visser

unread,
Jun 30, 2021, 9:42:53 AM6/30/21
to ansible...@googlegroups.com
On Wed, 30 Jun 2021 at 15:00, Arun Kumar <arunkuma...@gmail.com> wrote:
>
> Does the find module stores the files by sorting based on modification time?

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')



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

김정현

unread,
Jun 30, 2021, 9:49:01 AM6/30/21
to Ansible Project

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님이 작성:

Dick Visser

unread,
Jun 30, 2021, 10:37:26 AM6/30/21
to ansible...@googlegroups.com
pls don't hijack existing/unrelated threads just because they're active...
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4a6f5ddb-7f1d-45ed-9f1c-c5f3543c6930n%40googlegroups.com.

Arun Kumar

unread,
Jun 30, 2021, 1:05:25 PM6/30/21
to Ansible Project

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"}

Arun Kumar

unread,
Jul 1, 2021, 12:27:28 AM7/1/21
to Ansible Project
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}}"


Dick Visser

unread,
Jul 1, 2021, 3:09:46 AM7/1/21
to ansible...@googlegroups.com
On Thu, 1 Jul 2021 at 06:27, Arun Kumar <arunkuma...@gmail.com> wrote:
>
> 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}}"

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:

- debug: msg="{{ item.path }}"
loop: "{{ op.files|sort(attribute='mtime') }}"
Reply all
Reply to author
Forward
0 new messages