I have a directory that will have a series of subdirectories added to it, and I want to maintain only the most recent 5. Here are the two tasks that I think should accomplish this:
- name: Get directory listing
shell: ls -tr1 chdir={{ project_root}}/sources
register: sources_list
- name: Pare directories
shell: rm -rf {{ sources_list.stdout_lines[0] }}; ls -tr1 chdir={{ project_root}}/sources
register: sources_list
until: sources_list.stdout_lines|length <= 5
retries: 10
My thinking is that the first task will produce an ordered list of directory names and register it to sources_list
. The second task will then get the name of the oldest directory (the first line in sources_list
, rm -rf
it, then output the list of the rest of the directories. It will do this until there are 5 directories or less in the list.
Instead I get this error message:
error while evaluating conditional: sources_list.stdout_lines|length <= 5
I have already confirmed that sources_list.stdout_lines|length
does evaluate to the number of files in the directory, and that sources_list
is correctly updated after the first iteration of the loop. But for some reason that conditional doesn't work. Any guidance would be appreciated.