I need to find a files in unknown directory place and remove them. Tried to use "find" module, register its output, and pass it to "file".
Even if I see path registered, I can not use it later:
< TASK [print find_result] >
ok: [1.2.3.4] => {
"find_result": {
"changed": false,
"examined": 3119,
"files": [
{
"atime": 1483973253.7295375,
...
"mode": "0600",
"mtime": 1483973253.7295375,
"nlink": 1,
"path": "/tmp/delme",
My playbook is:
- hosts: "{{ target }}"
become: no
vars:
find_what: "delme*"
find_where: "/tmp"
tasks:
- name: finding files
find:
paths: "{{ find_where }}"
patterns: "{{ find_what }}"
recurse: "yes"
file_type: "file"
register: find_result
# \/ for debugging
- name: print find_result
debug: var=find_result
- name: remove files
file:
path= "{{ item.path }}"
state=absent
with_items: "{{ find_result.files }}"
Registering variables To see the values for a particular task, run your playbook with -v . Registered variables are stored in memory. You cannot cache registered variables for use in future plays. Registered variables are only valid on the host for the rest of the current playbook run.
There's a syntax flaw in file
task – space after =
.
Try:
- name: remove files
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ find_result.files }}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With