Reputation: 833
I am working on small playbook that will search for files in specified directories and then delete them if they met certain conditions. I have following playbook so far
- hosts:
- localhost
gather_facts: false
tasks:
- name: find logs
find:
paths: "{{ item.0 }}"
file_type: file
patterns: "{{ item.1 }}"
register: find_logs
with_nested:
- ["/var/log/apache2", "/var/log/nginx"]
- ["access.log", "error.log"]
- debug:
var: item
loop:
- "{{ find_logs }}"
So this will obviously look into /var/log/apache2 and /var/log/nginx directories and search for access.log and error.log files. Now, following ansible's documentation I want to access files
return values and their paths. The issue I'm having right now is with nested loop and registered find_logs
variable which holds list of dictionaries in results
key. If I do find_logs.results
then I will get a list of dictionaries and each of these dictionaries will have another list of files kept in files
section. How can I 'flatten' this list even more to be able to retrieve files.path
for every element produced by nested loop? To be honest I also tried find_logs | json_query('results[*].files[*]')
but that gives me another list and I can't seem to iterate over it to get what I want (which is path of file). Any idea on how to make this work?
Upvotes: 1
Views: 895
Reputation: 833
I completely misunderstood documentation for find
module, Nested loop can be excluded in this case and replaced with following syntax
- hosts:
- localhost
gather_facts: false
tasks:
- name: find logs
find:
paths:
- /var/log/apache2
- /var/log/nginx
file_type: file
patterns:
- "access.log"
- "error.log"
- "other_vhosts_access.log"
register: find_logs
- name: check what was registered
debug:
msg: "my path -> {{ item.path }}"
with_items:
- "{{ find_logs.files }}"
Upvotes: 2