I have a list of files. I want to iterate over a list of files retrieving only the path to a new list variable.
This gives me a list of files:
- name: sweep directory /foo/bar recursivly
find:
paths: /foo/bar
recurse: yes
patterns: "*"
hidden: True
register: sweep_result
- name: build file list
set_fact:
all_list: "{{ sweep_result.files | map(attribute='path') | list }}"
- debug: var=all_list
Conclusion:
TASK [debug] *************************************************
ok: [host] => {
"all_list": [
"/foo/bar/.hiddenfile",
"/foo/bar/file1",
"/foo/bar/folder/file1",
"/foo/bar/folder/file2.ext",
]
}
This is an example of what I want:
"paths": [
"/foo/bar/",
"/foo/bar/folder/",
]
I see that iterating over all_list like this works on every element separating the file and extension, leaving me with only a path. I don’t know how to do something like this by creating a new list variable in the process.
- debug: var=item[:item.rfind('/')]
with_items:
- "{{ all_list }}"
Iterating over a list can give me duplicate paths. Using a “unique” filter can solve this. I am sure there is the right jinja style filter to get the result. Thanks for any help.
source
share