I'm trying to use the Ansible find
builtin to create something similar to
find /home/user/data -maxdepth 2 -type d -name 'mul' -or -name 'sqr'
where /home/user/data
has folders a
,b
,c
... and in each of them could be none/one/both of mul
- or sqr
-directories.
It works well with that Linux query and with Ansible I tried
- name: find fields
find:
depth: 2
paths: "/home/user/data"
file_type: directory
patterns:
- 'mul'
- 'sqr'
register: fields
- name: debug1
debug:
msg: " myitem: {{ item }}"
loop: "{{ fields.files | map(attribute='path') }}"
but my debug output is empty.
I could remove the patterns
and set depth: 1
, resulting in a list a
,b
,c
and then iterate over those to find mul
and sqr
but that takes unnecessarily long if the lists get long.
As Vladimir said, setting recurse: yes
solves the issue.
I'm still wondering, why, when setting depth: 2
, recurse: yes
is not implied.
Here is the full example:
- name: find fields
find:
depth: 2
paths: "/home/user/data"
file_type: directory
recurse: yes
patterns:
- 'mul'
- 'sqr'
register: fields
- name: debug1
debug:
msg: " myitem: {{ item }}"
loop: "{{ fields.files | map(attribute='path') }}"